Example usage for com.google.gson JsonObject get

List of usage examples for com.google.gson JsonObject get

Introduction

In this page you can find the example usage for com.google.gson JsonObject get.

Prototype

public JsonElement get(String memberName) 

Source Link

Document

Returns the member with the specified name.

Usage

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());//from w w  w  .  j a va  2 s .  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  .co 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.pcgamingfreaks.Message.MessageComponent.java

License:Open Source License

@Override
public T deserialize(JsonElement json, java.lang.reflect.Type type, JsonDeserializationContext context)
        throws JsonParseException {
    JsonObject componentAsJsonObject = json.getAsJsonObject();
    T component;//www . j  a  va2s.c om
    try {
        component = (T) messageComponentConstructor.newInstance();
    } catch (Exception e) {
        e.printStackTrace();
        return null;
    }
    //Strings
    if (componentAsJsonObject.get("text") != null)
        component.text = componentAsJsonObject.get("text").getAsString();
    if (componentAsJsonObject.get("color") != null)
        component.color = MessageColor.valueOf(componentAsJsonObject.get("color").getAsString());
    if (componentAsJsonObject.get("insertion") != null)
        component.insertion = componentAsJsonObject.get("insertion").getAsString();
    //Booleans
    if (componentAsJsonObject.get("bold") != null)
        component.bold = componentAsJsonObject.get("bold").getAsBoolean();
    if (componentAsJsonObject.get("italic") != null)
        component.italic = componentAsJsonObject.get("italic").getAsBoolean();
    if (componentAsJsonObject.get("underlined") != null)
        component.underlined = componentAsJsonObject.get("underlined").getAsBoolean();
    if (componentAsJsonObject.get("obfuscated") != null)
        component.obfuscated = componentAsJsonObject.get("obfuscated").getAsBoolean();
    if (componentAsJsonObject.get("strikethrough") != null)
        component.strikethrough = componentAsJsonObject.get("strikethrough").getAsBoolean();
    //Extra List
    if (componentAsJsonObject.get("extra") != null)
        component.extra = fromJsonArrayWorker(componentAsJsonObject.get("extra").getAsJsonArray());
    //Events
    if (componentAsJsonObject.get("clickEvent") != null)
        component.clickEvent = GSON.fromJson(componentAsJsonObject.get("clickEvent"), MessageClickEvent.class);
    if (componentAsJsonObject.get("hoverEvent") == null)
        component.hoverEvent = GSON.fromJson(componentAsJsonObject.get("hoverEvent"), MessageHoverEvent.class);
    //Other stuff
    component.selector = componentAsJsonObject.get("with");
    component.selector = componentAsJsonObject.get("score");
    component.selector = componentAsJsonObject.get("selector");
    component.selector = componentAsJsonObject.get("translate");
    return component;
}

From source file:at.storm.bolt.FeatureGenerationBolt.java

License:Apache License

@Override
public void execute(Tuple tuple, BasicOutputCollector collector) {
    POSTaggerBoltData posTaggerValue = POSTaggerBoltData.getFromTuple(tuple);
    JsonObject jsonObject = posTaggerValue.getJsonObject();
    TupleStatistic tupleStatistic = posTaggerValue.getTupleStatistic();
    List<TaggedToken> taggedTokens = posTaggerValue.getTaggedTokens();

    // Generate Feature Vector
    Map<Integer, Double> featureVector = m_fvg.generateFeatureVector(taggedTokens);

    if (m_logging) {
        LOG.info("Tweet: " + jsonObject.get("msg").getAsString() + " FeatureVector: " + featureVector);
    }//from   www .  ja va2  s. com

    // Emit new tuples
    collector.emit(new FeatureGenerationBoltData(jsonObject, tupleStatistic, featureVector));
}

From source file:at.storm.bolt.JsonBolt.java

License:Apache License

