Example usage for com.google.gson JsonElement getAsJsonObject

List of usage examples for com.google.gson JsonElement getAsJsonObject

Introduction

In this page you can find the example usage for com.google.gson JsonElement getAsJsonObject.

Prototype

public JsonObject getAsJsonObject() 

Source Link

Document

convenience method to get this element as a JsonObject .

Usage

From source file:arces.unibo.SEPA.commons.SPARQL.BindingsResults.java

License:Open Source License

private JsonArray getBindingsArray() {
    JsonElement varArray;
    if (results == null)
        return null;
    if ((varArray = results.get("results")) == null)
        return null;
    if ((varArray = varArray.getAsJsonObject().get("bindings")) == null)
        return null;

    return varArray.getAsJsonArray();
}

From source file:arces.unibo.SEPA.commons.SPARQL.BindingsResults.java

License:Open Source License

private JsonArray getVariablesArray() {
    JsonElement varArray;
    if (results == null)
        return null;
    if ((varArray = results.get("head")) == null)
        return null;
    if ((varArray = varArray.getAsJsonObject().get("vars")) == null)
        return null;

    return varArray.getAsJsonArray();
}

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  www.  ja  va  2  s.co  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 .j  a  va  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.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);
        }/*  w w  w  .  ja va  2 s.c  o  m*/
        Log.w(LOG_TAG, "Ramp message not handle : " + t);
    }

    return null;
}

From source file:at.orz.arangodb.impl.InternalDocumentDriverImpl.java

License:Apache License

private <T> DocumentEntity<T> _createDocument(String database, String collectionName, String documentKey,
        Object value, Boolean createCollection, Boolean waitForSync, boolean raw) throws ArangoException {

    validateCollectionName(collectionName);

    String body;//from   w  w w  . j  av a  2 s .c  om
    if (raw) {
        body = value.toString();
    } else if (documentKey != null) {
        JsonElement elem = EntityFactory.toJsonElement(value, false);
        if (elem.isJsonObject()) {
            elem.getAsJsonObject().addProperty("_key", documentKey);
        }
        body = EntityFactory.toJsonString(elem);
    } else {
        body = EntityFactory.toJsonString(value);
    }

    HttpResponseEntity res = httpManager.doPost(createEndpointUrl(baseUrl, database, "/_api/document"),
            new MapBuilder().put("collection", collectionName).put("createCollection", createCollection)
                    .put("waitForSync", waitForSync).get(),
            body);

    return createEntity(res, DocumentEntity.class);

}

From source file:at.orz.arangodb.impl.InternalGraphDriverImpl.java

License:Apache License

public <T> EdgeEntity<T> createEdge(String database, String graphName, String key, String fromHandle,
        String toHandle, Object value, String label, Boolean waitForSync) throws ArangoException {

    JsonObject obj;/*from w  w  w . ja v  a 2s .  co  m*/
    if (value == null) {
        obj = new JsonObject();
    } else {
        JsonElement elem = EntityFactory.toJsonElement(value, false);
        if (elem.isJsonObject()) {
            obj = elem.getAsJsonObject();
        } else {
            throw new IllegalArgumentException("value need object type(not support array, primitive, etc..).");
        }
    }
    obj.addProperty("_key", key);
    obj.addProperty("_from", fromHandle);
    obj.addProperty("_to", toHandle);
    obj.addProperty("$label", label);

    validateCollectionName(graphName);
    HttpResponseEntity res = httpManager.doPost(
            createEndpointUrl(baseUrl, database, "/_api/graph", StringUtils.encodeUrl(graphName), "/edge"),
            new MapBuilder().put("waitForSync", waitForSync).get(), EntityFactory.toJsonString(obj));

    return createEntity(res, EdgeEntity.class, value == null ? null : value.getClass());

}

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;/* ww w  . j  a  v  a2s. co m*/
    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:au.edu.unsw.soacourse.client.ApplicationLogic.java

static public void getAppsByJobID(HttpServletRequest request, HttpServletResponse response)
        throws ServletException, IOException {
    response.setCharacterEncoding("utf8");
    response.setContentType("application/json");
    PrintWriter out = response.getWriter();
    try {/*from  w ww. ja  v  a 2s .co  m*/
        StringBuffer sb = new StringBuffer();
        BufferedReader reader = request.getReader();
        String temp = "";
        while ((temp = reader.readLine()) != null) {
            sb.append(temp);
        }
        // System.out.println(sb.toString());
        // convert to object
        Gson gson = new Gson();
        JsonElement je = gson.fromJson(sb.toString(), JsonElement.class);
        JsonObject jo = je.getAsJsonObject();

        String jobID = "";
        if (jo.has("jobID"))
            jobID = jo.get("jobID").getAsString();

        String REST_URI = "http://localhost:8080/RESTfulFoundITService/apps";
        REST_URI += "/jobID/" + jobID;

        WebClient client = WebClient.create(REST_URI);
        String s = client.get(String.class);
        // System.out.println(s);

        // write to json

        out.write(s);
        out.close();
    } catch (Exception e) {
        out.write("failed");
        e.printStackTrace();
        out.close();
    }

}

From source file:au.edu.unsw.soacourse.client.ApplicationLogic.java

static public void getAppsByAppID(HttpServletRequest request, HttpServletResponse response)
        throws ServletException, IOException {
    response.setCharacterEncoding("utf8");
    response.setContentType("application/json");
    PrintWriter out = response.getWriter();
    try {/*from   w w w . j a v  a2  s .c o  m*/
        StringBuffer sb = new StringBuffer();
        BufferedReader reader = request.getReader();
        String temp = "";
        while ((temp = reader.readLine()) != null) {
            sb.append(temp);
        }
        // System.out.println(sb.toString());
        // convert to object
        Gson gson = new Gson();
        JsonElement je = gson.fromJson(sb.toString(), JsonElement.class);
        JsonObject jo = je.getAsJsonObject();

        String appID = "";
        if (jo.has("appID"))
            appID = jo.get("appID").getAsString();

        String REST_URI = "http://localhost:8080/RESTfulFoundITService/apps/";
        REST_URI += appID;

        WebClient client = WebClient.create(REST_URI);
        String s = client.get(String.class);
        // System.out.println(s);

        // write to json

        out.write(s);
        out.close();
    } catch (Exception e) {
        out.write("failed");
        e.printStackTrace();
        out.close();
    }

}