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.eclipse.recommenders.internal.coordinates.rcp.DependencyInfoJsonTypeAdapter.java

License:Open Source License

@Override
public DependencyInfo deserialize(JsonElement json, Type typeOfT, JsonDeserializationContext context)
        throws JsonParseException {
    JsonObject jsonObject = json.getAsJsonObject();

    JsonPrimitive fileElement = jsonObject.getAsJsonPrimitive("location"); //$NON-NLS-1$
    File file = new File(fileElement.getAsString());

    JsonElement typeElement = jsonObject.get("type"); //$NON-NLS-1$
    DependencyType type = context.deserialize(typeElement, DependencyType.class);

    JsonElement hintsElement = jsonObject.get("hints"); //$NON-NLS-1$
    Map<String, String> hints = context.deserialize(hintsElement, cacheType);

    return new DependencyInfo(file, type, hints);
}

From source file:org.eclipse.recommenders.utils.gson.MultimapTypeAdapter.java

License:Open Source License

@Override
public Multimap deserialize(final JsonElement json, final Type typeOfT,
        final JsonDeserializationContext context) throws JsonParseException {
    final Multimap multimap = HashMultimap.create();
    final Map<Object, Collection> map = context.deserialize(json, createMapType(typeOfT));
    for (Entry<Object, Collection> entry : map.entrySet()) {
        multimap.putAll(entry.getKey(), entry.getValue());
    }//from w w  w  . j  a v  a 2s  .c o  m

    return multimap;
}

From source file:org.eclipse.recommenders.utils.gson.OptionalJsonTypeAdapter.java

License:Open Source License

@Override
public Optional<T> deserialize(JsonElement json, Type typeOfT, JsonDeserializationContext context)
        throws JsonParseException {
    if (json.getAsString().equals(ABSENT)) {
        return absent();
    } else {//www  .ja va  2s  . co  m
        final T entry = context.deserialize(json, ((ParameterizedType) typeOfT).getActualTypeArguments()[0]);
        return fromNullable(entry);
    }
}

From source file:org.eclipse.smarthome.storage.json.StringObjectMapDeserializer.java

License:Open Source License

@Override
public Map<String, Object> deserialize(JsonElement json, Type typeOfT, JsonDeserializationContext context)
        throws JsonParseException {

    Map<String, Object> map = new HashMap<String, Object>();

    JsonObject obj = json.getAsJsonObject();

    for (Map.Entry<String, JsonElement> me : obj.entrySet()) {
        String k = me.getKey();//from   w ww. ja v  a 2 s.  co m
        JsonElement v = me.getValue();

        if (v.isJsonPrimitive() && ((JsonPrimitive) v).isNumber()) {
            map.put(k, v.getAsBigDecimal());
        } else {
            Object value = context.deserialize(v, Object.class);
            map.put(k, value);
        }
    }
    return map;
}

From source file:org.fracturedatlas.athena.util.date.DateTimeTypeConverter.java

License:Open Source License

@Override
public DateTime deserialize(JsonElement json, Type type, JsonDeserializationContext context)
        throws JsonParseException {
    try {//from  www  .j av  a 2s . co  m
        return new DateTime(json.getAsString());
    } catch (IllegalArgumentException e) {
        // May be it came in formatted as a java.util.Date, so try that
        Date date = context.deserialize(json, Date.class);
        return new DateTime(date);
    }
}

From source file:org.immutables.mongo.fixture.holder.HolderJsonSerializer.java

License:Apache License

@Override
public Holder deserialize(JsonElement json, Type type, JsonDeserializationContext context)
        throws JsonParseException {
    JsonObject root = (JsonObject) json;

    ImmutableHolder.Builder builder = ImmutableHolder.builder();

    if (root.has("id")) {
        builder.id(root.get("id").getAsString());
    }//  w  w  w .j a va  2 s  . c  o  m

    JsonElement value = root.get(VALUE_PROPERTY);
    if (value == null) {
        throw new JsonParseException(String.format("%s not found for %s in JSON", VALUE_PROPERTY, type));
    }

    if (value.isJsonObject()) {
        final String valueTypeName = value.getAsJsonObject().get(Holder.TYPE_PROPERTY).getAsString();
        try {
            Class<?> valueType = Class.forName(valueTypeName);
            builder.value(context.deserialize(value, valueType));
        } catch (ClassNotFoundException e) {
            throw new JsonParseException(
                    String.format("Couldn't construct value class %s for %s", valueTypeName, type), e);
        }
    } else if (value.isJsonPrimitive()) {
        final JsonPrimitive primitive = value.getAsJsonPrimitive();
        if (primitive.isString()) {
            builder.value(primitive.getAsString());
        } else if (primitive.isNumber()) {
            builder.value(primitive.getAsInt());
        } else if (primitive.isBoolean()) {
            builder.value(primitive.getAsBoolean());
        }
    } else {
        throw new JsonParseException(String.format("Couldn't deserialize %s : %s. Not a primitive or object",
                VALUE_PROPERTY, value));
    }

    return builder.build();

}

From source file:org.infoglue.deliver.externalsearch.ExternalSearchManager.java

License:Open Source License

