Example usage for com.google.gson JsonObject getAsJsonObject

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

Introduction

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

Prototype

public JsonObject getAsJsonObject(String memberName) 

Source Link

Document

Convenience method to get the specified member as a JsonObject.

Usage

From source file:br.unicamp.cst.bindings.soar.SOARPlugin.java

License:Open Source License

public void addBranchToJson(String newBranch, JsonObject json, String value) {
    String[] newNodes = newBranch.split("\\.");
    JsonObject temp;// = new JsonObject();

    if (newNodes.length > 1) {
        if (json.has(newNodes[0])) {
            addBranchToJson(newBranch.substring(newNodes[0].length() + 1), json.getAsJsonObject(newNodes[0]),
                    value);//from   ww w. j  a  va2s  . c om
        } else {
            temp = createJsonFromString(newBranch.substring(newNodes[0].length() + 1), value);
            json.add(newNodes[0], temp);
        }
    } else {
        json.addProperty(newNodes[0], value);
    }
}

From source file:br.unicamp.cst.bindings.soar.SOARPlugin.java

License:Open Source License

public void addBranchToJson(String newBranch, JsonObject json, JsonObject value) {
    String[] newNodes = newBranch.split("\\.");
    JsonObject temp;// = new JsonObject();

    if (newNodes.length > 1) {
        if (json.has(newNodes[0])) {
            addBranchToJson(newBranch.substring(newNodes[0].length() + 1), json.getAsJsonObject(newNodes[0]),
                    value);/*from   w  ww  .  jav  a2 s. c  o m*/
        } else {
            temp = createJsonFromString(newBranch.substring(newNodes[0].length() + 1), value);
            json.add(newNodes[0], temp);
        }
    } else {
        json.add(newNodes[0], value);
    }
}

From source file:br.unicamp.cst.bindings.soar.SOARPlugin.java

License:Open Source License

public void removeBranchFromJson(String pathToOldBranch, JsonObject json) {
    String[] oldNodes = pathToOldBranch.split("\\.");
    if (oldNodes.length > 1) {
        if (json.has(oldNodes[0])) {
            removeBranchFromJson(pathToOldBranch.substring(oldNodes[0].length() + 1),
                    json.getAsJsonObject(oldNodes[0]));
        }//from   w ww . j  a va  2s.  c o m
    } else {
        json.remove(oldNodes[0]);
    }
}

From source file:brooklyn.entity.cloudfoundry.webapp.PaasWebAppCloudFoundryDriver.java

License:Apache License

private void updateVariableEnvironmentSensors() {
    Map<String, Object> env = getClient().getApplicationEnvironment(applicationName);
    JsonObject envTree = new Gson().toJsonTree(env).getAsJsonObject();
    getEntity().setAttribute(CloudFoundryWebApp.VCAP_SERVICES,
            envTree.getAsJsonObject("system_env_json").getAsJsonObject("VCAP_SERVICES").toString());
}

From source file:cc.twittertools.corpus.data.Status.java

License:Apache License

public static Status fromJson(String json) {
    JsonObject obj = null;
    try {/* w w  w .  j a v a  2 s.co  m*/
        obj = (JsonObject) JSON_PARSER.parse(json);
    } catch (Exception e) {
        // Catch any malformed JSON.
        LOG.error("Error parsing: " + json);
        return null;
    }

    if (obj.get("text") == null) {
        return null;
    }

    Status status = new Status();
    status.text = obj.get("text").getAsString();
    status.id = obj.get("id").getAsLong();
    status.screenname = obj.get("user").getAsJsonObject().get("screen_name").getAsString();
    status.createdAt = obj.get("created_at").getAsString();

    try {
        status.epoch = (new SimpleDateFormat(DATE_FORMAT)).parse(status.createdAt).getTime() / 1000;
    } catch (ParseException e) {
        status.epoch = -1L;
    }

    // TODO: trying to fetch fields and then catching exceptions is bad practice, fix!
    try {
        status.inReplyToStatusId = obj.get("in_reply_to_status_id").getAsLong();
    } catch (Exception e) {
        status.inReplyToStatusId = -1L;
    }

    try {
        status.inReplyToUserId = obj.get("in_reply_to_user_id").getAsLong();
    } catch (Exception e) {
        status.inReplyToUserId = -1L;
    }

    try {
        status.retweetStatusId = obj.getAsJsonObject("retweeted_status").get("id").getAsLong();
        status.retweetUserId = obj.getAsJsonObject("retweeted_status").get("user").getAsJsonObject().get("id")
                .getAsLong();
        // retweet_count might say "100+"
        // TODO: This is ugly, come back and fix later.
        status.retweetCount = Integer.parseInt(obj.get("retweet_count").getAsString().replace("+", ""));
    } catch (Exception e) {
        status.retweetStatusId = -1L;
        status.retweetUserId = -1L;
        status.retweetCount = -1;
    }

    try {
        status.inReplyToUserId = obj.get("in_reply_to_user_id").getAsLong();
    } catch (Exception e) {
        status.inReplyToUserId = -1L;
    }

    try {
        status.latitude = obj.getAsJsonObject("coordinates").getAsJsonArray("coordinates").get(1).getAsDouble();
        status.longitude = obj.getAsJsonObject("coordinates").getAsJsonArray("coordinates").get(0)
                .getAsDouble();
    } catch (Exception e) {
        status.latitude = Double.NEGATIVE_INFINITY;
        status.longitude = Double.NEGATIVE_INFINITY;
    }

    try {
        status.lang = obj.get("lang").getAsString();
    } catch (Exception e) {
        status.lang = "unknown";
    }

    status.followersCount = obj.get("user").getAsJsonObject().get("followers_count").getAsInt();
    status.friendsCount = obj.get("user").getAsJsonObject().get("friends_count").getAsInt();
    status.statusesCount = obj.get("user").getAsJsonObject().get("statuses_count").getAsInt();

    status.jsonObject = obj;
    status.jsonString = json;

    return status;
}