public void execute(Tuple tuple, BasicOutputCollector collector) {
    String jsonString = tuple.getStringByField("jsonString");
    TupleStatistic tupleStatistic = (TupleStatistic) tuple.getValueByField("tupleStatistic");

    JsonObject jsonObject = (JsonObject) jsonParser.parse(jsonString);

    if (m_logging) {
        LOG.info("JSON: " + jsonObject.toString());
    }/*from w ww  .  j  a v a2s  . c  om*/

    tupleStatistic.setRealStart(jsonObject.get("timestamp").getAsLong());

    String message = jsonObject.get("msg").getAsString();

    if (!LanguageDetection.isEnglish(message)) {
        JsonObject score = new JsonObject();
        score.addProperty("score", 0);

        jsonObject.add("sentiment", score);

        collector.emit(TO_REDIS_PUBLISH_STREAM, new JsonBoltData(jsonObject, tupleStatistic));
    } else {
        collector.emit(PIPELINE_STREAM, new JsonBoltData(jsonObject, tupleStatistic));
    }

}

From source file:at.storm.bolt.TokenizerBolt.java

License:Apache License

@Override
public void execute(Tuple tuple, BasicOutputCollector collector) {
    //      asdJsonBoltData jsonBoltValue = JsonBoltData.getFromTuple(tuple);
    //      JsonObject jsonObject = jsonBoltValue.getJsonObject();
    //      TupleStatistic tupleStatistic = jsonBoltValue.getTupleStatistic();
    //      String msg = jsonObject.get("msg").getAsString();

    RedisSpoutData redisSpoutValue = RedisSpoutData.getFromTuple(tuple);
    JsonObject jsonObject = redisSpoutValue.getJsonObject();
    TupleStatistic tupleStatistic = redisSpoutValue.getTupleStatistic();
    String msg = jsonObject.get("msg").getAsString();

    List<String> tokens = Tokenizer.tokenize(msg);

    if (m_logging) {
        LOG.info("Tweet: \"" + msg + "\" Tokenized: " + tokens + "  json: " + jsonObject.toString());
    }/*from   w w  w. jav a2s  .  com*/

    // Emit new tuples
    collector.emit(new TokenizerBoltData(jsonObject, tupleStatistic, tokens));
}

From source file:at.tugraz.kmi.medokyservice.fca.util.ImportExport.java

License:Open Source License

private Map<Long, User> json2Users(JsonObject json) {
    HashMap<Long, User> users = new HashMap<Long, User>();
    JsonObject block = json.get(SECTION_U).getAsJsonObject();

    Set<Entry<String, JsonElement>> entries = block.entrySet();
    for (Entry<String, JsonElement> u : entries) {
        JsonObject jsUser = u.getValue().getAsJsonObject();
        System.out.println(jsUser.toString());
        String uname;/*from  w  w  w . j  av  a  2 s  . co  m*/
        if (jsUser.has(NAME))
            uname = jsUser.get(NAME).getAsString();
        else
            uname = "";
        String uDescr;
        if (jsUser.has(DESCRIPTION))
            uDescr = jsUser.get(DESCRIPTION).getAsString();
        else
            uDescr = "";
        String externalUID;
        if (jsUser.has(E_UID))
            externalUID = jsUser.get(E_UID).getAsString();
        else
            externalUID = "";
        User user = new User(externalUID, uname, uDescr);
        users.put(Long.parseLong(u.getKey()), user);
    }
    return users;
}

From source file:at.tugraz.kmi.medokyservice.fca.util.ImportExport.java

License:Open Source License

private Map<Long, LearningObject> json2LearningObjects(JsonObject json, Map<Long, User> users) {
    HashMap<Long, LearningObject> learningObjects = new HashMap<Long, LearningObject>();
    JsonObject block = json.get(SECTION_LO).getAsJsonObject();

    Set<Entry<String, JsonElement>> entries = block.entrySet();
    for (Entry<String, JsonElement> lo : entries) {
        JsonObject jsLo = lo.getValue().getAsJsonObject();
        User owner;/*  www .j  a va 2  s . c om*/
        try {
            owner = users.get(Long.parseLong(jsLo.get(OWNER).getAsString()));
        } catch (Exception e) {
            owner = null;
        }
        LearningObject learningObject = new LearningObject(jsLo.get(NAME).getAsString(),
                jsLo.get(DESCRIPTION).getAsString(), jsLo.get(DATA).getAsString(), owner);
        learningObjects.put(Long.parseLong(lo.getKey()), learningObject);
    }
    return learningObjects;
}

