Example usage for com.google.gson JsonElement isJsonObject

List of usage examples for com.google.gson JsonElement isJsonObject

Introduction

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

Prototype

public boolean isJsonObject() 

Source Link

Document

provides check for verifying if this element is a Json object or not.

Usage

From source file:com.apcb.utils.utils.HtmlTemplateReader.java

private SectionMatch readJson(JsonElement jsonElement, String from, SectionMatch sectionMatch) {
    //log.info(jsonElement.toString());
    //SectionMatch sectionMatchChild = new SectionMatch(from);
    if (jsonElement.isJsonArray()) {
        //log.info("JsonArray");
        JsonArray jsonArray = jsonElement.getAsJsonArray();
        for (int i = 0; i < jsonArray.size(); i++) {
            SectionMatch sectionMatchArrayLevel = new SectionMatch(sectionMatch.getName());
            sectionMatchArrayLevel.setIndex(i);
            sectionMatch.putChildens(readJson(jsonArray.get(i), from + "[" + i + "]", sectionMatchArrayLevel));
        }//from  w w w  . j a  va 2 s  .c  o m
    } else if (jsonElement.isJsonObject()) {
        //log.info("JsonObject");
        JsonObject jsonObject = jsonElement.getAsJsonObject(); //since you know it's a JsonObject
        Set<Map.Entry<String, JsonElement>> entries = jsonObject.entrySet();//will return members of your object
        for (Map.Entry<String, JsonElement> entry : entries) {
            //log.info(entry.getKey());
            sectionMatch.putChildens(readJson(jsonObject.get(entry.getKey()), from + "." + entry.getKey(),
                    new SectionMatch(entry.getKey())));
        }
    } else if (jsonElement.isJsonNull()) {
        log.info("JsonNull");
    } else if (jsonElement.isJsonPrimitive()) {
        //log.info("JsonPrimitive");
        String jsonVal = jsonElement.getAsString();
        //from+= "."+entry.getKey(); 
        sectionMatch.setValue(jsonVal);
        log.info(from + "=" + jsonVal);
    }

    return sectionMatch;
}

From source file:com.appdynamics.extensions.couchbase.CouchBaseWrapper.java

License:Apache License

/**
 * Gathers Cluster and Node stats. Cluster stats as Map of CLUSTER_STATS_KEY
 * and Map of MetricName, MetricValue and NodeStats as Map of NodeName, Map
 * of MetricName, MetricValue//from  ww w.  j av  a 2 s  .c om
 * 
 * @param httpClient
 * @return
 */
public Map<String, Map<String, Double>> gatherClusterNodeMetrics(SimpleHttpClient httpClient) {
    JsonElement clusterNodeResponse = getResponse(httpClient, CLUSTER_NODE_URI);
    Map<String, Map<String, Double>> clusterNodeMetrics = new HashMap<String, Map<String, Double>>();
    if (clusterNodeResponse != null && clusterNodeResponse.isJsonObject()) {
        JsonObject clusterNodeJsonObject = clusterNodeResponse.getAsJsonObject();
        JsonObject storageTotals = clusterNodeJsonObject.getAsJsonObject("storageTotals");
        clusterNodeMetrics = populateClusterMetrics(clusterNodeMetrics, storageTotals);

        JsonArray nodes = (JsonArray) clusterNodeJsonObject.get("nodes");
        clusterNodeMetrics.putAll(populateNodeMetrics(nodes));
    }
    return clusterNodeMetrics;
}

From source file:com.appdynamics.extensions.couchbase.CouchBaseWrapper.java

License:Apache License

/**
 * Gathers Bucket Replication stats from /pools/default/buckets/<bucket_name>/stats
 * /* w  w w  . j av a2 s .  com*/
 * Response contains all other stats but this only filters any stats that starts with 'replications', 
 * e.g. replications/03cd3332434401c64594f47eeeabbb79/beer-sample/gamesim-sample/wtavg_meta_latency
 * 
 * @param buckets
 * @param httpClient
 * @return Map<String, Map<String, Double>>
 */
public Map<String, Map<String, Double>> gatherOtherBucketMetrics(Set<String> buckets,
        SimpleHttpClient httpClient) {
    Map<String, Map<String, Double>> otherBucketMetrics = new HashMap<String, Map<String, Double>>();

    if (buckets != null) {
        for (String bucket : buckets) {
            JsonElement bucketResponse = getResponse(httpClient, String.format(BUCKET_STATS_URI, bucket));

            if (bucketResponse != null && bucketResponse.isJsonObject()) {
                JsonObject bucketStats = bucketResponse.getAsJsonObject();
                JsonObject op = bucketStats.getAsJsonObject("op");
                JsonObject samples = op.getAsJsonObject("samples");
                populateOtherBucketMetrics(bucket, samples.entrySet().iterator(), otherBucketMetrics);
            }
        }
    }

    return otherBucketMetrics;
}

