Example usage for com.google.gson JsonObject getAsJsonPrimitive

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

Introduction

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

Prototype

public JsonPrimitive getAsJsonPrimitive(String memberName) 

Source Link

Document

Convenience method to get the specified member as a JsonPrimitive element.

Usage

From source file:JavaAction.java

License:Apache License

public static JsonObject main(JsonObject args) {
    String text;//from   w  w w. ja va  2  s.  c  om

    try {
        text = args.getAsJsonPrimitive("text").getAsString();
    } catch (Exception e) {
        text = "stranger";
    }

    JsonObject response = new JsonObject();
    System.out.println("Hello " + text + "!");
    response.addProperty("payload", "Hello " + text + "!");
    return response;
}

From source file:Unicode.java

License:Apache License

public static JsonObject main(JsonObject args) throws InterruptedException {
    String delimiter = args.getAsJsonPrimitive("delimiter").getAsString();
    JsonObject response = new JsonObject();
    String str = delimiter + "  " + delimiter;
    System.out.println(str);/*from w  w  w .  j a  v a  2 s. c  o  m*/
    response.addProperty("winter", str);
    return response;
}

From source file:JavaTestRunner.java

License:Apache License

public static Object runQuery(JavaQuery query, JsonObject combinedInput) {
    JsonArray hierarchy = combinedInput.getAsJsonArray("inheritance");
    JsonElement input = combinedInput.get("input");
    final ZonedDateTime now;
    JsonPrimitive primnow = combinedInput.getAsJsonPrimitive("now");
    if (primnow == null) {
        now = ZonedDateTime.now();
    } else {/*from  ww w  . j a va2 s.  c om*/
        final String strnow = (String) (primnow.getAsString());
        now = ZonedDateTime.parse(strnow);
    }
    return runQuery(query, hierarchy, input, now);
}

From source file:Sleep.java

License:Apache License

public static JsonObject main(JsonObject parm) throws InterruptedException {
    int sleepTimeInMs = 1;
    if (parm.has("sleepTimeInMs")) {
        sleepTimeInMs = parm.getAsJsonPrimitive("sleepTimeInMs").getAsInt();
    }//from w  w  w  .j a va2s  . c o  m
    System.out.println("Specified sleep time is " + sleepTimeInMs + " ms.");

    final String responseText = "Terminated successfully after around " + sleepTimeInMs + " ms.";
    final JsonObject response = new JsonObject();
    response.addProperty("msg", responseText);

    Thread.sleep(sleepTimeInMs);

    System.out.println(responseText);
    return response;
}

From source file:ProcessRequest.java

public JSONObject parseQuery(JSONObject requestObject, OutputStream os)
        throws JSONException, SQLException, IOException {
    String query = "";
    String tableName = "";

    JSONArray selectionColumns = requestObject.getJSONArray("selection");
    query += "SELECT " + selectionColumns.getString(0);
    for (int i = 1; i < selectionColumns.length(); i++) {
        query += "," + selectionColumns.getString(i);
    }//from  w  w  w .j  ava  2s.c om
    query += " FROM ";
    tableName = requestObject.getString("table");
    query += tableName;
    if (requestObject.has("where")) {
        query += " WHERE ";
        query += requestObject.getString("where");
    }
    query += ";";

    JSONObject responseObject = new JSONObject();
    JSONArray resultJSONArray;

    //System.out.println(query);
    if (os != null) {
        os.write(new String("{\"result_set\":[").getBytes());
        parseQueryResults(executeQuery(query), tableName, os, false);
    }

    resultJSONArray = parseQueryResults(executeQuery(query), tableName);
    //System.out.println(resultSet.toString(5));
    if (requestObject.has("join")) {
        JSONArray joinArray = requestObject.getJSONArray("join");
        List<String> tableList = new LinkedList<String>();
        tableList.add(tableName);
        for (int h = 0; h < joinArray.length(); h++) {
            //find the next link table
            JSONObject joinObject = null;
            for (int j = 0; j < joinArray.length(); j++) {
                for (int k = 0; k < tableList.size(); k++) {
                    if (joinArray.getJSONObject(j).getString("table").equals(tableList.get(k))) {
                        //break;   //this table has already been joined
                    } else if (joinArray.getJSONObject(j).getString("link_table").equals(tableList.get(k))) {
                        joinObject = joinArray.getJSONObject(j);
                    }
                }
            }
            if (joinObject == null) {
                throw new JSONException("join syntax was incorrect, no valid joins were found");
            }
            for (int i = 0; i < resultJSONArray.length(); i++) {
                //we now know the table to join, now search through the results looking for the link_table and query on each
                if (joinObject.getString("link_table").equals(
                        resultJSONArray.getJSONObject(i).getJSONObject("metadata").getString("table"))) {
                    //this result contains the correct link_table.  query against it's link_column value
                    JsonObject linkObject = new JsonParser().parse(resultJSONArray.getJSONObject(i).toString())
                            .getAsJsonObject();
                    JsonPrimitive linkValue = null;
                    if (linkObject.has(joinObject.getString("link_column"))) {
                        linkValue = linkObject.getAsJsonPrimitive(joinObject.getString("link_column"));
                    } else {
                        //just skip this result and notify the log console.
                        //however, this is most likely an error and should be fixed.
                        System.out.println("link column did not contain the following column (please fix): "
                                + joinObject.get("link_column"));
                    }

                    String predicate = "1=1";
                    if (linkValue.isBoolean()) {
                        if (linkValue.getAsBoolean() == true) {
                            predicate = joinObject.getString("column") + "=1";
                        } else {
                            predicate = joinObject.getString("column") + "=0";
                        }
                    }
                    if (linkValue.isNumber()) {
                        predicate = joinObject.getString("column") + "=" + linkValue.getAsString();
                    }
                    if (linkValue.isString()) {
                        predicate = joinObject.getString("column") + "='" + linkValue.getAsString() + "'";
                    }
                    String joinQuery = "";
                    if (joinObject.has("selection")) {
                        JSONArray joinSelectionColumns = joinObject.getJSONArray("selection");
                        joinQuery += "SELECT " + joinSelectionColumns.getString(0);
                        for (int k = 1; k < joinSelectionColumns.length(); k++) {
                            joinQuery += "," + joinSelectionColumns.getString(k);
                        }
                    } else {
                        joinQuery += "SELECT *";
                    }
                    //build and execute query, adding it to the result set
                    joinQuery += " FROM " + joinObject.getString("table") + " WHERE " + predicate;
                    if (joinObject.has("where")) {
                        String whereClause = joinObject.getString("where");
                        joinQuery += " AND " + whereClause;
                    }
                    joinQuery += ";";
                    //System.out.println("join query: "+joinQuery);
                    //JSONArray parsedResult = parseQueryResults(executeQuery(joinQuery), joinObject.getString("table"));
                    //System.out.println("join parsed result: "+parsedResult.toString(5));
                    if (os != null)
                        parseQueryResults(executeQuery(query), joinObject.getString("table"), os, true);
                    else
                        concatArray(resultJSONArray,
                                parseQueryResults(executeQuery(query), joinObject.getString("table")));
                }
            }
            tableList.add(joinObject.getString("table"));
        }
    }
    if (os != null)
        os.write(new String("]}\n").getBytes());
    responseObject.put("result_set", resultJSONArray);
    //System.out.println(responseObject.toString(5));
    return responseObject;
}

