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:allout58.mods.techtree.tree.TechNodeGSON.java

License:Open Source License

@Override
public TechNode deserialize(JsonElement json, Type typeOfT, JsonDeserializationContext context)
        throws JsonParseException {
    final JsonObject obj = json.getAsJsonObject();
    final int id = obj.get("id").getAsInt();
    final String name = obj.get("name").getAsString();
    final int science = obj.get("scienceRequired").getAsInt();
    final String description = obj.get("description").getAsString();
    final ItemStack[] items = context.deserialize(obj.get("lockedItems"), ItemStack[].class);

    final TechNode node = new TechNode(id);
    //        ItemStack[] items = new ItemStack[] { new ItemStack(Items.apple), new ItemStack(Items.arrow), new ItemStack(Items.bow) };
    node.setup(name, science, description, items);

    final JsonArray jsonParentArray = obj.getAsJsonArray("parents");
    for (int i = 0; i < jsonParentArray.size(); i++) {
        node.addParentNode(jsonParentArray.get(i).getAsInt());
    }/*from   ww  w. j  ava 2  s  .  c o  m*/

    return node;
}

From source file:at.ac.tuwien.big.we14.lab2.api.impl.JSONQuestionDataProvider.java

License:Open Source License

@Override
public Category deserialize(JsonElement json, Type type, JsonDeserializationContext context)
        throws JsonParseException {
    Category category = factory.createCategory();
    JsonObject object = json.getAsJsonObject();

    category.setName(object.get("name").getAsString());

    for (JsonElement jsonquestion : object.get("questions").getAsJsonArray()) {
        Question question = context.deserialize(jsonquestion, new TypeToken<Question>() {
        }.getType());/* w w w.  j av a  2s . c o m*/
        category.addQuestion(question);
    }

    return category;
}

From source file:at.ac.tuwien.big.we14.lab2.api.impl.JSONQuestionDataProvider.java

License:Open Source License

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

    Question question = factory.createQuestion();

    JsonObject object = json.getAsJsonObject();
    question.setId(object.get("id").getAsInt());
    question.setText(object.get("text").getAsString());

    for (JsonElement wrongChoice : object.get("wrongChoices").getAsJsonArray()) {
        SimpleChoice choice = context.deserialize(wrongChoice, new TypeToken<SimpleChoice>() {
        }.getType());/* w  w  w  .  ja v a 2s .  c  o m*/
        choice.setId(lastChoiceId++);
        question.addChoice(choice, false);
    }

    question.setMaxTime(object.get("maxTime").getAsLong());

    for (JsonElement correctChoice : object.get("correctChoices").getAsJsonArray()) {
        SimpleChoice choice = context.deserialize(correctChoice, new TypeToken<SimpleChoice>() {
        }.getType());
        choice.setId(lastChoiceId++);
        question.addChoice(choice, true);
    }

    return question;
}

From source file:at.maui.cheapcast.json.deserializer.ProtocolMessageDeserializer.java

License:Apache License

@Override
public ProtocolMessage deserialize(JsonElement jsonElement, Type type,
        JsonDeserializationContext jsonDeserializationContext) throws JsonParseException {
    ProtocolMessage pm;/*from  w  w  w.j  ava 2 s .c  o m*/

    if (jsonElement.isJsonArray()) {
        JsonArray arr = jsonElement.getAsJsonArray();

        pm = new ProtocolMessage();
        pm.setProtocol(arr.get(0).getAsString());

        if (pm.getProtocol().equals("ramp")) {
            pm.setPayload((RampMessage) jsonDeserializationContext.deserialize(arr.get(1), RampMessage.class));
        } else if (pm.getProtocol().equals("cm")) {
            pm.setPayload((CmMessage) jsonDeserializationContext.deserialize(arr.get(1), CmMessage.class));
        }

        return pm;
    }

    return null; //To change body of implemented methods use File | Settings | File Templates.
}

From source file:at.maui.cheapcast.json.deserializer.RampMessageDeserializer.java

License:Apache License

