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.graphhopper.json.geo.FeatureJsonDeserializer.java

License:Apache License

@Override
public JsonFeature deserialize(JsonElement json, Type type, JsonDeserializationContext context)
        throws JsonParseException {
    try {//from ww w  .  j  av  a2  s  . co m
        JsonFeature feature = new JsonFeature();
        JsonObject obj = json.getAsJsonObject();

        // TODO ensure uniqueness
        if (obj.has("id"))
            feature.id = obj.get("id").getAsString();
        else
            feature.id = UUID.randomUUID().toString();

        if (obj.has("properties")) {
            Map<String, Object> map = context.deserialize(obj.get("properties"), Map.class);
            feature.properties = map;
        }

        if (obj.has("bbox"))
            feature.bbox = parseBBox(obj.get("bbox").getAsJsonArray());

        if (obj.has("geometry")) {
            JsonObject geometry = obj.get("geometry").getAsJsonObject();

            if (geometry.has("coordinates")) {
                if (!geometry.has("type"))
                    throw new IllegalArgumentException("No type for non-empty coordinates specified");

                String strType = context.deserialize(geometry.get("type"), String.class);
                feature.type = strType;
                if ("Point".equals(strType)) {
                    JsonArray arr = geometry.get("coordinates").getAsJsonArray();
                    double lon = arr.get(0).getAsDouble();
                    double lat = arr.get(1).getAsDouble();
                    if (arr.size() == 3)
                        feature.geometry = new Point(lat, lon, arr.get(2).getAsDouble());
                    else
                        feature.geometry = new Point(lat, lon);

                } else if ("MultiPoint".equals(strType)) {
                    feature.geometry = parseLineString(geometry);

                } else if ("LineString".equals(strType)) {
                    feature.geometry = parseLineString(geometry);

                } else {
                    throw new IllegalArgumentException("Coordinates type " + strType + " not yet supported");
                }
            }
        }

        return feature;

    } catch (Exception ex) {
        throw new JsonParseException("Problem parsing JSON feature " + json);
    }
}

From source file:com.heartbuffer.pipette.json.FilterConfigJsonAdapter.java

License:Open Source License

@Override
public FilterConfig deserialize(JsonElement json, Type typeOfT, JsonDeserializationContext context)
        throws JsonParseException {
    JsonObject jsonObject = json.getAsJsonObject();
    JsonPrimitive primitive = (JsonPrimitive) jsonObject.get(TYPE_FIELD);
    String filterType = primitive.getAsString();

    Class<?> clazz = null;/*w  ww . j  av  a 2 s.  c  o m*/

    switch (filterType) {
    case AddTimestampFilter.TYPE:
        clazz = AddTimestampFilterConfig.class;
        break;
    case EsDetermineDocumentDataFilter.TYPE:
        clazz = EsDetermineDocumentDataFilterConfig.class;
        break;
    case NDiffFilter.TYPE:
        //TODO: Add NDiffFilterConfig
        break;
    default:
        throw new JsonParseException("No such filter of type '" + filterType + "'");
    }

    return context.deserialize(jsonObject, clazz);
}

From source file:com.heartbuffer.pipette.json.InputConfigJsonAdapter.java

License:Open Source License

@Override
public InputConfig deserialize(JsonElement json, Type typeOfT, JsonDeserializationContext context)
        throws JsonParseException {
    JsonObject jsonObject = json.getAsJsonObject();
    JsonPrimitive primitive = (JsonPrimitive) jsonObject.get(TYPE_FIELD);
    String inputType = primitive.getAsString();

    Class<?> clazz = null;/*from   w  ww. ja v a  2  s.c om*/

    switch (inputType) {
    case FileInput.TYPE:
        clazz = FileInputConfig.class;
        break;
    default:
        throw new JsonParseException("No such input of type '" + inputType + "'");
    }

    return context.deserialize(jsonObject, clazz);
}

From source file:com.heartbuffer.pipette.json.OutputConfigJsonAdapter.java

License:Open Source License

