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:com.ontotext.s4.SBTDemo.parse.JsonToRDF.java

License:Apache License

/**
 * This method should create/* w ww.  j  a  v a  2  s  . co  m*/
 * 
 * @param jsonLine
 * @param fileSubject
 * @return
 */
public List<String[]> parse(String jsonLine, String fileSubject) throws Exception {
    List<String[]> hashList = new LinkedList<String[]>();

    JsonElement jelement = new JsonParser().parse(jsonLine);
    JsonObject jobject = jelement.getAsJsonObject();
    String text = null;
    if (jobject.has(textJson)) {
        text = jobject.getAsJsonPrimitive(textJson).getAsString();
    }
    if (jobject.has(entity)) {

        JsonObject entities = jobject.getAsJsonObject(entity);
        for (Iterator<Entry<String, JsonElement>> iterator = entities.entrySet().iterator(); iterator
                .hasNext();) {
            Entry<String, JsonElement> element = iterator.next();
            JsonArray jsonArrayOfEntities = element.getValue().getAsJsonArray();
            for (JsonElement jsonElement : jsonArrayOfEntities) {
                JsonObject annotation = jsonElement.getAsJsonObject();
                try {
                    String[] fileMentionsArray = new String[] { fileSubject, MENTIONS_URI,
                            annotation.getAsJsonPrimitive(inst).getAsString() };
                    String[] instanceTypeArray = new String[] {
                            annotation.getAsJsonPrimitive(inst).getAsString(), RDF_TYPE_URI,
                            annotation.getAsJsonPrimitive(classProperty).getAsString() };
                    String[] instanceLabelArray = new String[] {
                            annotation.getAsJsonPrimitive(inst).getAsString(), LABEL_URI,
                            annotation.getAsJsonPrimitive(stringProperty).getAsString() };
                    String[] classLableArray = new String[] {
                            annotation.getAsJsonPrimitive(classProperty).getAsString(), LABEL_URI,
                            annotation.getAsJsonPrimitive(type).getAsString().replaceAll("_", " ") };
                    String[] stringAbstractText = new String[] { fileSubject, ABSTRACT_TEXT_URI, text };

                    hashList.add(fileMentionsArray);
                    hashList.add(instanceTypeArray);
                    hashList.add(classLableArray);
                    hashList.add(instanceLabelArray);
                    hashList.add(stringAbstractText);

                } catch (Exception e) {
                    logger.error(e);
                }

            }

        }

    }
    return hashList;
}

From source file:com.optimizely.ab.config.parser.GsonHelpers.java

License:Apache License

static Experiment parseExperiment(JsonObject experimentJson, String groupId,
        JsonDeserializationContext context) {
    String id = experimentJson.get("id").getAsString();
    String key = experimentJson.get("key").getAsString();
    JsonElement experimentStatusJson = experimentJson.get("status");
    String status = experimentStatusJson.isJsonNull() ? ExperimentStatus.NOT_STARTED.toString()
            : experimentStatusJson.getAsString();

    JsonElement layerIdJson = experimentJson.get("layerId");
    String layerId = layerIdJson == null ? null : layerIdJson.getAsString();

    JsonArray audienceIdsJson = experimentJson.getAsJsonArray("audienceIds");
    List<String> audienceIds = new ArrayList<>(audienceIdsJson.size());
    for (JsonElement audienceIdObj : audienceIdsJson) {
        audienceIds.add(audienceIdObj.getAsString());
    }//  ww  w  .  ja v  a2 s. c  o  m

    Condition conditions = parseAudienceConditions(experimentJson);

    // parse the child objects
    List<Variation> variations = parseVariations(experimentJson.getAsJsonArray("variations"), context);
    Map<String, String> userIdToVariationKeyMap = parseForcedVariations(
            experimentJson.getAsJsonObject("forcedVariations"));
    List<TrafficAllocation> trafficAllocations = parseTrafficAllocation(
            experimentJson.getAsJsonArray("trafficAllocation"));

    return new Experiment(id, key, status, layerId, audienceIds, conditions, variations,
            userIdToVariationKeyMap, trafficAllocations, groupId);
}

From source file:com.owncloud.android.lib.resources.activities.GetRemoteActivitiesOperation.java

License:Open Source License

private ArrayList<Activity> parseResult(String response) throws JSONException {
    JsonParser jsonParser = new JsonParser();
    JsonObject jo = (JsonObject) jsonParser.parse(response);
    JsonArray jsonDataArray = jo.getAsJsonObject(NODE_OCS).getAsJsonArray(NODE_DATA);

    Gson gson = new GsonBuilder().registerTypeAdapter(RichElement.class, new RichElementTypeAdapter())//Add TypeAdapter to parse RichElement
            .create();//from ww  w  . j  ava 2 s .c o m
    Type listType = new TypeToken<List<Activity>>() {
    }.getType();

    return gson.fromJson(jsonDataArray, listType);
}

From source file:com.owncloud.android.lib.resources.notifications.GetRemoteNotificationsOperation.java

License:Open Source License

private List<Notification> parseResult(String response) throws JSONException {
    JsonParser jsonParser = new JsonParser();
    JsonObject jo = (JsonObject) jsonParser.parse(response);
    JsonArray jsonDataArray = jo.getAsJsonObject(NODE_OCS).getAsJsonArray(NODE_DATA);

    Gson gson = new Gson();
    Type listType = new TypeToken<List<Notification>>() {
    }.getType();//from   w  w  w . j  a  v  a  2 s. c om

    return gson.fromJson(jsonDataArray, listType);
}

From source file:com.owncloud.android.lib.resources.notifications.RegisterAccountDeviceForNotificationsOperation.java

License:Open Source License

