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:com.pinterest.deployservice.common.EventMessageParser.java

License:Apache License

/**
 * Take a JSON string and convert into a list of Alarm configs.
 *//*from   w  ww .  j  a v  a 2s . com*/
public EventMessage fromJson(String payload) {
    if (StringUtils.isEmpty(payload)) {
        return null;
    }

    JsonParser parser = new JsonParser();
    JsonObject jsonObj = (JsonObject) parser.parse(payload);
    String notifType = jsonObj.getAsJsonPrimitive(TYPE).getAsString();
    if (!notifType.equals(NOTIFICATION)) {
        return null;
    }

    String messageBody = jsonObj.getAsJsonPrimitive(MESSAGE_BODY).getAsString();
    JsonObject message = (JsonObject) parser.parse(messageBody);
    String groupName = message.getAsJsonPrimitive(AUTO_SCALING_GROUP).getAsString();

    // Lifecycle event parsing
    if (message.getAsJsonPrimitive(LIFECYCLE_ACTION_TYPE) != null) {
        String lifecycleActionType = message.getAsJsonPrimitive(LIFECYCLE_ACTION_TYPE).getAsString();
        LOG.debug(String.format("Lifecycle %s notification message", lifecycleActionType));
        String instanceId = message.getAsJsonPrimitive(EC2_INSTANCE_ID).getAsString();
        String lifecycleToken = message.getAsJsonPrimitive(LIFECYCLE_TOKEN).getAsString();
        long timestamp = getDateTime(message.getAsJsonPrimitive("Time").getAsString());
        if (!StringUtils.isEmpty(lifecycleToken)) {
            String lifecycleHook = message.getAsJsonPrimitive(LIFECYCLE_HOOK).getAsString();
            return new EventMessage(lifecycleToken, lifecycleHook, groupName, lifecycleActionType, instanceId,
                    timestamp);
        }
    }

    String eventType = message.getAsJsonPrimitive("Event").getAsString();
    if (eventType.equals(TEST_NOTIFICATION)) {
        LOG.debug("Ingore test notification message:", payload);
        return null;
    }

    // Autoscaling event parsing
    String instanceId = message.getAsJsonPrimitive(EC2_INSTANCE_ID).getAsString();
    String cause = message.getAsJsonPrimitive(CAUSE).getAsString();
    long timestamp = getDateTime(message.getAsJsonPrimitive(INSTANCE_LAUNCH_TIME).getAsString());
    return new EventMessage(instanceId, groupName, eventType, cause, timestamp);
}

From source file:com.pinterest.deployservice.rodimus.RodimusManagerImpl.java

License:Apache License

@Override
public Long getClusterInstanceLaunchGracePeriod(String clusterName) throws Exception {
    String url = String.format("%s/v1/groups/%s/config", rodimusUrl, clusterName);
    String res = httpClient.get(url, null, null, headers, RETRIES);
    JsonObject jsonObject = gson.fromJson(res, JsonObject.class);
    if (jsonObject == null || jsonObject.isJsonNull()) {
        return null;
    }//from  www  . j  av a 2 s  .c o m

    JsonPrimitive launchGracePeriod = jsonObject.getAsJsonPrimitive("launchLatencyTh");
    if (launchGracePeriod == null || launchGracePeriod.isJsonNull()) {
        return null;
    }

    return launchGracePeriod.getAsLong();
}

From source file:com.pmeade.arya.gson.deserialize.EnumFieldDeserializer.java

License:Open Source License

/**
 * Request deserialization of a JSON string into an enumerated type in
 * the field in the provided Object./*w w  w  . j av  a  2s. com*/
 * @param jo JsonObject containing the value to be deserialized
 * @param t Object to be populated with the enumerated value
 * @param field Field of the object to be populated (also identifies the
 *              name of the JSON field to be deserialized)
 * @throws IllegalAccessException never thrown (you don't actually believe
 *                                what I just told you, right?)
 */