@Override
public OutputConfig deserialize(JsonElement json, Type typeOfT, JsonDeserializationContext context)
        throws JsonParseException {
    JsonObject jsonObject = json.getAsJsonObject();
    JsonPrimitive primitive = (JsonPrimitive) jsonObject.get(TYPE_FIELD);
    String outputType = primitive.getAsString();

    Class<?> clazz = null;/*www  . j a  v a2 s  .c om*/

    switch (outputType) {
    case FileOutput.TYPE:
        //TODO: Add FileOutputConfig
        break;
    case ElasticsearchTransportOutput.TYPE:
        clazz = ElasticsearchTransportOutputConfig.class;
        break;
    case ElasticsearchHttpOutput.TYPE:
        clazz = ElasticsearchHttpOutputConfig.class;
        break;
    default:
        throw new JsonParseException("No such output of type '" + outputType + "'");
    }

    return context.deserialize(jsonObject, clazz);
}

From source file:com.heroiclabs.sdk.android.util.json.DatastoreItemJsonDeserializer.java

License:Apache License

/** {@inheritDoc} */
@Override/*  www .  j a v  a2 s  .c o  m*/
public DatastoreItem deserialize(final @NonNull JsonElement json, final @NonNull Type typeOfT,
        final @NonNull JsonDeserializationContext context) throws JsonParseException {
    final JsonObject object = json.getAsJsonObject();
    final DatastoreItem.Metadata metadata = context.deserialize(object.get("metadata"),
            DatastoreItem.Metadata.class);
    final String data = JsonDeserializerUtils.extractAsString(object, "data");

    return new DatastoreItem(metadata, data, codec);
}

From source file:com.hkm.disqus.api.gson.ApplicationsUsageDeserializer.java

License:Apache License

@Override
public Usage deserialize(JsonElement json, Type typeOfT, JsonDeserializationContext context)
        throws JsonParseException {
    // JSON element should be an array
    if (json.isJsonArray()) {
        // Create usage
        Usage usage = new Usage();

        // Iterate the array
        for (JsonElement element : json.getAsJsonArray()) {
            // Each element should be an array containing a date and int
            if (element.isJsonArray() && element.getAsJsonArray().size() == 2) {
                JsonArray jsonArray = element.getAsJsonArray();
                Date date = context.deserialize(jsonArray.get(0), Date.class);
                int count = jsonArray.get(1).getAsInt();
                usage.put(date, count);//from   ww  w  .  j a  va 2  s.c o m
            }
        }
        return usage;
    }
    return null;
}

From source file:com.ibasco.agql.protocols.valve.csgo.webapi.adapters.CsgoDatacenterStatusDeserializer.java

License:Open Source License

@Override
public List<CsgoDatacenterStatus> deserialize(JsonElement json, Type typeOfT,
        JsonDeserializationContext context) throws JsonParseException {
    log.debug("{}", json);
    JsonObject dataCenters = json.getAsJsonObject();
    List<CsgoDatacenterStatus> datacenterStatusList = new ArrayList<>();
    for (Map.Entry<String, JsonElement> entry : dataCenters.entrySet()) {
        String location = entry.getKey();
        CsgoDatacenterStatus status = context.deserialize(entry.getValue(), CsgoDatacenterStatus.class);
        status.setLocation(location);//  ww  w  . ja v  a 2  s  . c om
        datacenterStatusList.add(status);
    }
    return datacenterStatusList;
}

From source file:com.ibasco.agql.protocols.valve.steam.webapi.adapters.SteamAssetClassInfoMapDeserializer.java

License:Open Source License

@Override
public Map<String, SteamAssetClassInfo> deserialize(JsonElement json, Type typeOfT,
        JsonDeserializationContext context) throws JsonParseException {
    Map<String, SteamAssetClassInfo> map = new HashMap<>();
    JsonObject jObject = json.getAsJsonObject();
    for (Map.Entry<String, JsonElement> entry : jObject.entrySet()) {
        String key = entry.getKey();
        JsonElement value = entry.getValue();
        //Only process entries that are of type object
        if (value.isJsonObject()) {
            map.put(key, context.deserialize(value, SteamAssetClassInfo.class));
        }/*www.j a va2  s .c  o  m*/
    }
    log.debug("Finished populating map. Total Map Size: {}", map.size());
    return map;
}

From source file:com.ibasco.agql.protocols.valve.steam.webapi.adapters.SteamAssetDescDeserializer.java

License:Open Source License

