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.terasology.utilities.gson.ResolutionHandler.java

License:Apache License

@Override
public Resolution deserialize(JsonElement json, Type typeOfT, JsonDeserializationContext context) {
    if (json.isJsonNull()) {
        return HeadlessResolution.getInstance();
    }// w  ww  .  ja va  2  s.  c om
    return new LwjglResolution(context.deserialize(json, DisplayMode.class));
}

From source file:org.terasology.world.block.loader.BlockLoader.java

License:Apache License

public static <T> void deserializeBlockPartMap(EnumMap<BlockPart, T> target, JsonObject jsonObj, Class<T> type,
        JsonDeserializationContext context) {
    if (jsonObj.has("all")) {
        T value = context.deserialize(jsonObj.get("all"), type);
        for (BlockPart part : BlockPart.values()) {
            target.put(part, value);//from w  w w  .j a  v  a2 s.  c  om
        }
    }
    if (jsonObj.has("sides")) {
        T value = context.deserialize(jsonObj.get("sides"), type);
        for (Side side : Side.horizontalSides()) {
            target.put(BlockPart.fromSide(side), value);
        }
    }
    if (jsonObj.has("topBottom")) {
        T value = context.deserialize(jsonObj.get("topBottom"), type);
        target.put(BlockPart.TOP, value);
        target.put(BlockPart.BOTTOM, value);
    }
    if (jsonObj.has("top")) {
        T value = context.deserialize(jsonObj.get("top"), type);
        target.put(BlockPart.TOP, value);
    }
    if (jsonObj.has("bottom")) {
        T value = context.deserialize(jsonObj.get("bottom"), type);
        target.put(BlockPart.BOTTOM, value);
    }
    if (jsonObj.has("front")) {
        T value = context.deserialize(jsonObj.get("front"), type);
        target.put(BlockPart.FRONT, value);
    }
    if (jsonObj.has("back")) {
        T value = context.deserialize(jsonObj.get("back"), type);
        target.put(BlockPart.BACK, value);
    }
    if (jsonObj.has("left")) {
        T value = context.deserialize(jsonObj.get("left"), type);
        target.put(BlockPart.LEFT, value);
    }
    if (jsonObj.has("right")) {
        T value = context.deserialize(jsonObj.get("right"), type);
        target.put(BlockPart.RIGHT, value);
    }
    if (jsonObj.has("center")) {
        T value = context.deserialize(jsonObj.get("center"), type);
        target.put(BlockPart.CENTER, value);
    }
}

From source file:org.tuleap.mylyn.task.core.internal.parser.AbstractProjectElementDeserializer.java

License:Open Source License

/**
 * {@inheritDoc}/*from  w w w .  j  a  v  a  2  s .  c o  m*/
 *
 * @see com.google.gson.JsonDeserializer#deserialize(com.google.gson.JsonElement, java.lang.reflect.Type,
 *      com.google.gson.JsonDeserializationContext)
 */
@Override
public T deserialize(JsonElement rootJsonElement, Type type, JsonDeserializationContext context)
        throws JsonParseException {

    JsonObject jsonObject = rootJsonElement.getAsJsonObject();

    String id = jsonObject.get(ITuleapConstants.ID).getAsString();
    T pojo = getPrototype(id);

    JsonElement jsonElement = jsonObject.get(ITuleapConstants.LABEL);
    if (jsonElement != null && !jsonElement.isJsonNull()) {
        pojo.setLabel(jsonElement.getAsString());
    }
    TuleapReference project = context.deserialize(jsonObject.get(ITuleapConstants.JSON_PROJECT),
            TuleapReference.class);
    JsonElement element = jsonObject.get(ITuleapConstants.URI);
    if (element != null && !element.isJsonNull()) {
        pojo.setUri(element.getAsString());
    }

    pojo.setProject(project);
    return pojo;
}

From source file:org.tuleap.mylyn.task.core.internal.parser.AbstractTuleapDeserializer.java

License:Open Source License

/**
 * {@inheritDoc}//  w  ww .  j  a v  a  2  s . c om
 *
 * @see com.google.gson.JsonDeserializer#deserialize(com.google.gson.JsonElement, java.lang.reflect.Type,
 *      com.google.gson.JsonDeserializationContext)
 */
