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:io.vertigo.vega.engines.webservice.json.UiObjectDeserializer.java

License:Apache License

/** {@inheritDoc} */
@Override//from  ww  w. ja va 2 s. co  m
public UiObject<D> deserialize(final JsonElement json, final Type typeOfT,
        final JsonDeserializationContext context) {
    final Type[] typeParameters = ((ParameterizedType) typeOfT).getActualTypeArguments();
    final Class<D> dtoClass = (Class<D>) typeParameters[0]; // Id has only one parameterized type T
    final JsonObject jsonObject = json.getAsJsonObject();
    final D inputDto = context.deserialize(jsonObject, dtoClass);
    final DtDefinition dtDefinition = DtObjectUtil.findDtDefinition(dtoClass);
    final Set<String> dtFields = getFieldNames(dtDefinition);
    final Set<String> modifiedFields = new HashSet<>();
    for (final Entry<String, JsonElement> entry : jsonObject.entrySet()) {
        final String fieldName = entry.getKey();
        if (dtFields.contains(fieldName)) { //we only keep fields of this dtObject
            modifiedFields.add(fieldName);
        }
    }
    //Send a alert if no fields match the DtObject ones : details may be a security issue ?
    if (modifiedFields.isEmpty()) {
        final Set<String> jsonEntry = new HashSet<>();
        for (final Entry<String, JsonElement> entry : jsonObject.entrySet()) {
            jsonEntry.add(entry.getKey());
        }
        throw new JsonSyntaxException(
                "Received Json's fields doesn't match " + dtoClass.getSimpleName() + " ones : " + jsonEntry);
    }
    final UiObject<D> uiObject = new VegaUiObject<>(inputDto, modifiedFields);
    if (jsonObject.has(JsonEngine.SERVER_SIDE_TOKEN_FIELDNAME)) {
        uiObject.setServerSideToken(jsonObject.get(JsonEngine.SERVER_SIDE_TOKEN_FIELDNAME).getAsString());
    }
    return uiObject;
}

From source file:it.geosolutions.android.map.geostore.model.GeoStoreAttributeTypeAdapter.java

License:Open Source License

@Override
public List<Attribute> deserialize(JsonElement json, java.lang.reflect.Type typeOfT,
        JsonDeserializationContext ctx) throws JsonParseException {
    List<Attribute> vals = new ArrayList<Attribute>();
    if (json.isJsonArray()) {
        for (JsonElement e : json.getAsJsonArray()) {
            vals.add((Attribute) ctx.deserialize(e, Attribute.class));
        }/*  ww w.  j  a v  a 2 s. c  om*/
    } else if (json.isJsonObject()) {
        vals.add((Attribute) ctx.deserialize(json, Attribute.class));
    } else {
        return vals;//throw new RuntimeException("Unexpected JSON type: " + json.getClass());
    }
    return vals;
}

From source file:it.geosolutions.android.map.geostore.model.GeoStoreResourceTypeAdapter.java

License:Open Source License

@Override
public List<Resource> deserialize(JsonElement json, java.lang.reflect.Type typeOfT,
        JsonDeserializationContext ctx) throws JsonParseException {
    List<Resource> vals = new ArrayList<Resource>();
    if (json.isJsonArray()) {
        for (JsonElement e : json.getAsJsonArray()) {
            vals.add((Resource) ctx.deserialize(e, Resource.class));
        }//from  w  ww  . jav  a2s .com
    } else if (json.isJsonObject()) {
        vals.add((Resource) ctx.deserialize(json, Resource.class));
    } else {
        return vals;//throw new RuntimeException("Unexpected JSON type: " + json.getClass());
    }
    return vals;
}

From source file:it.geosolutions.android.map.geostore.model.GeoStoreTypeAdapter.java

License:Open Source License

@SuppressWarnings("unchecked")
@Override/*from  w w w  . ja  v a2  s .c om*/
public List<T> deserialize(JsonElement json, java.lang.reflect.Type typeOfT, JsonDeserializationContext ctx)
        throws JsonParseException {
    List<T> vals = new ArrayList<T>();

    if (json.isJsonArray()) {
        for (JsonElement e : json.getAsJsonArray()) {
            vals.add((T) ctx.deserialize(e, this.type));
        }
    } else if (json.isJsonObject()) {
        vals.add((T) ctx.deserialize(json, type));
    } else if (json.isJsonPrimitive()) {
        //empty is a string
        return vals;
    } else {
        throw new RuntimeException("Unexpected JSON type: " + json.getClass());
    }
    return vals;
}

From source file:ivorius.ivtoolkit.models.loaders.data.ColorDeserializer.java

License:Apache License

@Override
public Color deserialize(JsonElement json, Type typeOfT, JsonDeserializationContext context)
        throws JsonParseException {
    float[] array = context.deserialize(json, float[].class);
    return new Color(array[0], array[1], array[2], array.length >= 4 ? array[3] : 1.0f);
}

From source file:ivorius.ivtoolkit.models.loaders.data.QuaternionDeserializer.java

License:Apache License

@Override
public Quaternion deserialize(JsonElement json, Type typeOfT, JsonDeserializationContext context)
        throws JsonParseException {
    float[] array = context.deserialize(json, float[].class);
    return new Quaternion(array[0], array[1], array[2], array[3]);
}

From source file:ivorius.ivtoolkit.models.loaders.data.Vector2fDeserializer.java

License:Apache License

@Override
public Vector2f deserialize(JsonElement json, Type typeOfT, JsonDeserializationContext context)
        throws JsonParseException {
    float[] array = context.deserialize(json, float[].class);
    return new Vector2f(array[0], array[1]);
}

From source file:ivorius.ivtoolkit.models.loaders.data.Vector3fDeserializer.java

License:Apache License

@Override
public Vector3f deserialize(JsonElement json, Type typeOfT, JsonDeserializationContext context)
        throws JsonParseException {
    float[] array = context.deserialize(json, float[].class);
    return new Vector3f(array[0], array[1], array[2]);
}

From source file:jcoderwall.gson.BadgeDeserializer.java

License:Open Source License

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

    return new Badge(jsonObject.get("name").getAsString(), jsonObject.get("description").getAsString(),
            (DateTime) context.deserialize(jsonObject.get("created"), DateTime.class),
            jsonObject.get("badge").getAsString());
}

From source file:jcoderwall.gson.BadgesDeserializer.java

License:Open Source License

/**
 * {@inheritDoc}//from w ww.  j av  a  2s  .c o  m
 */
@Override
public Badges deserialize(JsonElement json, Type typeOfT, JsonDeserializationContext context)
        throws JsonParseException {
    Badges badges = new Badges();
    if (json instanceof JsonArray) {
        for (JsonElement element : (JsonArray) json) {
            badges.add((Badge) context.deserialize(element, Badge.class));
        }
    }
    return badges;
}