Example usage for com.google.gson JsonDeserializationContext deserialize

List of usage examples for com.google.gson JsonDeserializationContext deserialize

Introduction

In this page you can find the example usage for com.google.gson JsonDeserializationContext deserialize.

Prototype

public <T> T deserialize(JsonElement json, Type typeOfT) throws JsonParseException;

Source Link

Document

Invokes default deserialization on the specified object.

Usage

From source file:com.github.autermann.matlab.json.MatlabRequestSerializer.java

License:Open Source License

@Override
public MatlabRequest deserialize(JsonElement elem, Type type, JsonDeserializationContext ctx)
        throws JsonParseException {
    JsonObject json = elem.getAsJsonObject();
    String function = json.get(MatlabJSONConstants.FUNCTION).getAsString();
    MatlabRequest request = new MatlabRequest(function);

    JsonObject results = json.get(MatlabJSONConstants.RESULTS).getAsJsonObject();
    for (Entry<String, JsonElement> result : results.entrySet()) {
        request.addResult(result.getKey(), parseType(result.getValue()));
    }//from ww  w .  j ava  2s  .com

    JsonArray parameters = json.get(MatlabJSONConstants.PARAMETERS).getAsJsonArray();
    for (JsonElement parameter : parameters) {
        MatlabValue value = ctx.deserialize(parameter, MatlabValue.class);
        request.addParameter(value);
    }

    return request;
}

From source file:com.github.autermann.matlab.json.MatlabResultSerializer.java

License:Open Source License

@Override
public MatlabResult deserialize(JsonElement elem, Type type, JsonDeserializationContext ctx)
        throws JsonParseException {
    MatlabResult mlresult = new MatlabResult();
    JsonObject results = elem.getAsJsonObject().get(MatlabJSONConstants.RESULTS).getAsJsonObject();

    for (Entry<String, JsonElement> result : results.entrySet()) {
        MatlabValue value = ctx.deserialize(result.getValue(), MatlabValue.class);
        mlresult.addResult(result.getKey(), value);
    }//from ww w  .  j  a  v a2s  . co  m
    return mlresult;
}

From source file:com.github.duhongming.highcharts.json.SeriesDeserializer.java

License:Open Source License

@Override
/**//from  www . ja v a2  s .c  o m
 * json,typeOfT,context
 *
 * @param json
 * @param typeOfT
 * @param context
 */
public Series deserialize(JsonElement json, Type typeOfT, JsonDeserializationContext context)
        throws JsonParseException {
    final JsonObject jsonObject = json.getAsJsonObject();
    String _type = jsonObject.get("type").getAsString();
    SeriesType type = SeriesType.valueOf(_type);
    Series series = null;
    switch (type) {
    case line:
        series = context.deserialize(jsonObject, Line.class);
        break;
    //            case bar:
    //                series = context.deserialize(jsonObject, Bar.class);
    //                break;
    //            case scatter:
    //                series = context.deserialize(jsonObject, Scatter.class);
    //                break;
    //            case funnel:
    //                series = context.deserialize(jsonObject, Funnel.class);
    //                break;
    case pie:
        series = context.deserialize(jsonObject, Pie.class);
        break;
    //            case force:
    //                series = context.deserialize(jsonObject, Force.class);
    //                break;
    //            case gauge:
    //                series = context.deserialize(jsonObject, Gauge.class);
    //                break;
    //            case map:
    //                series = context.deserialize(jsonObject, Map.class);
    //                break;
    //            case island:
    //                series = context.deserialize(jsonObject, Island.class);
    //                break;
    //            case k:
    //                series = context.deserialize(jsonObject, K.class);
    //                break;
    //            case radar:
    //                series = context.deserialize(jsonObject, Radar.class);
    //                break;
    //            case chord:
    //                series = context.deserialize(jsonObject, Chord.class);
    //                break;
    }
    return series;
}

From source file:com.github.easyjsonapi.adapters.EasyJsonApiDeserializer.java

License:Apache License

/**
 * Deserializer when occur an error/*from w w  w . j  a  va2 s.  c o m*/
 * 
 * @param jsonElem
 *            the json element
 * @param jsonContext
 *            the json context
 * @return the json api object with values created
 */
