Example usage for com.google.gson JsonParser JsonParser

List of usage examples for com.google.gson JsonParser JsonParser

Introduction

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

Prototype

@Deprecated
public JsonParser() 

Source Link

Usage

From source file:br.org.cesar.knot.lib.connection.ThingApi.java

License:Open Source License

/**
 * Send a message in Meshblu instance//from w  w w .  ja  v a 2  s  .  c  o m
 *
 * @param message model sample to create a new message. Basically this message model
 *                contains attributes that will be send into Meshblu.
 * @return New message with meshblu content.
 * @throws KnotException KnotException
 * @see AbstractThingMessage
 */
public <T extends AbstractThingMessage> T sendMessage(T message)
        throws KnotException, InvalidDeviceOwnerStateException {
    // Check if the current state of device owner is valid
    if (!isValidDeviceOwner()) {
        throw new InvalidDeviceOwnerStateException("The device owner is invalid or null");
    }

    final String endPoint = mEndPoint + MESSAGE;
    String json = mGson.toJson(message);

    RequestBody body = createRequestBodyWith(json);
    Request request = generateBasicRequestBuild(this.abstractDeviceOwner.getUuid(),
            this.abstractDeviceOwner.getToken(), endPoint).post(body).build();

    try {
        Response response = mHttpClient.newCall(request).execute();
        JsonElement jsonElement = new JsonParser().parse(response.body().string());

        return (T) mGson.fromJson(jsonElement.toString(), message.getClass());
    } catch (Exception e) {
        throw new KnotException(e);
    }
}

From source file:brooklyn.event.feed.http.JsonFunctions.java

License:Apache License

public static Function<String, JsonElement> asJson() {
    return new Function<String, JsonElement>() {
        @Override/*  www. ja va 2s  .  c o  m*/
        public JsonElement apply(String input) {
            return new JsonParser().parse(input);
        }
    };
}

From source file:brooklyn.networking.cloudstack.CloudstackNew40FeaturesClient.java

License:Apache License

public static JsonElement json(InputStream is) {
    JsonParser parser = new JsonParser();
    JsonReader reader = null;/*from   w  ww. j  a v a  2  s  .co m*/
    try {
        reader = new JsonReader(new InputStreamReader(is, "UTF-8"));
    } catch (UnsupportedEncodingException e) {
        throw Exceptions.propagate(e);
    }
    JsonElement el = parser.parse(reader);
    return el;
}

From source file:brooklyn.storage.softlayer.SoftLayerRestClient.java

License:Apache License

public static JsonElement json(InputStream is) {
    JsonParser parser = new JsonParser();
    JsonReader reader = null;//from  w w w .  ja v a2  s .  c o m
    try {
        reader = new JsonReader(new InputStreamReader(is, "UTF-8"));
    } catch (UnsupportedEncodingException e) {
        e.printStackTrace();
    }
    JsonElement el = parser.parse(reader);
    return el;
}

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

License:Apache License

private void recursiveDeserialize(JsonObject object, String id, ArrayList<Comment> list) {
    JsonParser parser = new JsonParser();
    JsonArray array = parser.parse(object.get(id).getAsString()).getAsJsonArray();
    for (int i = 0; i < array.size(); ++i) {
        list.add(GsonHelper.getOfflineGson().fromJson(array.get(i), Comment.class));
    }/*from w  ww .  ja v a 2 s . c  om*/
    for (Comment comment : list) {
        ArrayList<Comment> childList = new ArrayList<Comment>();
        recursiveDeserialize(object, comment.getId(), childList);
        comment.setChildren(childList);
    }
}

From source file:catalog.CloudantUtil.java

License:Apache License

public static JsonObject deleteTestDatabase(Credential credential, String dbName)
        throws UnsupportedEncodingException, InterruptedException {
    // Use DELETE to delete the database.
    // This could fail if the database already exists, but that's ok.
    Response response = null;//from   www .ja v  a 2 s . c  om
    String db = (dbName != null && !dbName.isEmpty()) ? dbName : credential.dbname;
    assertTrue("failed to determine database name", db != null && !db.isEmpty());
    response = given().port(443).baseUri(cloudantAccount(credential.user)).auth()
            .basic(credential.user, credential.password).when().delete("/" + db);
    System.out.format("Response of delete database %s: %s\n", db, response.asString());
    return (JsonObject) new JsonParser().parse(response.asString());
}

From source file:catalog.CloudantUtil.java

License:Apache License

private static Pair<Integer, JsonObject> createTestDatabase(Credential credential, boolean failIfCannotCreate)
        throws UnsupportedEncodingException {
    // Use PUT to create the database.
    // This could fail if the database already exists, but that's ok.
    String dbName = credential.dbname;
    assertTrue("failed to determine database name", dbName != null && !dbName.isEmpty());
    Response response = given().port(443).baseUri(cloudantAccount(credential.user)).auth()
            .basic(credential.user, credential.password).when().put("/" + dbName);
    System.out.format("Response of create database %s: %s\n", dbName, response.asString());
    if (failIfCannotCreate)
        assertTrue("failed to create database " + dbName,
                response.statusCode() == 201 || response.statusCode() == 202);
    return Pair.make(response.statusCode(), (JsonObject) new JsonParser().parse(response.asString()));
}

From source file:catalog.CloudantUtil.java

License:Apache License

/**
 * create a document in the cloudant database
 *
 * @throws UnsupportedEncodingException/*from   w w  w.j  a  v a 2 s .  co  m*/
 */
public static JsonObject createDocument(Credential credential, String jsonObj)
        throws UnsupportedEncodingException {
    JsonObject obj = new JsonParser().parse(jsonObj).getAsJsonObject();

    CloudantClient client = new CloudantClient(credential.user, credential.user, credential.password);
    Database db = client.database(credential.dbname, false);
    com.cloudant.client.api.model.Response res = db.post(obj);
    client.shutdown();

    JsonObject ret = new JsonObject();
    ret.addProperty("ok", true);
    ret.addProperty("id", res.getId());
    ret.addProperty("rev", res.getRev());
    return ret;
}

From source file:catalog.CloudantUtil.java

License:Apache License

/**
 * get a document from the cloudant database
 *
 * @throws UnsupportedEncodingException/*from   www.jav a2 s  . c o  m*/
 */
public static JsonObject getDocument(Credential credential, String docId) throws UnsupportedEncodingException {
    // use GET to get the document
    Response response = given().port(443).baseUri(cloudantAccount(credential.user)).auth()
            .basic(credential.user, credential.password).get("/" + credential.dbname + "/" + docId);
    String responseStr = response.asString();
    if (responseStr.length() > 500)
        responseStr = responseStr.substring(0, 500);
    System.out.format("Response of get document from database %s: %s\n", credential.dbname, responseStr);
    return (JsonObject) new JsonParser().parse(response.asString());
}

From source file:catalog.CloudantUtil.java

License:Apache License

/**
 * delete a document from the cloudant database
 *
 * @throws UnsupportedEncodingException/*from w  w  w.j a  v a 2s. com*/
 */
public static JsonObject deleteDocument(Credential credential, String docId)
        throws UnsupportedEncodingException {
    // use GET to get the document
    Response response = given().port(443).baseUri(cloudantAccount(credential.user)).auth()
            .basic(credential.user, credential.password).delete("/" + credential.dbname + "/" + docId);
    System.out.format("Response of delete document in database %s: %s\n", credential.dbname,
            response.asString());
    assertTrue("failed to delete document in database " + credential.dbname,
            response.statusCode() == 200 || response.statusCode() == 202);
    return (JsonObject) new JsonParser().parse(response.asString());
}