From source file:ccm.pay2spawn.types.CustomEntityType.java

License:Open Source License

@Override
public String replaceInTemplate(String id, JsonObject jsonObject) {
    switch (id) {
    case "entity":
        StringBuilder sb = new StringBuilder();
        sb.append(jsonObject.get("id").getAsString().replace("STRING:", ""));
        while (jsonObject.has(RIDING_KEY)) {
            jsonObject = jsonObject.getAsJsonObject(RIDING_KEY);
            sb.append(" riding a ").append(jsonObject.get("id").getAsString().replace("STRING:", ""));
        }//from  ww w.j  a v a2s .co  m
        return sb.toString();
    }
    return id;
}

From source file:ccm.pay2spawn.types.EntityType.java

License:Open Source License

@Override
public String replaceInTemplate(String id, JsonObject jsonObject) {
    switch (id) {
    case "entity":
        StringBuilder sb = new StringBuilder();
        sb.append(jsonObject.get(ENTITYNAME_KEY).getAsString().replace("STRING:", ""));
        while (jsonObject.has(RIDING_KEY)) {
            jsonObject = jsonObject.getAsJsonObject(RIDING_KEY);
            sb.append(" riding a ").append(jsonObject.get(ENTITYNAME_KEY).getAsString().replace("STRING:", ""));
        }//from   www  . j a  va 2s. c  om
        return sb.toString();
    }
    return id;
}

From source file:ccm.pay2spawn.util.Reward.java

License:Open Source License

public String getHTML() throws IOException {
    StringBuilder sb = new StringBuilder();
    for (JsonElement element : rewards) {
        JsonObject object = element.getAsJsonObject();
        if (object.has(CUSTOMHTML) && !Strings.isNullOrEmpty(object.get(CUSTOMHTML).getAsString()))
            sb.append(object.get(CUSTOMHTML).getAsString());
        else//from   w  w w  .ja  v a  2  s . c  o m
            sb.append(TypeRegistry.getByName(object.get("type").getAsString())
                    .getHTML(object.getAsJsonObject("data")));
    }
    return sb.toString();
}

From source file:ch.cyberduck.core.dropbox.DropboxExceptionMappingService.java

License:Open Source License

private void parse(final StringBuilder buffer, final String message) {
    final JsonParser parser = new JsonParser();
    try {// ww  w . j a v a  2s.  c  o m
        final JsonElement element = parser.parse(new StringReader(message));
        if (element.isJsonObject()) {
            final JsonObject json = element.getAsJsonObject();
            final JsonObject error = json.getAsJsonObject("error");
            if (null == error) {
                this.append(buffer, message);
            } else {
                final JsonPrimitive tag = error.getAsJsonPrimitive(".tag");
                if (null == tag) {
                    this.append(buffer, message);
                } else {
                    this.append(buffer, StringUtils.replace(tag.getAsString(), "_", " "));
                }
            }
        }
        if (element.isJsonPrimitive()) {
            this.append(buffer, element.getAsString());
        }
    } catch (JsonParseException e) {
        // Ignore
    }
}

From source file:citysdk.tourism.client.parser.POIDeserializer.java

License:Open Source License

private HypermediaLink getResource(JsonObject ob) {
    HypermediaLink hypermediaLink = new HypermediaLink();
    hypermediaLink.setVersion(ob.get(VERSION).getAsString());

    if (ob.has(_LINK)) {
        JsonObject map = ob.getAsJsonObject(_LINK);
        Set<Entry<String, JsonElement>> keySet = map.entrySet();
        for (Entry<String, JsonElement> entry : keySet) {
            JsonElement element = entry.getValue();
            JsonObject value = element.getAsJsonObject();

            Hypermedia link = new Hypermedia();
            link.setHref(value.get(HREF).getAsString());
            link.setTemplated(value.get(TEMPLATED).getAsBoolean());

            hypermediaLink.addHypermedia(entry.getKey(), link);
        }// w  w  w  .j  a  v  a 2 s.com
    }

    return hypermediaLink;
}