From source file:com.arangodb.entity.EntityDeserializers.java

License:Apache License

private static JsonObject getFirstResultAsJsonObject(JsonObject obj) {
    if (obj.has("result")) {
        if (obj.get("result").isJsonArray()) {
            JsonArray result = obj.getAsJsonArray("result");

            if (result.size() > 0) {
                JsonElement jsonElement = result.get(0);
                if (jsonElement.isJsonObject()) {
                    return jsonElement.getAsJsonObject();
                }/*from  w  w w.  j a  va2  s .co m*/
            }
        } else if (obj.get("result").isJsonObject()) {
            return obj.getAsJsonObject("result");
        }

    }
    return null;
}

From source file:com.arangodb.impl.InternalDocumentDriverImpl.java

License:Apache License

private <T> DocumentEntity<T> _createDocument(String database, String collectionName, String documentKey,
        T value, Boolean createCollection, Boolean waitForSync, boolean raw) throws ArangoException {

    validateCollectionName(collectionName);

    String body;/*from   w ww  .j a  v  a2 s.  c om*/
    if (raw) {
        body = value.toString();
    } else if (documentKey != null) {
        JsonElement elem = EntityFactory.toJsonElement(value, false);
        if (elem.isJsonObject()) {
            elem.getAsJsonObject().addProperty(BaseDocument.KEY, documentKey);
        }
        body = EntityFactory.toJsonString(elem);
    } else {
        body = EntityFactory.toJsonString(value);
    }

    HttpResponseEntity res = httpManager.doPost(createEndpointUrl(database, "/_api/document"),
            new MapBuilder().put("collection", collectionName).put("createCollection", createCollection)
                    .put("waitForSync", waitForSync).get(),
            body);

    @SuppressWarnings("unchecked")
    DocumentEntity<T> result = createEntity(res, DocumentEntity.class);

    annotationHandler.updateDocumentAttributes(value, result.getDocumentRevision(), result.getDocumentHandle(),
            result.getDocumentKey());

    result.setEntity(value);
    return result;
}

From source file:com.arangodb.impl.InternalGraphDriverImpl.java

License:Apache License

@SuppressWarnings("unchecked")
@Override//from ww  w .  ja  v  a 2s .  c  o  m
public <T> VertexEntity<T> createVertex(String database, String graphName, String collectionName, String key,
        T vertex, Boolean waitForSync) throws ArangoException {

    JsonObject obj;
    if (vertex == null) {
        obj = new JsonObject();
    } else {
        JsonElement elem = EntityFactory.toJsonElement(vertex, false);
        if (elem.isJsonObject()) {
            obj = elem.getAsJsonObject();
        } else {
            throw new IllegalArgumentException("vertex need object type(not support array, primitive, etc..).");
        }
    }
    if (key != null) {
        obj.addProperty("_key", key);
    }

    validateCollectionName(graphName);
    HttpResponseEntity res = httpManager.doPost(
            createEndpointUrl(database, "/_api/gharial", StringUtils.encodeUrl(graphName), "vertex",
                    StringUtils.encodeUrl(collectionName)),
            new MapBuilder().put("waitForSync", waitForSync).get(), EntityFactory.toJsonString(obj));

    if (!res.isJsonResponse()) {
        throw new ArangoException("unknown error");
    }

    VertexEntity<T> result = createEntity(res, VertexEntity.class, vertex.getClass());
    if (vertex != null) {
        result.setEntity(vertex);
        annotationHandler.updateDocumentAttributes(result.getEntity(), result.getDocumentRevision(),
                result.getDocumentHandle(), result.getDocumentKey());
    }
    return result;
}

From source file:com.arangodb.impl.InternalGraphDriverImpl.java

License:Apache License

