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.birbit.jsonapi.JsonApiResourceDeserializer.java

License:Apache License

private void parseRelationships(JsonDeserializationContext context, T t, JsonObject jsonObject)
        throws IllegalAccessException, InvocationTargetException {
    JsonElement relationships = jsonObject.get("relationships");
    if (relationships != null && relationships.isJsonObject()) {
        JsonObject relationshipsObject = relationships.getAsJsonObject();
        for (Map.Entry<String, Setter> entry : relationshipSetters.entrySet()) {
            JsonElement relationship = relationshipsObject.get(entry.getKey());
            if (relationship != null && relationship.isJsonObject()) {
                if (entry.getValue().type() == JsonApiRelationshipList.class) {
                    entry.getValue().setOnObject(t,
                            context.deserialize(relationship, JsonApiRelationshipList.class));
                } else if (entry.getValue().type() == JsonApiRelationship.class) { // JsonApiRelationship
                    entry.getValue().setOnObject(t,
                            context.deserialize(relationship, JsonApiRelationship.class));
                } else { // String list or id
                    JsonElement data = relationship.getAsJsonObject().get("data");
                    if (data != null) {
                        if (data.isJsonObject()) {
                            JsonElement relationshipIdElement = data.getAsJsonObject().get("id");
                            if (relationshipIdElement != null) {
                                if (relationshipIdElement.isJsonPrimitive()) {
                                    entry.getValue().setOnObject(t, relationshipIdElement.getAsString());
                                }//from  w w  w  . jav a  2 s  .com
                            }
                        } else if (data.isJsonArray()) {
                            List<String> idList = parseIds(data.getAsJsonArray());
                            entry.getValue().setOnObject(t, idList);
                        }
                    }
                }
            }
        }
    }
}

From source file:com.birbit.jsonapi.JsonApiResourceDeserializer.java

License:Apache License

private T parseObject(JsonDeserializationContext context, String id, JsonObject jsonObject)
        throws InstantiationException, IllegalAccessException, InvocationTargetException {
    JsonElement attributesElement = jsonObject.get("attributes");
    T object;//from  ww  w . j av a 2 s . c om
    if (attributesElement != null && attributesElement.isJsonObject()) {
        object = context.deserialize(attributesElement, klass);
    } else {
        object = klass.newInstance();
    }
    idSetter.setOnObject(object, id);
    return object;
}

From source file:com.claresco.tinman.json.JsonUtility.java

License:Open Source License

/**
 * Description://w w w .  j av a2 s . co m
 *      Let some piece of element to be deserialized by another class
 *
 * Params:
 *
 */
protected static <T> T delegateDeserialization(JsonDeserializationContext theContext, JsonElement theElement,
        Type theType) {
    return theContext.deserialize(theElement, theType);
}

From source file:com.cloudant.client.api.Database.java

License:Open Source License

public List<Shard> deserialize(JsonElement json, Type typeOfT, JsonDeserializationContext context)
        throws JsonParseException {

    final List<Shard> shards = new ArrayList<Shard>();

    final JsonObject jsonObject = json.getAsJsonObject();
    Set<Map.Entry<String, JsonElement>> shardsObj = jsonObject.get("shards").getAsJsonObject().entrySet();

    for (Entry<String, JsonElement> entry : shardsObj) {
        String range = entry.getKey();
        List<String> nodeNames = context.deserialize(entry.getValue(), new TypeToken<List<String>>() {
        }.getType());/* w  ww  .  j ava 2 s . com*/
        shards.add(new Shard(range, nodeNames));
    }

    return shards;
}

From source file:com.cloudant.client.api.Database.java

License:Open Source License

public Map<String, EnumSet<Permissions>> deserialize(JsonElement json, Type typeOfT,
        JsonDeserializationContext context) throws JsonParseException {

    Map<String, EnumSet<Permissions>> perms = new HashMap<String, EnumSet<Permissions>>();
    JsonElement elem = json.getAsJsonObject().get("cloudant");
    if (elem == null) {
        return perms;
    }//from   w  w w .  j  a v  a 2  s  .  c  o m
    Set<Map.Entry<String, JsonElement>> permList = elem.getAsJsonObject().entrySet();
    for (Entry<String, JsonElement> entry : permList) {
        String user = entry.getKey();
        EnumSet<Permissions> p = context.deserialize(entry.getValue(), new TypeToken<EnumSet<Permissions>>() {
        }.getType());
        perms.put(user, p);
    }
    return perms;

}