@Override
public T deserialize(JsonElement rootJsonElement, Type type, JsonDeserializationContext context)
        throws JsonParseException {
    JsonObject jsonObject = rootJsonElement.getAsJsonObject();
    T pojo = super.deserialize(rootJsonElement, type, context);
    JsonElement jsonTrackerRef = jsonObject.get(ITuleapConstants.JSON_TRACKER);
    TuleapReference trackerRef = context.deserialize(jsonTrackerRef, TuleapReference.class);
    pojo.setTracker(trackerRef);

    JsonArray fields = jsonObject.get(ITuleapConstants.VALUES).getAsJsonArray();
    for (JsonElement field : fields) {
        JsonObject jsonField = field.getAsJsonObject();
        int fieldId = jsonField.get(ITuleapConstants.FIELD_ID).getAsInt();
        if (jsonField.has(ITuleapConstants.FIELD_VALUE)) {
            JsonElement jsonValue = jsonField.get(ITuleapConstants.FIELD_VALUE);
            if (jsonValue.isJsonPrimitive()) {
                JsonPrimitive primitive = jsonValue.getAsJsonPrimitive();
                if (primitive.isString()) {
                    pojo.addFieldValue(new LiteralFieldValue(fieldId, primitive.getAsString()));
                } else if (primitive.isNumber()) {
                    pojo.addFieldValue(new LiteralFieldValue(fieldId, String.valueOf(primitive.getAsNumber())));
                } else if (primitive.isBoolean()) {
                    pojo.addFieldValue(
                            new LiteralFieldValue(fieldId, String.valueOf(primitive.getAsBoolean())));
                }
            }
        } else if (jsonField.has(ITuleapConstants.FIELD_BIND_VALUE_ID)
                && !jsonField.get(ITuleapConstants.FIELD_BIND_VALUE_ID).isJsonNull()) {
            // sb?
            JsonElement jsonBindValueId = jsonField.get(ITuleapConstants.FIELD_BIND_VALUE_ID);
            int bindValueId = jsonBindValueId.getAsInt();
            pojo.addFieldValue(new BoundFieldValue(fieldId, Lists.newArrayList(Integer.valueOf(bindValueId))));
        } else if (jsonField.has(ITuleapConstants.FIELD_BIND_VALUE_IDS)
                && !jsonField.get(ITuleapConstants.FIELD_BIND_VALUE_IDS).isJsonNull()) {
            // sb?, msb, cb, or tbl (open list)
            JsonElement jsonBindValueIds = jsonField.get(ITuleapConstants.FIELD_BIND_VALUE_IDS);
            JsonArray jsonIds = jsonBindValueIds.getAsJsonArray();
            if (jsonIds.size() > 0) {
                JsonPrimitive firstElement = jsonIds.get(0).getAsJsonPrimitive();
                if (firstElement.isString()) {
                    // Open list (tbl)
                    List<String> bindValueIds = new ArrayList<String>();
                    for (JsonElement idElement : jsonIds) {
                        bindValueIds.add(idElement.getAsString());
                    }
                    pojo.addFieldValue(new OpenListFieldValue(fieldId, bindValueIds));
                } else {
                    List<Integer> bindValueIds = new ArrayList<Integer>();
                    for (JsonElement idElement : jsonIds) {
                        bindValueIds.add(Integer.valueOf(idElement.getAsInt()));
                    }
                    pojo.addFieldValue(new BoundFieldValue(fieldId, bindValueIds));
                }
            } else {
                pojo.addFieldValue(new BoundFieldValue(fieldId, Collections.<Integer>emptyList()));
            }
        } else if (jsonField.has(ITuleapConstants.FIELD_LINKS)
                && !jsonField.get(ITuleapConstants.FIELD_LINKS).isJsonNull()) {
            // Artifact links
            pojo.addFieldValue(
                    context.<ArtifactLinkFieldValue>deserialize(jsonField, ArtifactLinkFieldValue.class));
        } else if (jsonField.has(ITuleapConstants.FILE_DESCRIPTIONS)
                && jsonField.get(ITuleapConstants.FILE_DESCRIPTIONS).isJsonArray()) {
            AttachmentFieldValue value = context.deserialize(jsonField, AttachmentFieldValue.class);
            pojo.addFieldValue(value);
        }

    }

    return pojo;
}

From source file:org.tuleap.mylyn.task.core.internal.parser.AttachmentFieldValueAdapter.java

License:Open Source License