public void deserialize(JsonObject jo, Object t, Field field) throws IllegalAccessException {
    // if this JsonObject has something to map into the object's field
    if (jo.has(field.getName())) {
        // obtain the string representation of the enumerated type from the JSON
        String enumKey = jo.getAsJsonPrimitive(field.getName()).getAsString();
        // convert the string representation into an enumerated value object
        Object o = Enum.valueOf((Class<Enum>) field.getType(), enumKey);
        // populate the provided object in the provided field with the
        // enumerated value that we obtained
        field.set(t, o);
    }
}

From source file:com.pmeade.arya.gson.deserialize.ObjectFieldDeserializer.java

License:Open Source License

/**
 * Request deserialization of a UUID into the provided Object.
 * @param jo JsonObject containing the UUID identity of the object
 * @param t Object to be populated with the deserialized object
 * @param field Field of the object to be populated (also identifies the
 *              name of the UUID in the JsonObject to be deserialized)
 * @throws IllegalAccessException never thrown (you don't actually believe
 *                                what I just told you, right?)
 *///from   w ww .  j a va  2  s.  c o  m
public void deserialize(JsonObject jo, Object t, Field field) throws IllegalAccessException {
    // if the provided JsonObject has a UUID mapped to the field name
    if (jo.has(field.getName())) {
        // obtain the UUID identity of the obejct (in String form)
        String sUuid = jo.getAsJsonPrimitive(field.getName()).getAsString();
        // convert the String form into an actual UUID object
        java.util.UUID uuid = java.util.UUID.fromString(sUuid);
        // use the UUID to request that Arya deserialize the complex object
        Object o = arya.load(uuid, field.getType());
        // set the deserialized object in the provided field on the
        // provided object
        field.set(t, o);
    }
}

From source file:com.px100systems.util.IpLocator.java

License:Open Source License

public static Info locateIpOrHost(String ipOrHost) {
    try {// ww  w  .  j  a v a2  s .  co  m
        JsonObject jso = new com.google.gson.JsonParser().parse(
                Request.Get("http://freegeoip.net/json/" + ipOrHost).execute().returnContent().asString())
                .getAsJsonObject();

        return new Info(jso.getAsJsonPrimitive("ip").getAsString(),
                jso.getAsJsonPrimitive("country_code").getAsString(),
                jso.getAsJsonPrimitive("country_name").getAsString(),
                jso.getAsJsonPrimitive("region_code").getAsString(),
                jso.getAsJsonPrimitive("region_name").getAsString(),
                jso.getAsJsonPrimitive("city").getAsString(), jso.getAsJsonPrimitive("zip_code").getAsString(),
                jso.getAsJsonPrimitive("latitude").getAsDouble(),
                jso.getAsJsonPrimitive("longitude").getAsDouble());
    } catch (IOException e) {
        return null;
    }
}

From source file:com.razza.apps.iosched.server.schedule.model.DataExtractor.java

License:Open Source License

@Deprecated
private void setRelatedVideos(JsonObject origin, JsonObject dest) {
    JsonArray related = DataModelHelper.getAsArray(origin, InputJsonKeys.VendorAPISource.Topics.related);
    if (related == null) {
        return;//from w  ww  .  ja  v  a  2  s .  co m
    }
    for (JsonElement el : related) {
        if (!el.isJsonObject()) {
            continue;
        }
        JsonObject obj = el.getAsJsonObject();
        if (!obj.has("name") || !obj.has("values")) {
            continue;
        }

        if (InputJsonKeys.VendorAPISource.Topics.RELATED_NAME_VIDEO
                .equals(obj.getAsJsonPrimitive("name").getAsString())) {

            JsonElement values = obj.get("values");
            if (!values.isJsonArray()) {
                continue;
            }

            // As per the data specification, related content is formatted as
            // "video1 title1\nvideo2 title2\n..."
            StringBuilder relatedContentStr = new StringBuilder();
            for (JsonElement value : values.getAsJsonArray()) {
                String relatedSessionId = value.getAsString();
                JsonObject relatedVideo = videoSessionsById.get(relatedSessionId);
                if (relatedVideo != null) {
                    JsonElement vid = DataModelHelper.get(relatedVideo, OutputJsonKeys.VideoLibrary.vid);
                    JsonElement title = DataModelHelper.get(relatedVideo, OutputJsonKeys.VideoLibrary.title);
                    if (vid != null && title != null) {
                        relatedContentStr.append(vid.getAsString()).append(" ").append(title.getAsString())
                                .append("\n");
                    }
                }
            }
            DataModelHelper.set(new JsonPrimitive(relatedContentStr.toString()), dest,
                    OutputJsonKeys.Sessions.relatedContent);
        }
    }
}