@Override
public RampMessage deserialize(JsonElement jsonElement, Type type,
        JsonDeserializationContext jsonDeserializationContext) throws JsonParseException {
    if (jsonElement.isJsonObject()) {
        JsonObject obj = jsonElement.getAsJsonObject();
        String t = obj.getAsJsonPrimitive("type").getAsString();

        if (t.equals("VOLUME")) {
            return jsonDeserializationContext.deserialize(jsonElement, RampVolume.class);
        } else if (t.equals("STATUS") || t.equals("RESPONSE")) {
            return jsonDeserializationContext.deserialize(jsonElement, RampStatus.class);
        } else if (t.equals("PLAY")) {
            return jsonDeserializationContext.deserialize(jsonElement, RampPlay.class);
        } else if (t.equals("STOP")) {
            return jsonDeserializationContext.deserialize(jsonElement, RampStop.class);
        } else if (t.equals("LOAD")) {
            return jsonDeserializationContext.deserialize(jsonElement, RampLoad.class);
        } else if (t.equals("INFO")) {
            return jsonDeserializationContext.deserialize(jsonElement, RampInfo.class);
        }/*from  w  w  w  .  j  a  v  a 2  s .c  o  m*/
        Log.w(LOG_TAG, "Ramp message not handle : " + t);
    }

    return null;
}

From source file:be.ugent.tiwi.sleroux.newsrec.recommendationstester.CustomDeserializer.java

@Override
public NewsItemCluster deserialize(JsonElement json, Type typeOfT, JsonDeserializationContext context)
        throws JsonParseException {
    JsonObject jo = json.getAsJsonObject();
    RecommendedNewsItem[] items = context.deserialize(jo.get("items"), RecommendedNewsItem[].class);

    RecommendedNewsItem rep = context.deserialize(jo.get("representative"), RecommendedNewsItem.class);
    NewsItemCluster cluster = new NewsItemCluster();
    cluster.setItems(Arrays.asList(items));
    cluster.setRepresentative(rep);//  w  w w.  j  a  va  2s  .c o  m
    return cluster;
}

From source file:ca.ualberta.CMPUT301W15T06.GsonAdapter.java

License:Apache License

@Override
public T deserialize(JsonElement json, Type type, JsonDeserializationContext context)
        throws JsonParseException {
    // TODO Auto-generated method stub
    JsonObject jsonObject = json.getAsJsonObject();
    JsonPrimitive prim = (JsonPrimitive) jsonObject.get(CLASSNAME);
    String className = prim.getAsString();

    Class<?> klass = null;/*from   www .  java  2 s. co m*/
    try {
        klass = Class.forName(className);
    } catch (ClassNotFoundException e) {
        e.printStackTrace();
        throw new JsonParseException(e.getMessage());
    }
    return context.deserialize(jsonObject.get(INSTANCE), klass);
}

From source file:cc.alessandro.jpocket.gson.StatusesDeserializer.java

License:Open Source License

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

    Statuses statuses = new Statuses();

    if (json instanceof JsonArray) {
        for (JsonElement element : (JsonArray) json) {
            statuses.add((Status) context.deserialize(element, Status.class));
        }//from  ww  w  .  j av a 2 s. c o  m
    }
    return statuses;
}

From source file:cc.kave.commons.utils.json.legacy.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 map = context.deserialize(json, createMapType(typeOfT));
    for (final Object key : map.keySet()) {
        final Collection values = (Collection) map.get(key);
        multimap.putAll(key, values);/*w  w w.ja  v  a 2  s .c  om*/
    }

    return multimap;
}

From source file:ch.ethz.inf.vs.hypermedia.corehal.FormListDeserializer.java

License:Open Source License

@Override
public FormList deserialize(JsonElement json, Type typeOfT, JsonDeserializationContext context)
        throws JsonParseException {
    FormList list = new FormList();
    if (json.isJsonArray()) {
        for (JsonElement el : json.getAsJsonArray()) {
            list.add(context.deserialize(el, Form.class));
        }//from  w ww. j av a  2 s. co  m
    } else {
        list.add(context.deserialize(json, Form.class));
    }
    return list;
}