From source file:aproject.App.java

License:Apache License

public static JsonObject main(JsonObject args) {
    String name = "stranger";
    if (args.has("name"))
        name = args.getAsJsonPrimitive("name").getAsString();
    JsonObject response = new JsonObject();
    response.addProperty("greeting", "Hello " + name + "!");
    return response;
}

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

    return null;
}

From source file:at.orz.arangodb.entity.EntityDeserializers.java

License:Apache License

private static <T extends BaseEntity> T deserializeBaseParameter(JsonObject obj, T entity) {

    if (obj.has("error")) {
        entity.error = obj.getAsJsonPrimitive("error").getAsBoolean();
    }//from  w  w w.ja  v  a 2 s  .co  m
    if (obj.has("code")) {
        entity.code = obj.getAsJsonPrimitive("code").getAsInt();
    }
    if (obj.has("errorNum")) {
        entity.errorNumber = obj.getAsJsonPrimitive("errorNum").getAsInt();
    }
    if (obj.has("errorMessage")) {
        entity.errorMessage = obj.getAsJsonPrimitive("errorMessage").getAsString();
    }
    if (obj.has("etag")) {
        entity.etag = obj.getAsJsonPrimitive("etag").getAsLong();
    }

    return entity;
}

From source file:at.orz.arangodb.entity.EntityDeserializers.java

License:Apache License

private static <T extends DocumentHolder> T deserializeDocumentParameter(JsonObject obj, T entity) {

    if (obj.has("_rev")) {
        entity.setDocumentRevision(obj.getAsJsonPrimitive("_rev").getAsLong());
    }//from  w  ww  .jav a  2s  .  co  m
    if (obj.has("_id")) {
        entity.setDocumentHandle(obj.getAsJsonPrimitive("_id").getAsString());
    }
    if (obj.has("_key")) {
        entity.setDocumentKey(obj.getAsJsonPrimitive("_key").getAsString());
    }

    return entity;
}

From source file:ca.ualberta.cmput301w14t08.geochan.json.LocationJsonConverter.java

License:Apache License

/**
 * Deserializes data into a Location from JSON format.
 * /*from   ww  w.  j  a va  2s . c o  m*/
 * (Some of this code is taken from a stackOverflow user
 * Brian Roach, for details, see: http://stackoverflow.com/a/13997920)
 * 
 * @param je
 *            the JSON element to deserialize
 * @param type
 *            the Type
 * @param jdc
 *            the JSON deserialization context
 *
 * @return The deserialized Location object.
 * 
 * @throws JsonParseException
  */
@Override
public Location deserialize(JsonElement je, Type type, JsonDeserializationContext jdc)
        throws JsonParseException {
    JsonObject jo = je.getAsJsonObject();

    Location l = new Location(jo.getAsJsonPrimitive("mProvider").getAsString());
    l.setAccuracy(jo.getAsJsonPrimitive("mAccuracy").getAsFloat());
    l.setLongitude(jo.getAsJsonPrimitive("longitude").getAsFloat());
    l.setLatitude(jo.getAsJsonPrimitive("latitude").getAsFloat());

    return l;
}