@SuppressWarnings("unchecked")
@Override//from  w ww .  j av  a2s .  com
public <T> EdgeEntity<T> createEdge(String database, String graphName, String edgeCollectionName, String key,
        String fromHandle, String toHandle, T value, Boolean waitForSync) throws ArangoException {

    JsonObject obj;
    if (value == null) {
        obj = new JsonObject();
    } else {
        JsonElement elem = EntityFactory.toJsonElement(value, false);
        if (elem.isJsonObject()) {
            obj = elem.getAsJsonObject();
        } else {
            throw new IllegalArgumentException("value need object type(not support array, primitive, etc..).");
        }
    }
    if (key != null) {
        obj.addProperty("_key", key);
    }
    obj.addProperty("_from", fromHandle);
    obj.addProperty("_to", toHandle);

    validateCollectionName(graphName);
    HttpResponseEntity res = httpManager.doPost(
            createEndpointUrl(database, "/_api/gharial", StringUtils.encodeUrl(graphName), "/edge",
                    StringUtils.encodeUrl(edgeCollectionName)),
            new MapBuilder().put("waitForSync", waitForSync).get(), EntityFactory.toJsonString(obj));

    EdgeEntity<T> entity = createEntity(res, EdgeEntity.class, value == null ? null : value.getClass());
    if (value != null) {
        entity.setEntity(value);
        annotationHandler.updateEdgeAttributes(value, entity.getDocumentRevision(), entity.getDocumentHandle(),
                entity.getDocumentKey(), fromHandle, toHandle);
    }

    entity.setFromVertexHandle(fromHandle);
    entity.setToVertexHandle(toHandle);
    return entity;
}

From source file:com.asakusafw.lang.inspection.json.NodeAdapter.java

License:Apache License

@Override
public InspectionNode deserialize(JsonElement json, Type typeOfT, JsonDeserializationContext context)
        throws JsonParseException {
    if (json.isJsonObject() == false) {
        throw new JsonParseException("node must be an object"); //$NON-NLS-1$
    }//from w  w w  .  ja va 2s .c o m
    JsonObject object = (JsonObject) json;
    String id = object.get(KEY_ID).getAsString();
    String title = object.get(KEY_TITLE).getAsString();
    List<Port> inputs = context.deserialize(object.get(KEY_INPUTS), TYPE_PORTS);
    List<Port> outputs = context.deserialize(object.get(KEY_OUTPUTS), TYPE_PORTS);
    Map<String, String> properties = context.deserialize(object.get(KEY_PROPERTIES), TYPE_PROPERTIES);
    List<InspectionNode> elements = context.deserialize(object.get(KEY_ELEMENTS), TYPE_NODES);

    InspectionNode result = new InspectionNode(id, title);
    put(inputs, result.getInputs());
    put(outputs, result.getOutputs());
    result.getProperties().putAll(properties);
    put(elements, result.getElements());
    return result;
}

From source file:com.asakusafw.lang.inspection.json.PortAdapter.java

License:Apache License

@Override
public InspectionNode.Port deserialize(JsonElement json, Type typeOfT, JsonDeserializationContext context)
        throws JsonParseException {
    if (json.isJsonObject() == false) {
        throw new JsonParseException("port must be an object"); //$NON-NLS-1$
    }//from   www  . j  av  a 2 s .c  o m
    JsonObject object = (JsonObject) json;
    String id = object.get(KEY_ID).getAsString();
    Map<String, String> properties = context.deserialize(object.get(KEY_PROPERTIES), TYPE_PROPERTIES);
    Set<PortReference> opposites = context.deserialize(object.get(KEY_OPPOSITES), TYPE_REFERENCES);

    InspectionNode.Port result = new InspectionNode.Port(id);
    result.getProperties().putAll(properties);
    result.getOpposites().addAll(opposites);
    return result;
}

From source file:com.atlauncher.data.mojang.MojangArgumentsTypeAdapter.java

License:Open Source License

@Override
public MojangArguments deserialize(JsonElement json, Type type, JsonDeserializationContext context)
        throws JsonParseException {
    List<ArgumentRule> game = new ArrayList<ArgumentRule>();
    List<ArgumentRule> jvm = new ArrayList<ArgumentRule>();

    final JsonObject rootJsonObject = json.getAsJsonObject();
    final JsonArray gameArray = rootJsonObject.getAsJsonArray("game");
    final JsonArray jvmArray = rootJsonObject.getAsJsonArray("jvm");

    for (JsonElement gameArg : gameArray) {
        if (gameArg.isJsonObject()) {
            JsonObject argument = gameArg.getAsJsonObject();
            game.add(Gsons.DEFAULT_ALT.fromJson(argument, ArgumentRule.class));
        } else {/*from   w ww.j  av  a 2 s.c  o m*/
            String argument = gameArg.getAsString();
            game.add(new ArgumentRule(null, argument));
        }
    }

    for (JsonElement gameArg : jvmArray) {
        if (gameArg.isJsonObject()) {
            JsonObject argument = gameArg.getAsJsonObject();
            jvm.add(Gsons.DEFAULT_ALT.fromJson(argument, ArgumentRule.class));
        } else {
            String argument = gameArg.getAsString();
            jvm.add(new ArgumentRule(null, argument));
        }
    }

    return new MojangArguments(game, jvm);
}