From source file:at.tugraz.kmi.medokyservice.fca.util.ImportExport.java

License:Open Source License

@SuppressWarnings("unchecked")
private <E extends FCAAbstract> Map<Long, E> json2Absctract(JsonObject json,
        Map<Long, LearningObject> learningObjects, Class<E> type) {
    HashMap<Long, E> objects = new HashMap<Long, E>();
    JsonObject block = null;// w ww  . j a  v a  2s  .co  m
    if (type == FCAObject.class)
        block = json.get(SECTION_O).getAsJsonObject();
    else if (type == FCAAttribute.class)
        block = json.get(SECTION_A).getAsJsonObject();
    Set<Entry<String, JsonElement>> entries = block.entrySet();
    for (Entry<String, JsonElement> o : entries) {
        JsonObject jsO = o.getValue().getAsJsonObject();
        E object;
        String creationId;
        if (!jsO.has(CID)) {
            double num = rand.nextDouble() * 10000;
            int val = (int) num;
            creationId = Integer.toString(val, 16);
        } else
            creationId = jsO.get(CID).getAsString();
        if (type == FCAObject.class)
            object = (E) new FCAObject(jsO.get(NAME).getAsString(), jsO.get(DESCRIPTION).getAsString(),
                    creationId);
        else
            object = (E) new FCAAttribute(jsO.get(NAME).getAsString(), jsO.get(DESCRIPTION).getAsString(),
                    creationId);
        Set<LearningObject> lObjs = new HashSet<LearningObject>();
        Set<LearningObject> lObjsByLearner = new HashSet<LearningObject>();
        Iterator<JsonElement> lOs = jsO.getAsJsonArray(SECTION_LO).iterator();
        while (lOs.hasNext()) {
            LearningObject lo = learningObjects.get((lOs.next().getAsLong()));
            if (lo != null)
                lObjs.add(lo);
        }
        try {
            Iterator<JsonElement> lOsByLearner = jsO.getAsJsonArray(SECTION_LO_L).iterator();
            while (lOsByLearner.hasNext()) {
                LearningObject lo = learningObjects.get((lOsByLearner.next().getAsLong()));
                if (lo != null)
                    lObjsByLearner.add(lo);
            }
        } catch (Exception notAnError) {

        }
        clean(lObjsByLearner);
        object.setLearningObjectsByLearners(lObjsByLearner);
        clean(lObjs);
        object.setLearningObjects(lObjs);
        objects.put(Long.parseLong(o.getKey()), object);
    }
    return objects;
}

From source file:at.tugraz.kmi.medokyservice.fca.util.ImportExport.java

License:Open Source License

private Map<Long, FCAItemMetadata> json2Metadata(JsonObject json, Map<Long, LearningObject> learningObjects) {
    Map<Long, FCAItemMetadata> result = new HashMap<Long, FCAItemMetadata>();
    JsonObject jsM = json.get(SECTION_M).getAsJsonObject();
    for (Entry<String, JsonElement> entry : jsM.entrySet()) {
        JsonObject val = entry.getValue().getAsJsonObject();
        JsonArray arr = val.get(SECTION_LO).getAsJsonArray();
        LinkedHashSet<LearningObject> l_objsByLearner = new LinkedHashSet<LearningObject>();
        try {//w  ww  . j a  va 2s.  c  o m
            JsonArray arrByLearner = val.get(SECTION_LO_L).getAsJsonArray();
            for (JsonElement loID : arrByLearner)
                l_objsByLearner.add(learningObjects.get(loID.getAsLong()));
        } catch (Exception notAnError) {

        }
        LinkedHashSet<LearningObject> l_objs = new LinkedHashSet<LearningObject>();
        for (JsonElement loID : arr)
            l_objs.add(learningObjects.get(loID.getAsLong()));
        clean(l_objs);
        clean(l_objsByLearner);
        result.put(Long.parseLong(entry.getKey()), new FCAItemMetadata(val.get(DESCRIPTION).getAsString(),
                val.get(O_ID).getAsLong(), l_objs, l_objsByLearner));
    }
    return result;
}