List of usage examples for com.google.gson JsonElement isJsonArray
public boolean isJsonArray()
From source file:com.continusec.client.ObjectHash.java
License:Apache License
/** * Calculate the objecthash for a Gson JsonElement object, with a custom redaction prefix string. * @param o the Gson JsonElement to calculated the objecthash for. * @param r the string to use as a prefix to indicate that a string should be treated as a redacted subobject. * @return the objecthash for this object * @throws ContinusecException upon error *//* w ww . j ava 2 s. c om*/ public static final byte[] objectHashWithRedaction(JsonElement o, String r) throws ContinusecException { if (o == null || o.isJsonNull()) { return hashNull(); } else if (o.isJsonArray()) { return hashArray(o.getAsJsonArray(), r); } else if (o.isJsonObject()) { return hashObject(o.getAsJsonObject(), r); } else if (o.isJsonPrimitive()) { JsonPrimitive p = o.getAsJsonPrimitive(); if (p.isBoolean()) { return hashBoolean(p.getAsBoolean()); } else if (p.isNumber()) { return hashDouble(p.getAsDouble()); } else if (p.isString()) { return hashString(p.getAsString(), r); } else { throw new InvalidObjectException(); } } else { throw new InvalidObjectException(); } }
From source file:com.continusec.client.ObjectHash.java
License:Apache License
private static final JsonElement shedObject(JsonObject o, String r) throws ContinusecException { JsonObject rv = new JsonObject(); for (Map.Entry<String, JsonElement> e : o.entrySet()) { JsonElement v = e.getValue(); if (v.isJsonArray()) { JsonArray a = v.getAsJsonArray(); if (a.size() == 2) { rv.add(e.getKey(), shedRedactable(a.get(1), r)); } else { throw new InvalidObjectException(); }/*from ww w.j ava2 s . co m*/ } else if (v.isJsonPrimitive()) { JsonPrimitive p = v.getAsJsonPrimitive(); if (p.isString()) { if (p.getAsString().startsWith(r)) { // all good, but we shed it. } else { throw new InvalidObjectException(); } } else { throw new InvalidObjectException(); } } else { throw new InvalidObjectException(); } } return rv; }
From source file:com.continusec.client.ObjectHash.java
License:Apache License
/** * Strip away object values that are marked as redacted, and switch nonce-tuples back to normal values. * This is useful when an object has been stored with Redactable nonces added, but now it has been retrieved * and normal processing needs to be performed on it. * @param o the Gson JsonElement that contains the redacted elements and nonce-tuples. * @param r the redaction prefix that indicates if a string represents a redacted sub-object. * @return a new cleaned up JsonElement//from www .j a v a 2s.c o m * @throws ContinusecException upon error */ public static final JsonElement shedRedactable(JsonElement o, String r) throws ContinusecException { if (o == null) { return null; } else if (o.isJsonArray()) { return shedArray(o.getAsJsonArray(), r); } else if (o.isJsonObject()) { return shedObject(o.getAsJsonObject(), r); } else { return o; } }
From source file:com.continuuity.loom.macro.Expander.java
License:Apache License
/** * Given a JSON tree, find the element specified by the path (or the root if path is null). In that subtree, * recursively traverse all elements, and expand all String typed right hand side values. If a macro cannot be * expanded due to the cluster object missing certain data, that macro will be left unexpanded. * * @param json A JSON tree// w ww . j ava 2 s.com * @param path the path to expand under * @param cluster the cluster to use for expanding macros. * @param nodes the cluster nodes to use for expanding macros. * @param node the cluster node to use for expanding macros. * @return a new JSON tree if any expansion took place, and the original JSON tree otherwise. * @throws SyntaxException if a macro expression is ill-formed. * @throws IncompleteClusterException if the cluster does not have the meta data to expand all macros. */ public static JsonElement expand(JsonElement json, @Nullable java.util.List<String> path, Cluster cluster, Set<Node> nodes, Node node) throws SyntaxException, IncompleteClusterException { // if path is given, if (path != null && !path.isEmpty()) { String first = path.get(0); if (json.isJsonObject()) { JsonObject object = json.getAsJsonObject(); JsonElement json1 = object.get(first); if (json1 != null) { JsonElement expanded = expand(json1, path.subList(1, path.size()), cluster, nodes, node); if (expanded != json1) { // only construct new json object if actual expansion happened JsonObject object1 = new JsonObject(); for (Map.Entry<String, JsonElement> entry : object.entrySet()) { object1.add(entry.getKey(), entry.getKey().equals(first) ? expanded : entry.getValue()); } return object1; } } } // path was given, but either no corresponding subtree was found or no expansion happened... return json; } if (json.isJsonPrimitive()) { JsonPrimitive primitive = json.getAsJsonPrimitive(); if (primitive.isString()) { String value = primitive.getAsString(); String expanded = expand(value, cluster, nodes, node); if (!expanded.equals(value)) { // only return a new json element if actual expansion happened return new JsonPrimitive(expanded); } } } if (json.isJsonArray()) { JsonArray array = json.getAsJsonArray(); JsonArray array1 = new JsonArray(); boolean expansionHappened = false; for (JsonElement element : array) { JsonElement expanded = expand(element, path, cluster, nodes, node); if (expanded != element) { expansionHappened = true; } array1.add(expanded); } // only return a new json array if actual expansion happened if (expansionHappened) { return array1; } } if (json.isJsonObject()) { JsonObject object = json.getAsJsonObject(); JsonObject object1 = new JsonObject(); boolean expansionHappened = false; for (Map.Entry<String, JsonElement> entry : object.entrySet()) { JsonElement expanded = expand(entry.getValue(), path, cluster, nodes, node); if (expanded != entry.getValue()) { expansionHappened = true; } object1.add(entry.getKey(), expand(entry.getValue(), path, cluster, nodes, node)); } if (expansionHappened) { return object1; } } return json; }
From source file:com.couchbase.cbadmin.assets.NodeGroupList.java
License:Open Source License
public NodeGroupList(JsonObject json) throws RestApiException { JsonElement e = json.get("uri"); if (e == null || e.isJsonPrimitive() == false) { throw new RestApiException("Expected modification URI", json); }//from w w w . ja v a 2 s . c o m assignmentUri = URI.create(e.getAsString()); e = json.get("groups"); if (e == null || e.isJsonArray() == false) { throw new RestApiException("Expected 'groups'", e); } groups = new ArrayList<NodeGroup>(); for (JsonElement groupElem : e.getAsJsonArray()) { if (groupElem.isJsonObject() == false) { throw new RestApiException("Expected object for group", groupElem); } groups.add(new NodeGroup(groupElem.getAsJsonObject())); } }
From source file:com.couchbase.cbadmin.client.ConnectionInfo.java
License:Open Source License
public ConnectionInfo(JsonObject poolsJson) throws RestApiException { JsonElement eTmp; eTmp = poolsJson.get("implementationVersion"); if (eTmp == null) { throw new RestApiException("implementationVersion missing", poolsJson); }/*from w w w . jav a 2 s .co m*/ clusterVersion = eTmp.getAsString(); eTmp = poolsJson.get("pools"); if (eTmp == null || eTmp.isJsonArray() == false) { throw new RestApiException("pools missing", poolsJson); } if (eTmp.getAsJsonArray().size() > 0) { clusterActive = true; eTmp = eTmp.getAsJsonArray().get(0); if (eTmp.isJsonObject() == false) { throw new RestApiException("Expected object in pools entry", eTmp); } eTmp = eTmp.getAsJsonObject().get("uri"); if (eTmp == null || eTmp.isJsonPrimitive() == false) { throw new RestApiException("uri missing or malformed", eTmp); } clusterId = eTmp.getAsString(); } else { clusterActive = false; clusterId = null; } eTmp = poolsJson.get("isAdminCreds"); adminCreds = eTmp != null && eTmp.getAsBoolean(); }
From source file:com.couchbase.cbadmin.client.CouchbaseAdmin.java
License:Open Source License
@Override public Map<String, Bucket> getBuckets() throws RestApiException { JsonElement e; Map<String, Bucket> ret = new HashMap<String, Bucket>(); try {/* www . j a v a 2s . c o m*/ e = getJson(P_BUCKETS); } catch (IOException ex) { throw new RestApiException(ex); } JsonArray arr; if (!e.isJsonArray()) { throw new RestApiException("Expected JsonObject", e); } arr = e.getAsJsonArray(); for (int i = 0; i < arr.size(); i++) { JsonElement tmpElem = arr.get(i); if (!tmpElem.isJsonObject()) { throw new RestApiException("Expected JsonObject", tmpElem); } Bucket bucket = new Bucket(tmpElem.getAsJsonObject()); ret.put(bucket.getName(), bucket); } return ret; }
From source file:com.couchbase.plugin.basement.api.Client.java
License:Open Source License
private void addJsonElement(String key, JsonElement value, HashMap<String, Object> map) { if (value.isJsonArray()) { JsonArray jsonArray = value.getAsJsonArray(); ArrayList listAsValue = new ArrayList<HashMap<String, Object>>(); HashMap<String, Object> listElementMap = new HashMap<String, Object>(); Iterator<JsonElement> jsonArrayIterator = jsonArray.iterator(); while (jsonArrayIterator.hasNext()) { JsonElement jsonElement = jsonArrayIterator.next(); listAsValue.add(parse(jsonElement.toString())); }/* ww w . j av a 2 s. c om*/ map.put(key, listAsValue); } else if (value.isJsonPrimitive()) { // check the type using JsonPrimitive // TODO: check all types JsonPrimitive jsonPrimitive = value.getAsJsonPrimitive(); if (jsonPrimitive.isNumber()) { map.put(key, jsonPrimitive.getAsInt()); } else { map.put(key, jsonPrimitive.getAsString()); } } else { map.put(key, parse(value.toString())); } }
From source file:com.crowdmap.java.sdk.json.UsersDeserializer.java
License:Open Source License
@Override public List<User> deserialize(JsonElement json, Type typeOfT, JsonDeserializationContext context) throws JsonParseException { List<User> users = new ArrayList<User>(); if (json.isJsonArray()) { for (JsonElement e : json.getAsJsonArray()) { users.add((User) context.deserialize(e, User.class)); }//from w ww. j a v a2s .c o m } else if (json.isJsonObject()) { users.add((User) context.deserialize(json, User.class)); } else { throw new CrowdmapException("Unexpected JSON type" + json.getClass()); } return users; }
From source file:com.crypticbit.diff.demo.swing.contacts.JsonJTreeNode.java
License:Apache License
/** * @param fieldName/*from w w w .j a va 2 s. c om*/ * - name of field if applicable or null * @param index * - index of element in the array or -1 if not part of an array * @param jsonElement * - element to represent */ public JsonJTreeNode(String fieldName, int index, JsonElement jsonElement) { this.index = index; this.fieldName = fieldName; if (jsonElement.isJsonArray()) { this.dataType = DataType.ARRAY; this.value = jsonElement.toString(); populateChildren(jsonElement); } else if (jsonElement.isJsonObject()) { this.dataType = DataType.OBJECT; this.value = jsonElement.toString(); populateChildren(jsonElement); } else if (jsonElement.isJsonPrimitive()) { this.dataType = DataType.VALUE; this.value = jsonElement.toString(); } else if (jsonElement.isJsonNull()) { this.dataType = DataType.VALUE; this.value = jsonElement.toString(); } else { throw new IllegalArgumentException("jsonElement is an unknown element type."); } }