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:org.rapla.rest.gwtjsonrpc.server.CallDeserializer.java

License:Apache License

@Override
public ActiveCall deserialize(final JsonElement json, final Type typeOfT,
        final JsonDeserializationContext context) throws JsonParseException, NoSuchRemoteMethodException {
    if (!json.isJsonObject()) {
        throw new JsonParseException("Expected object");
    }//from  www  .  j a  v a2 s .co  m

    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");
    }

    String asString = method.getAsString();
    req.method = server.lookupMethod(asString);
    if (req.method == null) {
        throw new NoSuchRemoteMethodException(server.getInterfaceClass() + "." + asString);
    }

    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;
}

From source file:org.rapla.server.jsonrpc.CallDeserializer.java

License:Apache License

@Override
public CallType deserialize(final JsonElement json, final Type typeOfT,
        final JsonDeserializationContext context) throws JsonParseException, NoSuchRemoteMethodException {
    if (!json.isJsonObject()) {
        throw new JsonParseException("Expected object");
    }/*from  ww w . ja  va  2 s.  co  m*/

    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;
}

From source file:org.redpin.android.json.BaseSpinnerDataTypeAdapter.java

License:Open Source License

/**
 * @see JsonDeserializer#deserialize(JsonElement, Type,
 *      JsonDeserializationContext)// w  w  w .ja v  a2 s.  com
 */
public SpinnerData deserialize(JsonElement json, Type typeOfT, JsonDeserializationContext context)
        throws JsonParseException {
    return context.deserialize(json, SpinnerData.class);
}

From source file:org.redpin.android.json.BaseUserTypeAdapter.java

License:Open Source License

/**
 * @see JsonDeserializer#deserialize(JsonElement, Type,
 *      JsonDeserializationContext)/*from w  w w .  ja  va 2s  .c o m*/
 */
public org.redpin.base.core.User deserialize(JsonElement json, Type typeOfT, JsonDeserializationContext context)
        throws JsonParseException {
    return context.deserialize(json, User.class);
}

From source file:org.redpin.android.json.MeasurementTypeAdapter.java

License:Open Source License

/**
 * @see JsonDeserializer#deserialize(JsonElement, Type,
 *      JsonDeserializationContext)/*from  ww  w .  j  a  v a 2  s  .co m*/
 */
public Measurement deserialize(JsonElement json, Type typeOfT, JsonDeserializationContext context)
        throws JsonParseException {

    // get all json elements in order to deserialize them separately
    JsonObject obj = json.getAsJsonObject();
    JsonElement json_timestamp = obj.get("timestamp");
    JsonElement json_wifi = obj.get("wifiReadings");
    JsonElement json_gsm = obj.get("gsmReadings");
    JsonElement json_bluetooth = obj.get("bluetoothReadings");

    // init vectors
    Vector<WiFiReading> wifi = new Vector<>();
    Vector<GSMReading> gsm = new Vector<>();
    Vector<BluetoothReading> bluetooth = new Vector<>();

    // deserialize reading vectors
    Type listType;
    if (json_wifi != null) {
        listType = new TypeToken<Vector<WiFiReading>>() {
        }.getType();
        Collection<WiFiReading> wificol = context.deserialize(json_wifi, listType);
        wifi.addAll(wificol);
    }

    if (json_gsm != null) {
        listType = new TypeToken<Vector<GSMReading>>() {
        }.getType();
        Collection<GSMReading> gsmcol = context.deserialize(json_gsm, listType);
        gsm.addAll(gsmcol);
    }

    if (json_bluetooth != null) {
        listType = new TypeToken<Vector<BluetoothReading>>() {
        }.getType();
        Collection<BluetoothReading> bluetoothcol = context.deserialize(json_bluetooth, listType);
        bluetooth.addAll(bluetoothcol);
    }
    // create deserialized measurement
    Measurement m = new Measurement(gsm, wifi, bluetooth);
    if (json_timestamp != null) {
        m.setTimestamp((Long) context.deserialize(json_timestamp, Long.class));
    }

    return m;
}

From source file:org.redpin.server.standalone.json.BaseFingerprintTypeAdapter.java

License:Open Source License

/**
 * @see JsonDeserializer#deserialize(JsonElement, Type, JsonDeserializationContext)
 *//* www  .ja  v  a 2 s .  c o  m*/
@Override
public Fingerprint deserialize(JsonElement json, Type typeOfT, JsonDeserializationContext context)
        throws JsonParseException {
    return context.deserialize(json, Fingerprint.class);
}

From source file:org.redpin.server.standalone.json.BaseHistoryTypeAdapter.java

License:Open Source License

/**
 * @see JsonDeserializer#deserialize(JsonElement, Type, JsonDeserializationContext)
 *///from   w ww. j a v a 2s. c o  m
@Override
public History deserialize(JsonElement json, Type typeOfT, JsonDeserializationContext context)
        throws JsonParseException {
    return context.deserialize(json, History.class);
}

From source file:org.redpin.server.standalone.json.BaseLocationTypeAdapter.java

License:Open Source License

/**
 * @see JsonDeserializer#deserialize(JsonElement, Type, JsonDeserializationContext)
 *///  w w  w  .j a v a2 s.  co m
@Override
public Location deserialize(JsonElement json, Type typeOfT, JsonDeserializationContext context)
        throws JsonParseException {

    return context.deserialize(json, Location.class);
}

From source file:org.redpin.server.standalone.json.BaseMapTypeAdapter.java

License:Open Source License

/**
 * @see JsonDeserializer#deserialize(JsonElement, Type, JsonDeserializationContext)
 *//*from  w  ww  .j  av a 2 s.  c  o  m*/
@Override
public Map deserialize(JsonElement json, Type typeOfT, JsonDeserializationContext context)
        throws JsonParseException {
    return context.deserialize(json, Map.class);
}

From source file:org.redpin.server.standalone.json.BaseMeasurementTypeAdapter.java

License:Open Source License

/**
 * @see JsonDeserializer#deserialize(JsonElement, Type, JsonDeserializationContext)
 *//*  w  w w . ja  v  a2s. co m*/
@Override
public Measurement deserialize(JsonElement json, Type typeOfT, JsonDeserializationContext context)
        throws JsonParseException {

    return context.deserialize(json, Measurement.class);
}