private JsonApi deserializerError(JsonElement jsonElem, JsonDeserializationContext jsonContext) {

    JsonApi request = new JsonApi();

    JsonArray jsonArrayErrors = jsonElem.getAsJsonObject().get("errors").getAsJsonArray();

    // Iterate the errors list
    for (int index = 0; index < jsonArrayErrors.size(); index++) {

        JsonObject jsonError = jsonArrayErrors.get(index).getAsJsonObject();

        String jsonApiErrorDetail = JsonTools.getStringInsideJson("detail", jsonError);
        String jsonApiErrorCode = JsonTools.getStringInsideJson("code", jsonError);
        String jsonApiErrorTitle = JsonTools.getStringInsideJson("title", jsonError);
        String jsonApiErrorId = JsonTools.getStringInsideJson("id", jsonError);

        Source jsonApiErrorSource = null;
        HttpStatus jsonApiErrorStatus = null;

        // Get the source json
        if (Assert.notNull(jsonError.get("source"))) {
            JsonObject jsonErrorSource = jsonError.get("source").getAsJsonObject();
            jsonApiErrorSource = jsonContext.deserialize(jsonErrorSource, Source.class);
        }

        // Get the http status json
        if (Assert.notNull(jsonError.get("status"))) {
            JsonObject jsonErrorStatus = jsonError.get("status").getAsJsonObject();
            jsonApiErrorStatus = HttpStatus.getStatus(Integer.valueOf(jsonErrorStatus.getAsString()));
        }

        Error jsonApiError = new Error(jsonApiErrorId, jsonApiErrorTitle, jsonApiErrorStatus, jsonApiErrorCode,
                jsonApiErrorDetail, Nullable.OBJECT, jsonApiErrorSource);

        request.addError(jsonApiError);
    }

    return request;
}

From source file:com.github.easyjsonapi.adapters.EasyJsonApiDeserializer.java

License:Apache License

/**
 * Deserializer object json for {@link EasyJsonApiTypeToken} sent to method
 * //from   ww  w.jav a 2s  .  c  om
 * @param name
 *            the name of json element need extract
 * @param json
 *            the json object
 * @param typeToken
 *            the type token to convert json
 * @param jsonContext
 *            the json context
 * @return one instance of typeToken sent
 */
private Object deserializerObject(String name, JsonObject json, EasyJsonApiTypeToken typeToken,
        JsonDeserializationContext jsonContext) {

    Object objDeserialized = Nullable.OBJECT;

    if (Assert.notNull(json.get(name))) {
        JsonObject jsonObject = json.get(name).getAsJsonObject();

        Type type = Assert.notNull(this.tokenTypesToUse.get(EasyJsonApiTypeToken.TOKEN_DEFAULT))
                ? this.tokenTypesToUse.get(EasyJsonApiTypeToken.TOKEN_DEFAULT)
                : this.tokenTypesToUse.get(typeToken);

        if (Assert.isNull(type)) {
            throw new EasyJsonApiCastException(
                    "Doesn't find token for " + name + " resource object inside json!");
        }

        objDeserialized = jsonContext.deserialize(jsonObject, type);
    }

    return objDeserialized;

}

From source file:com.google.devtools.moe.client.gson.GsonUtil.java

License:Apache License

@SuppressWarnings("unchecked") // Type is assured by gson's context.deserialize method.
public static <T> T getPropertyOrLegacy(JsonDeserializationContext context, Class<T> type, JsonElement json,
        String elementName, String legacyName) {
    JsonObject obj = json.getAsJsonObject();
    JsonElement element = obj.get(legacyName);
    if (element == null) {
        element = obj.get(elementName);/*from  w ww  .  ja v  a 2s  .  c  om*/
    }
    if (element == null) {
        throw new JsonParseException(type.getSimpleName() + " is missing a " + elementName);
    }
    return (T) context.deserialize(element, type);
}

From source file:com.google.devtools.moe.client.gson.ImmutableListDeserializer.java

License:Apache License

@Override
public ImmutableList<?> deserialize(JsonElement json, Type type, JsonDeserializationContext context)
        throws JsonParseException {
    Type[] typeArguments = ((ParameterizedType) type).getActualTypeArguments();
    ParameterizedType listType = SimpleParameterizedType.create(List.class, typeArguments);
    List<?> list = context.deserialize(json, listType);
    return (list == null) ? ImmutableList.of() : ImmutableList.copyOf(list);
}

From source file:com.google.gerrit.server.events.EventDeserializer.java

License:Apache License