/**
 * {@inheritDoc}//from   w  w  w.jav  a  2  s.  c  om
 *
 * @see com.google.gson.JsonDeserializer#deserialize(com.google.gson.JsonElement, java.lang.reflect.Type,
 *      com.google.gson.JsonDeserializationContext)
 */
@Override
public AttachmentFieldValue deserialize(JsonElement element, Type type, JsonDeserializationContext context)
        throws JsonParseException {
    JsonObject jsonObject = element.getAsJsonObject();
    int fieldId = jsonObject.get("field_id").getAsInt(); //$NON-NLS-1$
    List<AttachmentValue> fileDescriptions = new ArrayList<AttachmentValue>();
    for (JsonElement fileDescElt : jsonObject.get("file_descriptions").getAsJsonArray()) { //$NON-NLS-1$
        fileDescriptions.add((AttachmentValue) context.deserialize(fileDescElt, AttachmentValue.class));
    }
    return new AttachmentFieldValue(fieldId, fileDescriptions);
}

From source file:org.tuleap.mylyn.task.core.internal.parser.TuleapTrackerDeserializer.java

License:Open Source License

/**
 * Sets the workflow and the transition for the select box.
 *
 * @param jsonObject//from   ww  w .j  a  va 2 s  .co m
 *            the JSON root object
 * @param selectBoxField
 *            the select box field
 * @param context
 *            the deserialization context
 */
private void fillWorkflow(JsonObject jsonObject, TuleapSelectBox selectBoxField,
        JsonDeserializationContext context) {
    JsonElement workflowJsonElement = jsonObject.get(ITuleapTrackerConstants.WORKFLOW);
    if (workflowJsonElement != null && !workflowJsonElement.isJsonNull()) {
        JsonObject workflowJsonObject = workflowJsonElement.getAsJsonObject();

        if (workflowJsonObject.get(FIELD_ID).getAsInt() == selectBoxField.getIdentifier()) {
            // the workflow transitions
            JsonArray transitionsJsonArray = workflowJsonObject.get(TRANSITIONS).getAsJsonArray();
            for (JsonElement transitionElement : transitionsJsonArray) {
                TuleapWorkflowTransition workflowTransition = context.deserialize(transitionElement,
                        TuleapWorkflowTransition.class);
                selectBoxField.getWorkflow().addTransition(workflowTransition);
            }
        }
    }
}

From source file:org.uncertml.io.json.IUncertaintyDeserializer.java

License:Apache License

@Override
public IUncertainty deserialize(JsonElement element, Type type, JsonDeserializationContext context)
        throws JsonParseException {
    JsonObject json = element.getAsJsonObject();
    String uncertaintyType = json.entrySet().iterator().next().getKey();
    if (uncertaintyType.equals("StatisticCollection")) {
        // special case
        StatisticCollection collection = new StatisticCollection();
        JsonArray members = json.get("StatisticCollection").getAsJsonObject().get("members").getAsJsonArray();
        for (JsonElement member : members) {
            collection.add((IStatistic) context.deserialize(member, IUncertainty.class));
        }/*from w ww.ja va 2  s.  c  o m*/
        return collection;
    } else if (uncertaintyType.equals("MixtureModel")) {
        // another special case
        ArrayList<WeightedDistribution> distributions = new ArrayList<WeightedDistribution>();
        JsonArray components = json.get("MixtureModel").getAsJsonObject().get("components").getAsJsonArray();
        for (JsonElement component : components) {
            double weight = component.getAsJsonObject().get("weight").getAsDouble();
            IDistribution distribution = (IDistribution) context
                    .deserialize(component.getAsJsonObject().get("distribution"), IUncertainty.class);
            distributions.add(new WeightedDistribution(weight, distribution));
        }
        return new MixtureModel(distributions);
    } else if (uncertaintyType.equals("Realisation")) {
        // another special case
        JsonObject realisationObj = json.get("Realisation").getAsJsonObject();

        // get values/categories
        AbstractRealisation realisation;
        if (realisationObj.has("categories")) {
            List<String> categories = context.deserialize(realisationObj.get("categories"),
                    new TypeToken<List<String>>() {
                    }.getType());
            String id = null;
            Double weight = Double.NaN;
            if (realisationObj.has("id")) {
                id = realisationObj.get("id").getAsString();
            }
            if (realisationObj.has("weight")) {
                weight = realisationObj.get("weight").getAsDouble();
            }
            realisation = new CategoricalRealisation(categories, weight, id);
        } else {
            List<Double> values = context.deserialize(realisationObj.get("values"),
                    new TypeToken<List<Double>>() {
                    }.getType());
            // TODO: fix repeated code
            String id = null;
            Double weight = Double.NaN;
            if (realisationObj.has("id")) {
                id = realisationObj.get("id").getAsString();
            }
            if (realisationObj.has("weight")) {
                weight = realisationObj.get("weight").getAsDouble();
            }
            realisation = new ContinuousRealisation(values, weight, id);
        }

        return realisation;
    } else {
        Class<?> uncertaintyClass = getUncertaintyClass(uncertaintyType);
        if (uncertaintyClass != null) {
            return context.deserialize(json.get(uncertaintyType), uncertaintyClass);
        }
    }
    return null;
}