From source file:com.cloudant.client.internal.util.SecurityDeserializer.java

License:Open Source License

public Map<String, EnumSet<Permissions>> deserialize(JsonElement json, Type typeOfT,
        JsonDeserializationContext context) throws JsonParseException {

    Map<String, EnumSet<Permissions>> perms = new HashMap<String, EnumSet<Permissions>>();
    JsonElement elem = json.getAsJsonObject().get("cloudant");
    if (elem == null) {
        return perms;
    }/*from   w ww. j  a  v  a 2  s.c o m*/
    Set<Map.Entry<String, JsonElement>> permList = elem.getAsJsonObject().entrySet();
    for (Map.Entry<String, JsonElement> entry : permList) {
        String user = entry.getKey();
        EnumSet<Permissions> p = context.deserialize(entry.getValue(), DeserializationTypes.PERMISSIONS);
        perms.put(user, p);
    }
    return perms;

}

From source file:com.cloudant.client.internal.util.ShardDeserializer.java

License:Open Source License

public List<Shard> deserialize(JsonElement json, Type typeOfT, JsonDeserializationContext context)
        throws JsonParseException {

    final List<Shard> shards = new ArrayList<Shard>();

    final JsonObject jsonObject = json.getAsJsonObject();
    Set<Map.Entry<String, JsonElement>> shardsObj = jsonObject.get("shards").getAsJsonObject().entrySet();

    for (Map.Entry<String, JsonElement> entry : shardsObj) {
        String range = entry.getKey();
        List<String> nodeNames = context.deserialize(entry.getValue(), DeserializationTypes.STRINGS);
        shards.add(new Shard(range, nodeNames));
    }/*from   w w  w.ja v  a  2s.  com*/

    return shards;
}

From source file:com.cloudbase.CBNaturalDeserializer.java

License:Open Source License

private Object handleArray(JsonArray json, JsonDeserializationContext context) {
    Object[] array = new Object[json.size()];
    for (int i = 0; i < array.length; i++)
        array[i] = context.deserialize(json.get(i), Object.class);
    return array;
}

From source file:com.cloudbase.CBNaturalDeserializer.java

License:Open Source License

private Object handleObject(JsonObject json, JsonDeserializationContext context) {
    Map<String, Object> map = new HashMap<String, Object>();
    for (Map.Entry<String, JsonElement> entry : json.entrySet())
        map.put(entry.getKey(), context.deserialize(entry.getValue(), Object.class));
    return map;/* w w  w. j av  a  2s  .  c om*/
}

From source file:com.contentful.java.cda.ResourceTypeAdapter.java

License:Apache License

/**
 * De-serialize a resource of type Space.
 *
 * @param jsonElement the resource in JSON form
 * @param context gson context//from  ww  w  .jav  a2s .  c o m
 * @param sys map of system attributes
 * @return {@code CDASpace} result object
 */
private CDASpace deserializeSpace(JsonElement jsonElement, JsonDeserializationContext context, JsonObject sys) {
    // Name
    String name = jsonElement.getAsJsonObject().get("name").getAsString();

    // Locales
    JsonArray localesArray = jsonElement.getAsJsonObject().get("locales").getAsJsonArray();
    Type t = new TypeToken<ArrayList<CDALocale>>() {
    }.getType();
    ArrayList<CDALocale> locales = context.deserialize(localesArray, t);

    // Default locale
    String defaultLocale = Constants.DEFAULT_LOCALE;
    for (CDALocale l : locales) {
        if (l.isDefault()) {
            defaultLocale = l.getCode();
            break;
        }
    }

    CDASpace result = new CDASpace(defaultLocale, locales, name);
    setBaseFields(result, sys, jsonElement, context);
    return result;
}