@Override
public Event deserialize(JsonElement json, Type typeOfT, JsonDeserializationContext context)
        throws JsonParseException {
    if (!json.isJsonObject()) {
        throw new JsonParseException("Not an object");
    }// w  ww .j  a v a 2 s .c  o m
    JsonElement typeJson = json.getAsJsonObject().get("type");
    if (typeJson == null || !typeJson.isJsonPrimitive() || !typeJson.getAsJsonPrimitive().isString()) {
        throw new JsonParseException("Type is not a string: " + typeJson);
    }
    String type = typeJson.getAsJsonPrimitive().getAsString();
    Class<?> cls = EventTypes.getClass(type);
    if (cls == null) {
        throw new JsonParseException("Unknown event type: " + type);
    }
    return context.deserialize(json, cls);
}

From source file:com.google.gerrit.server.events.SupplierDeserializer.java

License:Apache License

@Override
public Supplier<?> deserialize(JsonElement json, Type typeOfT, JsonDeserializationContext context)
        throws JsonParseException {
    checkArgument(typeOfT instanceof ParameterizedType);
    ParameterizedType parameterizedType = (ParameterizedType) typeOfT;
    if (parameterizedType.getActualTypeArguments().length != 1) {
        throw new JsonParseException("Expected one parameter type in Supplier interface.");
    }/*from   w ww . jav a2s .  c  o  m*/
    Type supplierOf = parameterizedType.getActualTypeArguments()[0];
    return Suppliers.ofInstance(context.deserialize(json, supplierOf));
}

From source file:com.google.gwtjsonrpc.server.CallDeserializer.java

License:Apache License

public CallType deserialize(final JsonElement json, final Type typeOfT,
        final JsonDeserializationContext context) throws JsonParseException, NoSuchRemoteMethodException {
    if (!json.isJsonObject()) {
        throw new JsonParseException("Expected object");
    }/*from w w  w.ja  v  a  2s  . c om*/

    final JsonObject in = json.getAsJsonObject();
    req.id = in.get("id");

    final JsonElement jsonrpc = in.get("jsonrpc");
    final JsonElement version = in.get("version");
    if (isString(jsonrpc) && version == null) {
        final String v = jsonrpc.getAsString();
        if ("2.0".equals(v)) {
            req.versionName = "jsonrpc";
            req.versionValue = jsonrpc;
        } else {
            throw new JsonParseException("Expected jsonrpc=2.0");
        }

    } else if (isString(version) && jsonrpc == null) {
        final String v = version.getAsString();
        if ("1.1".equals(v)) {
            req.versionName = "version";
            req.versionValue = version;
        } else {
            throw new JsonParseException("Expected version=1.1");
        }
    } else {
        throw new JsonParseException("Expected version=1.1 or jsonrpc=2.0");
    }

    final JsonElement method = in.get("method");
    if (!isString(method)) {
        throw new JsonParseException("Expected method name as string");
    }

    req.method = server.lookupMethod(method.getAsString());
    if (req.method == null) {
        throw new NoSuchRemoteMethodException();
    }

    final JsonElement callback = in.get("callback");
    if (callback != null) {
        if (!isString(callback)) {
            throw new JsonParseException("Expected callback as string");
        }
        req.callback = callback.getAsString();
    }

    final JsonElement xsrfKey = in.get("xsrfKey");
    if (xsrfKey != null) {
        if (!isString(xsrfKey)) {
            throw new JsonParseException("Expected xsrfKey as string");
        }
        req.xsrfKeyIn = xsrfKey.getAsString();
    }

    final Type[] paramTypes = req.method.getParamTypes();
    final JsonElement params = in.get("params");
    if (params != null) {
        if (!params.isJsonArray()) {
            throw new JsonParseException("Expected params array");
        }

        final JsonArray paramsArray = params.getAsJsonArray();
        if (paramsArray.size() != paramTypes.length) {
            throw new JsonParseException("Expected " + paramTypes.length + " parameter values in params array");
        }

        final Object[] r = new Object[paramTypes.length];
        for (int i = 0; i < r.length; i++) {
            final JsonElement v = paramsArray.get(i);
            if (v != null) {
                r[i] = context.deserialize(v, paramTypes[i]);
            }
        }
        req.params = r;
    } else {
        if (paramTypes.length != 0) {
            throw new JsonParseException("Expected params array");
        }
        req.params = JsonServlet.NO_PARAMS;
    }

    return req;
}