From source file:com.razza.apps.iosched.server.schedule.model.DataExtractor.java

License:Open Source License

private void setRelatedContent(JsonObject origin, JsonObject dest) {
    JsonArray related = DataModelHelper.getAsArray(origin, InputJsonKeys.VendorAPISource.Topics.related);
    JsonArray outputArray = new JsonArray();

    if (related == null) {
        return;//from  w  ww  .j  av a  2  s .co  m
    }
    for (JsonElement el : related) {
        if (!el.isJsonObject()) {
            continue;
        }
        JsonObject obj = el.getAsJsonObject();
        if (!obj.has("name") || !obj.has("values")) {
            continue;
        }

        if (InputJsonKeys.VendorAPISource.Topics.RELATED_NAME_SESSIONS
                .equals(obj.getAsJsonPrimitive("name").getAsString())) {

            JsonElement values = obj.get("topics");
            if (!values.isJsonArray()) {
                continue;
            }

            // As per the data specification, related content is formatted as
            // "video1 title1\nvideo2 title2\n..."
            for (JsonElement topic : values.getAsJsonArray()) {
                if (!topic.isJsonObject()) {
                    continue;
                }

                JsonObject topicObj = topic.getAsJsonObject();

                String id = DataModelHelper.get(topicObj, InputJsonKeys.VendorAPISource.RelatedTopics.id)
                        .getAsString();
                String title = DataModelHelper.get(topicObj, InputJsonKeys.VendorAPISource.RelatedTopics.title)
                        .getAsString();

                if (id != null && title != null) {
                    JsonObject outputObj = new JsonObject();
                    DataModelHelper.set(new JsonPrimitive(id), outputObj, OutputJsonKeys.RelatedContent.id);
                    DataModelHelper.set(new JsonPrimitive(title), outputObj,
                            OutputJsonKeys.RelatedContent.title);
                    outputArray.add(outputObj);
                }
            }
            DataModelHelper.set(outputArray, dest, OutputJsonKeys.Sessions.relatedContent);
        }
    }
}

From source file:com.reclabs.recomendar.esdriver.helper.serializers.RecMoneyDeserializer.java

License:Open Source License

@Override
public RecMoney deserialize(JsonElement json, Type typeOfT, JsonDeserializationContext context)
        throws JsonParseException {
    JsonObject jsonObject = (JsonObject) json;
    JsonPrimitive currency = jsonObject.getAsJsonPrimitive("currency");
    JsonPrimitive amount = jsonObject.getAsJsonPrimitive("amount");
    return RecMoney.parse(currency.getAsString() + " " + amount.getAsString());
}

From source file:com.samsung.sjs.FFILinkage.java

License:Apache License

public void parseDecl(JsonObject decl) {
    String name = decl.getAsJsonPrimitive("name").getAsString();
    boolean boxed = decl.getAsJsonPrimitive("boxed").getAsBoolean();
    if (decl.has("rewrite")) {
        System.err.println("Ignoring rewrite of top-level [" + name + "] into: "
                + decl.getAsJsonPrimitive("rewrite").getAsString());
    }//from w  w w  .j  a  v  a 2  s  .  co m
    boolean untyped = decl.has("untyped") && decl.getAsJsonPrimitive("untyped").getAsBoolean();
    LinkEntry l = new LinkEntry(name, boxed, untyped);
    put(name, l);
}

From source file:com.samsung.sjs.FFILinkage.java

License:Apache License

public void parseTable(JsonObject table) {
    String name = table.getAsJsonPrimitive("name").getAsString();
    JsonArray arr = table.getAsJsonArray("fields");
    LinkedList<String> l = new LinkedList<>();
    for (JsonElement fname : arr) {
        l.add(fname.getAsJsonPrimitive().getAsString());
    }//from  w ww .ja v a2  s  .c  o  m
    tables_to_generate.put(name, l);
}