private PushResponse parseResult(String response) throws JSONException {
    JsonParser jsonParser = new JsonParser();
    JsonObject jo = (JsonObject) jsonParser.parse(response);
    JsonObject jsonDataObject = jo.getAsJsonObject(NODE_OCS).getAsJsonObject(NODE_DATA);

    Gson gson = new Gson();
    Type pushResponseType = new TypeToken<PushResponse>() {
    }.getType();//from ww  w . j  a va2s  .  c o  m

    return gson.fromJson(jsonDataObject, pushResponseType);
}

From source file:com.owncloud.android.lib.resources.notifications.RegisterAccountDeviceForNotificationsOperation.java

License:Open Source License

private boolean isInvalidSessionToken(String response) {
    JsonParser jsonParser = new JsonParser();
    JsonObject jsonObject = (JsonObject) jsonParser.parse(response);
    String message = jsonObject.getAsJsonObject(NODE_OCS).getAsJsonObject(NODE_DATA).get(MESSAGE).getAsString();

    return INVALID_SESSION_TOKEN.equals(message);
}

From source file:com.perl5.lang.perl.idea.run.debugger.PerlStackFrame.java

License:Apache License

@Nullable
@Override/* www .  j  av a  2  s  .  c o  m*/
public XDebuggerEvaluator getEvaluator() {
    return new XDebuggerEvaluator() {
        @Override
        public void evaluate(@NotNull String expression, @NotNull final XEvaluationCallback callback,
                @Nullable XSourcePosition expressionPosition) {
            PerlDebugThread thread = myPerlExecutionStack.getSuspendContext().getDebugThread();

            thread.sendCommandAndGetResponse("e", new PerlEvalRequestDescriptor(expression),
                    new PerlDebuggingTransactionHandler() {
                        @Override
                        public void run(JsonObject jsonObject,
                                JsonDeserializationContext jsonDeserializationContext) {
                            PerlEvalResponseDescriptor descriptor = jsonDeserializationContext.deserialize(
                                    jsonObject.getAsJsonObject("data"), PerlEvalResponseDescriptor.class);

                            if (descriptor == null) {
                                callback.errorOccurred(
                                        "Something bad happened on Perl side. Report to plugin devs.");
                            } else if (descriptor.isError()) {
                                callback.errorOccurred(descriptor.getResult().getValue());
                            } else {
                                callback.evaluated(
                                        new PerlXNamedValue(descriptor.getResult(), PerlStackFrame.this));
                            }
                        }
                    });
        }
    };
}

From source file:com.pinterest.arcee.lease.QuboleLeaseManager.java

License:Apache License

public QuboleClusterBean fromJson(String clusterId, String configuration, String clusterState) {
    if (StringUtils.isEmpty(configuration) || StringUtils.isEmpty(clusterState)) {
        return null;
    }//www  .j a va2 s .co m

    QuboleClusterBean quboleClusterBean = new QuboleClusterBean();
    quboleClusterBean.setClusterId(clusterId);
    JsonParser parser = new JsonParser();
    JsonObject jsonObj = (JsonObject) parser.parse(configuration);
    // http://docs.qubole.com/en/latest/rest-api/cluster_api/get-cluster-information.html
    JsonObject nodeConfiguration = jsonObj.getAsJsonObject("node_configuration");
    quboleClusterBean.setMinSize(nodeConfiguration.getAsJsonPrimitive(MIN_SIZE_TAG).getAsInt());
    quboleClusterBean.setMaxSize(nodeConfiguration.getAsJsonPrimitive(MAX_SIZE_TAG).getAsInt());

    JsonObject stateObj = (JsonObject) parser.parse(clusterState);
    JsonArray array = stateObj.getAsJsonArray("nodes");
    int reservedInsanceCount = 0;
    int spotInstanceCount = 0;
    for (int i = 0; i < array.size(); ++i) {
        JsonObject nodeObject = array.get(i).getAsJsonObject();
        Boolean isSpotInstance = nodeObject.getAsJsonPrimitive("is_spot_instance").getAsBoolean();
        if (isSpotInstance) {
            spotInstanceCount++;
        } else {
            reservedInsanceCount++;
        }
    }
    quboleClusterBean.setRunningReservedInstanceCount(reservedInsanceCount);
    quboleClusterBean.setRunningSpotInstanceCount(spotInstanceCount);
    return quboleClusterBean;
}

From source file:com.pinterest.teletraan.security.UserDataHelper.java

License:Apache License

public String getUsername(String token) throws Exception {
    HTTPClient httpClient = new HTTPClient();
    Map<String, String> params = ImmutableMap.of("access_token", token);
    String jsonPayload = httpClient.get(userDataUrl, params, null, 3);
    JsonObject jsonObject = new JsonParser().parse(jsonPayload).getAsJsonObject();
    jsonObject = jsonObject.getAsJsonObject("user");
    String user = jsonObject.get("username").getAsString();
    LOG.info("Retrieved username " + user + " from token.");
    return user;/*ww  w  .java 2 s .  c  o m*/
}

From source file:com.platzi.silmood.the_fm.io.deserializer.ArtistInfoResponseDeserializer.java

License:Apache License

@Override
public Artist deserialize(JsonElement json, Type typeOfT, JsonDeserializationContext context)
        throws JsonParseException {

    Gson gson = new Gson();

    JsonObject artistData = json.getAsJsonObject().getAsJsonObject(JsonKeys.ARTISTS_ARRAY);
    JsonArray artistImages = artistData.getAsJsonArray(JsonKeys.ARTIST_IMAGES);
    JsonObject artistStats = artistData.getAsJsonObject(JsonKeys.ARTIST_STATS);

    Artist artist = Artist.buildArtistFromJson(artistData);
    artist.extractUrlsFromImagesArray(artistImages);
    artist.extractStatsFromJson(artistStats);

    return artist;
}