private void initGSon() {
    final Type configType = new TypeToken<Map<String, Object>>() {
    }.getType();/*  w w  w. java 2s . c  o m*/
    class DelegateDeserializer<T extends ExternalSearchDelegate> implements JsonDeserializer<T> {
        @Override
        public T deserialize(JsonElement json, Type typeOfT, JsonDeserializationContext context)
                throws JsonParseException {
            try {
                JsonObject obj = (JsonObject) json;
                Class<?> clazz = Class.forName(obj.get("class").getAsString());
                @SuppressWarnings("unchecked")
                T delegate = (T) clazz.newInstance();

                if (obj.has("config")) {
                    Map<String, Object> config = context.deserialize(obj.get("config"), configType);
                    delegate.setConfig(config);
                }

                return delegate;
            } catch (Exception ex) {
                throw new JsonParseException(
                        "Failed to deserialize element. Exception message: " + ex.getMessage(), ex);
            }
        }

    }
    class DelegateConfigDeserializer implements JsonDeserializer<Map<String, Object>> {
        @Override
        public Map<String, Object> deserialize(JsonElement json, Type typeOfT,
                JsonDeserializationContext context) throws JsonParseException {
            try {
                Map<String, Object> config = new HashMap<String, Object>();
                JsonObject configObject = json.getAsJsonObject();
                for (Map.Entry<String, JsonElement> configEntry : configObject.entrySet()) {
                    if (configEntry.getValue().isJsonObject()) {
                        config.put(configEntry.getKey(),
                                context.deserialize(configEntry.getValue(), configType));
                    } else if (configEntry.getValue().isJsonPrimitive()
                            && ((JsonPrimitive) configEntry.getValue()).isString()) {
                        config.put(configEntry.getKey(), configEntry.getValue().getAsString());
                    }
                }

                return config;
            } catch (Exception ex) {
                throw new JsonParseException(
                        "Failed to deserialize element. Exception message: " + ex.getMessage(), ex);
            }
        }

    }

    GsonBuilder gson = new GsonBuilder();

    Type fieldsType = new TypeToken<Map<String, IndexableField>>() {
    }.getType();

    gson.registerTypeAdapter(fieldsType, new IndexableField.Deserializer());
    gson.registerTypeAdapter(configType, new DelegateConfigDeserializer());
    gson.registerTypeAdapter(DataRetriever.class, new DelegateDeserializer<DataRetriever>());
    gson.registerTypeAdapter(Parser.class, new DelegateDeserializer<Parser>());
    gson.registerTypeAdapter(Indexer.class, new DelegateDeserializer<Indexer>());

    configParser = gson.create();
}

From source file:org.jeeventstore.serialization.gson.EventListTypeConverter.java

License:Open Source License

@Override
public EventList deserialize(JsonElement json, Type type, JsonDeserializationContext context)
        throws JsonParseException {

    JsonObject obj = json.getAsJsonObject();
    Integer version = obj.getAsJsonPrimitive("version").getAsInt();
    if (version != 1)
        throw new JsonParseException("Unable to parse event of version " + version);

    Iterator<JsonElement> eit = obj.getAsJsonArray("events").iterator();
    List<Serializable> eventlist = new ArrayList<>();
    while (eit.hasNext()) {
        String clazz = null;/*from w w  w . ja  v  a 2s.c om*/
        try {
            JsonObject elem = eit.next().getAsJsonObject();
            clazz = elem.getAsJsonPrimitive("type").getAsString();
            Class<? extends Serializable> eventClass = (Class<? extends Serializable>) Class.forName(clazz);
            Serializable s = context.deserialize(elem.get("body"), eventClass);
            eventlist.add(s);
        } catch (ClassNotFoundException e) {
            throw new JsonParseException("Cannot deserialize events of class " + clazz, e);
        }
    }
    return new EventList(eventlist);
}

From source file:org.kie.workbench.common.forms.serialization.impl.FieldSerializer.java

License:Apache License

@Override
public FieldDefinition deserialize(JsonElement json, Type typeOfT, JsonDeserializationContext context)
        throws JsonParseException {

    JsonObject jsonField = json.getAsJsonObject();

    JsonElement jsonClassName = jsonField.get("serializedFieldClassName");

    if (jsonClassName != null && !StringUtils.isEmpty(jsonClassName.getAsString())) {
        try {/*ww w  .j ava2  s  . co  m*/
            return context.deserialize(json, Class.forName(jsonClassName.getAsString()));
        } catch (Exception ex) {
            log.error("Error deserializing field", ex);
        }
    }

    return null;
}

From source file:org.kie.workbench.common.forms.serialization.impl.FormModelSerializer.java

License:Apache License

@Override
public FormModel deserialize(JsonElement json, Type typeOfT, JsonDeserializationContext context)
        throws JsonParseException {

    JsonObject jsonField = json.getAsJsonObject();

    JsonElement jsonClassName = jsonField.get("formModelType");

    if (jsonClassName != null && !StringUtils.isEmpty(jsonClassName.getAsString())) {
        try {//from  ww w  .j a v a 2s.c  o m
            return context.deserialize(json, Class.forName(jsonClassName.getAsString()));
        } catch (Exception ex) {
            log.error("Error deserializing formModel", ex);
        }
    }

    return null;
}