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.lanternpowered.server.util.option.SimpleOptionValueMapJsonSerializer.java

License:MIT License

@Override
public SimpleOptionValueMap deserialize(JsonElement element, Type typeOfT, JsonDeserializationContext context)
        throws JsonParseException {
    final JsonObject json = element.getAsJsonObject();
    //noinspection unchecked
    final OptionValueMap map = new SimpleOptionValueMap();
    for (Map.Entry<String, JsonElement> entry : json.entrySet()) {
        final Optional<Option<?>> option = Option.get(entry.getKey());
        if (option.isPresent()) {
            map.put(option.get(), context.deserialize(entry.getValue(), option.get().getTypeToken().getType()));
        } else {//w  w w . j av a2s.c o m
            Lantern.getLogger().warn("Attempted to add a value for a non existing option: {}", entry.getKey());
        }
    }
    return (SimpleOptionValueMap) map;
}

From source file:org.lanternpowered.server.util.option.UnmodifiableOptionValueMapJsonSerializer.java

License:MIT License

@Override
public UnmodifiableOptionValueMap deserialize(JsonElement json, Type typeOfT,
        JsonDeserializationContext context) throws JsonParseException {
    return new UnmodifiableOptionValueMap(context.deserialize(json, SimpleOptionValueMap.class));
}

From source file:org.lanternpowered.server.world.weather.WeatherBuilderJsonSerializer.java

License:MIT License

@Override
public WeatherBuilder deserialize(JsonElement element, Type typeOfT, JsonDeserializationContext context)
        throws JsonParseException {
    final JsonObject json = element.getAsJsonObject();
    final Set<String> aliases;
    if (json.has("aliases")) {
        aliases = ImmutableSet.copyOf((String[]) context.deserialize(json.get("aliases"), String[].class));
    } else {/*ww w .  j a  v a  2s. c o  m*/
        aliases = ImmutableSet.of();
    }
    final OptionValueMap options;
    if (json.has("options")) {
        options = context.deserialize(json.get("options"), UnmodifiableOptionValueMap.class);
    } else {
        options = new UnmodifiableOptionValueMap(new SimpleOptionValueMap());
    }
    final Action action = context.deserialize(json.has("action") ? json.get("action") : JsonNull.INSTANCE,
            Action.class);
    final WeatherBuilder builder = LanternWeather.builder().action(action).aliases(aliases).options(options);
    if (json.has("name")) {
        builder.name(json.get("name").getAsString());
    }
    if (json.has("weight")) {
        builder.weight(json.get("weight").getAsDouble());
    }
    if (json.has("duration")) {
        builder.duration(context.deserialize(json.get("duration"), IntValueProvider.class));
    }
    return builder;
}

From source file:org.livespark.formmodeler.codegen.template.impl.serialization.FieldDeserializer.java

License:Apache License

@Override
public FieldDefinition deserialize(JsonElement json, Type typeOfT, JsonDeserializationContext context)
        throws JsonParseException {
    JsonArray ja = json.getAsJsonArray();

    for (JsonElement je : ja) {

        String typeCode = je.getAsJsonObject().get("code").getAsString();

        FieldDefinition definition = fieldManager.getDefinitionByTypeCode(typeCode);

        return context.deserialize(je, definition.getClass());
    }//  w ww.j  av a  2s . c om

    return null;
}

From source file:org.matrix.androidsdk.rest.json.ConditionDeserializer.java

License:Apache License

@Override
public Condition deserialize(JsonElement json, Type typeOfT, JsonDeserializationContext context)
        throws JsonParseException {
    JsonObject jsonObject = json.getAsJsonObject();
    JsonElement kindElement = jsonObject.get("kind");
    if (kindElement != null) {
        String kind = kindElement.getAsString();

        if (Condition.KIND_EVENT_MATCH.equals(kind)) {
            return context.deserialize(json, EventMatchCondition.class);
        }//from   ww  w  .j  a v  a 2  s  . c  o m
        if (Condition.KIND_DEVICE.equals(kind)) {
            return context.deserialize(json, DeviceCondition.class);
        }
        if (Condition.KIND_CONTAINS_DISPLAY_NAME.equals(kind)) {
            return context.deserialize(json, ContainsDisplayNameCondition.class);
        }
        if (Condition.KIND_ROOM_MEMBER_COUNT.equals(kind)) {
            return context.deserialize(json, RoomMemberCountCondition.class);
        }
    }
    return null;
}

From source file:org.mgenterprises.openbooks.saving.AbstractSaveableAdapter.java

License:Open Source License

@Override
public Saveable deserialize(JsonElement json, Type typeOfT, JsonDeserializationContext context)
        throws JsonParseException {
    JsonObject jsonObject = json.getAsJsonObject();
    String type = jsonObject.get("type").getAsString();
    JsonElement element = jsonObject.get("properties");

    try {/*from   w  ww.  j a v a2  s .  co m*/
        return context.deserialize(element, Class.forName(type));
    } catch (ClassNotFoundException cnfe) {
        throw new JsonParseException("Unknown element type: " + type, cnfe);
    }
}

From source file:org.myeslib.jdbi.storage.helpers.gson.RuntimeTypeAdapter.java

License:Apache License

public T deserialize(JsonElement json, Type typeOfT, JsonDeserializationContext context)
        throws JsonParseException {
    JsonElement labelJsonElement = json.getAsJsonObject().remove(typeFieldName);
    if (labelJsonElement == null) {
        throw new JsonParseException(
                "cannot deserialize " + typeOfT + " because it does not define a field named " + typeFieldName);
    }//from   w  ww  .j av  a  2  s  . c  o  m
    String label = labelJsonElement.getAsString();
    Class<?> subtype = labelToSubtype.get(label);
    if (subtype == null) {
        throw new JsonParseException("cannot deserialize " + baseType + " subtype named " + label
                + "; did you forget to register a subtype?");
    }
    @SuppressWarnings("unchecked") // registration requires that subtype extends T
    T result = (T) context.deserialize(json, subtype);
    return result;
}

From source file:org.n52.matlab.connector.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();
    long id = json.get(MatlabJSONConstants.ID).getAsLong();
    MatlabRequest request = new MatlabRequest(id, function);

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

    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:org.n52.matlab.connector.json.MatlabResultSerializer.java

License:Open Source License

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

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

From source file:org.openhab.binding.loxone.internal.types.LxConfig.java

License:Open Source License

public static <T> T deserializeObject(JsonObject parent, String name, Type type,
        JsonDeserializationContext context) {
    JsonElement element = parent.get(name);
    if (element != null) {
        return context.deserialize(element, type);
    }/*from  w  ww.  ja v a  2 s  .  c  om*/
    return null;
}