@Override
public SteamAssetDescription deserialize(JsonElement json, Type typeOfT, JsonDeserializationContext context)
        throws JsonParseException {
    SteamAssetDescription desc = new SteamAssetDescription();
    JsonObject jsonObj = json.getAsJsonObject();
    String type = jsonObj.getAsJsonPrimitive("type").getAsString();
    String value = jsonObj.getAsJsonPrimitive("value").getAsString();
    Map<String, String> mAppData = new HashMap<>();
    JsonElement appData = jsonObj.get("app_data");
    if (appData.isJsonObject()) {
        desc.setAppData(context.deserialize(appData, HashMap.class));
    } else {// www  .  ja  va2s .  co m
        desc.setAppData(mAppData);
    }
    desc.setType(type);
    desc.setValue(value);
    return desc;
}

From source file:com.ibm.common.activitystreams.internal.ASObjectAdapter.java

License:Apache License

/**
 * Method deserialize.//from  w  w w  . jav  a  2 s. co m
 * @param element JsonElement
 * @param type Type
 * @param context JsonDeserializationContext
 * @return ASObject 
 * @throws JsonParseException
 * @see com.google.gson.JsonDeserializer#deserialize(JsonElement, Type, JsonDeserializationContext) 
 **/
public final ASObject deserialize(JsonElement element, Type type, JsonDeserializationContext context)
        throws JsonParseException {

    JsonObject obj = (JsonObject) element;
    ASObject.AbstractBuilder<?, ?> builder = null;
    Model propMap = null;
    TypeValue tv = null;

    if (knowsType(type)) {
        builder = builderFor(type);
        propMap = modelFor(type);
    } else {
        if (obj.has("objectType")) {
            tv = context.deserialize(obj.get("objectType"), TypeValue.class);
            @SuppressWarnings("rawtypes")
            Class<? extends ASObject.AbstractBuilder> _class = schema.builderForObjectTypeOrClass(tv.id(),
                    (Class) type);
            if (_class != null) {
                propMap = schema.forObjectClassOrType(_class, tv.id());
                if (!_class.isInterface()) {
                    try {
                        builder = _class.getConstructor(String.class).newInstance(tv.id());
                    } catch (Throwable t) {
                        try {
                            builder = _class.newInstance();
                            builder.set("objectType", tv);
                        } catch (Throwable t2) {
                            builder = Makers.object(tv);
                        }
                    }
                } else
                    builder = Makers.object(tv);
            } else {
                builder = Makers.object(tv);
                propMap = schema.forObjectClassOrType(ASObject.Builder.class, tv.id());
            }
        } else {
            if (obj.has("verb") && (obj.has("actor") || obj.has("object") || obj.has("target"))) {
                builder = activity();
                propMap = schema.forObjectClassOrType(Activity.Builder.class, "activity");
            } else if (obj.has("items")) {
                builder = collection();
                propMap = schema.forObjectClassOrType(Collection.Builder.class, "collection");
            } else {
                @SuppressWarnings("rawtypes")
                Class<? extends ASObject.AbstractBuilder> _class = schema.builderFor((Class) type);
                if (_class != null) {
                    if (!_class.isInterface()) {
                        try {
                            builder = _class.newInstance();
                        } catch (Throwable t) {
                            builder = object();
                        }
                    } else
                        builder = object();
                }
                if (builder == null)
                    builder = object(); // anonymous
                propMap = schema.forObjectClass(builder.getClass());
                propMap = propMap != null ? propMap : schema.forObjectClass(ASObject.Builder.class);
            }
        }
    }

    for (Entry<String, JsonElement> entry : obj.entrySet()) {
        String name = entry.getKey();
        if (name.equalsIgnoreCase("objectType"))
            continue;
        Class<?> _class = propMap.get(name);
        JsonElement val = entry.getValue();
        if (val.isJsonPrimitive())
            builder.set(name, _class != null ? context.deserialize(val, _class)
                    : primConverter.convert(val.getAsJsonPrimitive()));
        else if (val.isJsonArray()) {
            builder.set(name,
                    LinkValue.class.isAssignableFrom(_class != null ? _class : Object.class)
                            ? context.deserialize(val, LinkValue.class)
                            : convert(val.getAsJsonArray(), _class, context, builder()));
        } else if (val.isJsonObject())
            builder.set(name, context.deserialize(val, propMap.has(name) ? propMap.get(name) : ASObject.class));
    }
    return builder.get();

}