From source file:org.wso2.carbon.siddhi.editor.core.util.designview.deserializers.types.AggregateByTimePeriodDeSerializer.java

License:Open Source License

@Override
public Object deserialize(JsonElement jsonElement, Type type,
        JsonDeserializationContext jsonDeserializationContext) {
    JsonObject jsonObject = jsonElement.getAsJsonObject();
    JsonPrimitive jsonPrimitive = (JsonPrimitive) jsonObject.get(TYPE);
    String attributesSelectionType = jsonPrimitive.getAsString();
    if (attributesSelectionType.equalsIgnoreCase(AggregationByTimeType.RANGE.toString())) {
        return jsonDeserializationContext.deserialize(jsonObject, AggregateByTimeRange.class);
    } else if (attributesSelectionType.equalsIgnoreCase(AggregationByTimeType.INTERVAL.toString())) {
        return jsonDeserializationContext.deserialize(jsonObject, AggregateByTimeInterval.class);
    }//from ww  w  .j  a va2s .  com
    throw new JsonParseException(
            "Unable to de-serialize the AggregateByTimePeriod JSON since its type is unknown");
}

From source file:org.wso2.carbon.siddhi.editor.core.util.designview.deserializers.types.AttributesSelectionConfigDeSerializer.java

License:Open Source License

@Override
public Object deserialize(JsonElement jsonElement, Type type,
        JsonDeserializationContext jsonDeserializationContext) {
    JsonObject jsonObject = jsonElement.getAsJsonObject();
    JsonPrimitive jsonPrimitive = (JsonPrimitive) jsonObject.get(TYPE);
    String attributesSelectionType = jsonPrimitive.getAsString();
    if (attributesSelectionType.equalsIgnoreCase(AttributeSelection.TYPE_USER_DEFINED)) {
        return jsonDeserializationContext.deserialize(jsonObject, UserDefinedSelectionConfig.class);
    } else if (attributesSelectionType.equalsIgnoreCase(AttributeSelection.TYPE_ALL)) {
        return jsonDeserializationContext.deserialize(jsonObject, AllSelectionConfig.class);
    }//from   w  w  w. j  ava2  s. co  m
    throw new JsonParseException(
            "Unable to de-serialize the AttributesSelectionConfig JSON since its type is unknown");
}

From source file:org.wso2.carbon.siddhi.editor.core.util.designview.deserializers.types.MapperPayloadOrAttributeDeserializer.java

License:Open Source License

@Override
public Object deserialize(JsonElement jsonElement, Type type,
        JsonDeserializationContext jsonDeserializationContext) {
    JsonObject jsonObject = jsonElement.getAsJsonObject();
    JsonPrimitive jsonPrimitive = (JsonPrimitive) jsonObject.get(TYPE);
    String mapperAttributeType = jsonPrimitive.getAsString();
    if (mapperAttributeType.equalsIgnoreCase(MapperPayloadOrAttributeType.LIST.toString())) {
        return jsonDeserializationContext.deserialize(jsonObject, MapperListPayloadOrAttribute.class);
    } else if (mapperAttributeType.equalsIgnoreCase(MapperPayloadOrAttributeType.MAP.toString())) {
        return jsonDeserializationContext.deserialize(jsonObject, MapperMapPayloadOrAttribute.class);
    }//  www.  jav a2s .c  o m
    throw new JsonParseException(
            "Unable to de-serialize the Mapper Attribute/Payload JSON since its type is unknown");
}