List of usage examples for com.google.gson JsonElement isJsonPrimitive
public boolean isJsonPrimitive()
From source file:com.ccc.crest.core.cache.crest.schema.endpoint.EndpointCollection.java
License:Open Source License
@Override public EndpointCollection deserialize(JsonElement json, Type typeOfT, JsonDeserializationContext context) throws JsonParseException { JsonObject topObj = (JsonObject) json; Set<Entry<String, JsonElement>> topSet = topObj.entrySet(); Iterator<Entry<String, JsonElement>> topIter = topSet.iterator(); do {/* ww w.j a v a2 s .c om*/ if (!topIter.hasNext()) break; Entry<String, JsonElement> topEntry = topIter.next(); String topKey = topEntry.getKey(); JsonElement topElement = topEntry.getValue(); if (topKey.equals(UserCountKey)) { userCount = Long.parseLong(topElement.getAsString()); continue; } if (topKey.equals(UserCountStrKey)) continue; if (topKey.equals(ServerVersionKey)) { serverVersion = topElement.getAsString(); continue; } if (topKey.equals(ServerNameKey)) { serverName = topElement.getAsString(); continue; } if (topKey.equals(ServerStatusKey)) { serviceStatus = topElement.getAsString(); continue; } // if its not a top object level known variable from above list, it must be a group object if (topElement.isJsonPrimitive()) { log.warn("unexpected key: " + topKey + " = " + topObj.toString()); continue; } if (!topElement.isJsonObject()) { log.warn("expected an object: " + topKey + " = " + topObj.toString()); continue; } // first pass you should have a group in the topElement String groupName = topKey; EndpointGroup endpointGroup = new EndpointGroup(groupName); callGroups.add(endpointGroup); Set<Entry<String, JsonElement>> groupSet = topElement.getAsJsonObject().entrySet(); Iterator<Entry<String, JsonElement>> groupIter = groupSet.iterator(); do { if (!groupIter.hasNext()) break; Entry<String, JsonElement> groupEntry = groupIter.next(); // expecting a primitive href here String endpointName = groupEntry.getKey(); JsonElement hrefElement = groupEntry.getValue(); if (hrefElement.isJsonObject()) { JsonObject groupChildObj = (JsonObject) hrefElement; Set<Entry<String, JsonElement>> groupChildSet = groupChildObj.entrySet(); Iterator<Entry<String, JsonElement>> groupChildIter = groupChildSet.iterator(); if (!groupChildIter.hasNext()) break; Entry<String, JsonElement> groupChildEntry = groupChildIter.next(); String groupChildKey = groupChildEntry.getKey(); JsonElement groupChildElement = groupChildEntry.getValue(); endpointGroup.addEndpoint(new CrestEndpoint(endpointName, groupChildElement.getAsString())); continue; } // expect an object with href in it if (!hrefElement.isJsonPrimitive()) { log.warn("expected a primitive after group: " + groupName + " = " + hrefElement.toString()); continue; } endpointGroup.addEndpoint(new CrestEndpoint(endpointName, hrefElement.getAsString())); break; } while (true); } while (true); return this; }
From source file:com.chatwing.whitelabel.managers.ApiManagerImpl.java
License:Apache License
protected String appendParams(Object params) { StringBuilder builder = new StringBuilder(); builder.append("?"); Gson gson = new Gson(); JsonObject gsonObject = gson.toJsonTree(params).getAsJsonObject(); for (Map.Entry<String, JsonElement> entry : gsonObject.entrySet()) { JsonElement value = entry.getValue(); if (value != null && value.isJsonPrimitive()) { try { builder.append(URLEncoder.encode(entry.getKey(), "UTF-8")); builder.append("="); builder.append(URLEncoder.encode(value.getAsString(), "UTF-8")); builder.append("&"); } catch (UnsupportedEncodingException e) { e.printStackTrace();//from w ww. jav a 2 s .c o m } } } return builder.toString(); }
From source file:com.claresco.tinman.json.XapiActivityDefinitionJson.java
License:Open Source License
@Override public XapiActivityDefinition deserialize(JsonElement arg0, Type arg1, JsonDeserializationContext arg2) throws JsonParseException { if (JsonUtility.isJsonElementNull(arg0)) { return null; }/*from www . ja va 2 s.c o m*/ JsonObject theActivityDefinition = JsonUtility.convertJsonElementToJsonObject(arg0); // Construct the name XapiLanguageMap theName = JsonUtility.constructLanguageMap(theActivityDefinition, "name"); // Construct the description XapiLanguageMap theDescription = JsonUtility.constructLanguageMap(theActivityDefinition, "description"); String theType = JsonUtility.getElementAsString(theActivityDefinition, "type"); String theMoreInfo = JsonUtility.getElementAsString(theActivityDefinition, "moreInfo"); XapiInteraction theInteraction = null; XapiExtension theExtension = null; // If activity has interaction, delegate it if (theType != null && JsonUtility.isActivityOfTypeInteraction(theType)) { theInteraction = JsonUtility.delegateDeserialization(arg2, arg0, XapiInteraction.class); } // Check for extension if (JsonUtility.hasElement(theActivityDefinition, "extensions")) { JsonElement theExtJson = JsonUtility.get(theActivityDefinition, "extensions"); JsonObject extJsonMap = JsonUtility.convertJsonElementToJsonObject(theExtJson); theExtension = new XapiExtension(); for (Map.Entry<String, JsonElement> entry : extJsonMap.entrySet()) { JsonElement theValue = entry.getValue(); if (theValue.isJsonPrimitive()) { theExtension.add(entry.getKey(), entry.getValue().getAsString()); } else { theExtension.add(entry.getKey(), entry.getValue().toString()); } } } if (theExtension != null && theExtension.isEmpty()) { return new XapiActivityDefinition(theName, theDescription, theType, theMoreInfo, theInteraction, null); } return new XapiActivityDefinition(theName, theDescription, theType, theMoreInfo, theInteraction, theExtension); }
From source file:com.claresco.tinman.json.XapiExtensionsJson.java
License:Open Source License
@Override public XapiExtension deserialize(JsonElement arg0, Type arg1, JsonDeserializationContext arg2) throws JsonParseException { JsonObject extensionJsonMap = JsonUtility.convertJsonElementToJsonObject(arg0); XapiExtension theExtension = new XapiExtension(); for (Map.Entry<String, JsonElement> entry : extensionJsonMap.entrySet()) { JsonElement theValue = entry.getValue(); if (theValue.isJsonPrimitive()) { theExtension.add(entry.getKey(), entry.getValue().getAsString()); } else {/* w w w .j a v a2 s . c o m*/ theExtension.add(entry.getKey(), entry.getValue().toString()); } } return theExtension; }
From source file:com.cloudbase.CBNaturalDeserializer.java
License:Open Source License
public Object deserialize(JsonElement json, Type typeOfT, JsonDeserializationContext context) { if (json.isJsonNull()) return null; else if (json.isJsonPrimitive()) return handlePrimitive(json.getAsJsonPrimitive()); else if (json.isJsonArray()) return handleArray(json.getAsJsonArray(), context); else/*ww w.j a v a2 s . c o m*/ return handleObject(json.getAsJsonObject(), context); }
From source file:com.collective.celos.ci.testing.fixtures.compare.json.JsonElementConstructor.java
License:Apache License
private JsonElement deepCopy(String path, JsonElement el, Set<String> ignorePaths) { if (el.isJsonArray()) { return deepCopyJsonArray(path, el.getAsJsonArray(), ignorePaths); }/*ww w. j av a2s. c om*/ if (el.isJsonObject()) { return deepCopyJsonObject(path, el.getAsJsonObject(), ignorePaths); } if (el.isJsonPrimitive() || el.isJsonNull()) { return el; } throw new IllegalStateException( "JSONElement should be either Array, Object, Primitive or Null. So you cannot get here"); }
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 *///from ww w.ja v a 2 s. com 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 w w w . java 2 s . c om*/ } 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.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/*from w w w .ja va 2 s.co m*/ * @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.continuuity.weave.internal.json.JsonUtils.java
License:Apache License
/** * Returns a String representation of the given property. *//*from w w w . j a v a 2 s.c om*/ public static String getAsString(JsonObject json, String property) { JsonElement jsonElement = json.get(property); if (jsonElement.isJsonNull()) { return null; } if (jsonElement.isJsonPrimitive()) { return jsonElement.getAsString(); } return jsonElement.toString(); }