List of usage examples for com.google.gson JsonElement isJsonPrimitive
public boolean isJsonPrimitive()
From source file:org.ppojo.data.CopyStyleDataSerializer.java
License:Apache License
private CopyStyleData ParseStyleJsonObject(JsonObject asObject, int memberIndex) { CopyStyleData copyStyleData = new CopyStyleData(); JsonElement styleMember = null; JsonElement methodNameMember = null; for (Map.Entry<String, JsonElement> elementEntry : asObject.entrySet()) { switch (elementEntry.getKey()) { case "style": styleMember = elementEntry.getValue(); break; case "methodName": methodNameMember = elementEntry.getValue(); break; default:// ww w . j a va 2s .c o m throw new JsonParseException("Invalid member for array element pojoCopyStyles[" + memberIndex + "], unsupported field " + elementEntry.getKey() + ". Only supports fields style and methodName, in " + getDeserializeFilePath()); } } if (styleMember == null) throw new JsonParseException("Invalid element in option pojoCopyStyles[" + memberIndex + "], required field: style is missing, in " + getDeserializeFilePath()); if (!(styleMember.isJsonPrimitive() && styleMember.getAsJsonPrimitive().isString())) throw new JsonParseException("Invalid element type for option pojoCopyStyles[" + memberIndex + "] style field, expected String got " + JsonElementTypes.getType(styleMember) + ", in " + getDeserializeFilePath()); if (methodNameMember != null && !(methodNameMember.isJsonPrimitive() && methodNameMember.getAsJsonPrimitive().isString())) throw new JsonParseException("Invalid element type for option pojoCopyStyles[" + memberIndex + "] methodName field, expected String got " + JsonElementTypes.getType(methodNameMember) + ", in " + getDeserializeFilePath()); copyStyleData.style = parseStyleType(styleMember.getAsString(), memberIndex); if (methodNameMember != null) copyStyleData.methodName = EmptyIfNull(methodNameMember.getAsString()); return copyStyleData; }
From source file:org.ppojo.data.JsonElementTypes.java
License:Apache License
public static JsonElementTypes getType(JsonElement json) { if (json.isJsonObject()) return JsonElementTypes.JsonObject; if (json.isJsonArray()) return JsonElementTypes.JsonArray; if (json.isJsonNull()) return JsonElementTypes.JsonNull; if (json.isJsonPrimitive()) { JsonPrimitive jsonPrimitive = json.getAsJsonPrimitive(); if (jsonPrimitive.isBoolean()) return JsonElementTypes.Boolean; if (jsonPrimitive.isNumber()) return JsonElementTypes.Number; if (jsonPrimitive.isString()) return JsonElementTypes.String; }/* ww w . j av a2 s .com*/ return JsonElementTypes.Unknown; }
From source file:org.projectbuendia.client.net.OpenMrsErrorListener.java
License:Apache License
/** Parsing the json formatted error response received from the OpenMRS server **/ public String extractMessageFromJson(String json) { String message = null;//from w ww . j av a2 s.c o m try { JsonObject result = new JsonParser().parse(json).getAsJsonObject(); if (result.has("error")) { JsonObject errorObject = result.getAsJsonObject("error"); JsonElement element = errorObject.get("message"); if (element == null || element.isJsonNull()) { element = errorObject.get("code"); } if (element != null && element.isJsonPrimitive()) { message = element.getAsString(); } } } catch (JsonParseException | IllegalStateException | UnsupportedOperationException e) { LOG.w("Problem parsing error message: " + e.getMessage()); } return message; }
From source file:org.projectbuendia.client.net.OpenMrsServer.java
License:Apache License
/** * Wraps an ErrorListener so as to extract an error message from the JSON * content of a response, if possible.//from w ww .jav a 2 s . com * @param errorListener An error listener. * @return A new error listener that tries to pass a more meaningful message * to the original errorListener. */ private Response.ErrorListener wrapErrorListener(final Response.ErrorListener errorListener) { return new Response.ErrorListener() { @Override public void onErrorResponse(VolleyError error) { String message = error.getMessage(); try { if (error.networkResponse != null && error.networkResponse.data != null) { String text = new String(error.networkResponse.data); JsonObject result = new JsonParser().parse(text).getAsJsonObject(); if (result.has("error")) { JsonObject errorObject = result.getAsJsonObject("error"); JsonElement element = errorObject.get("message"); if (element == null || element.isJsonNull()) { element = errorObject.get("code"); } if (element != null && element.isJsonPrimitive()) { message = element.getAsString(); } } } } catch (JsonParseException | IllegalStateException | UnsupportedOperationException e) { e.printStackTrace(); } errorListener.onErrorResponse(new VolleyError(message, error)); } }; }
From source file:org.qcert.camp.translator.Rule2CAMP.java
License:Open Source License
/** * Make a single data object given its type and a JsonObject containing its attributes * @param type the type// www . j av a2 s . c o m * @param attributes the attributes * @return the constructed object instance * @throws Exception */ private static Object makeDataObject(String type, JsonObject attributes) throws Exception { Object ans = Class.forName(type).newInstance(); for (Entry<String, JsonElement> attribute : attributes.entrySet()) { JsonElement value = attribute.getValue(); if (value.isJsonPrimitive()) setAttribute(ans, attribute.getKey(), valueFromJson(value.getAsJsonPrimitive())); else if (value.isJsonArray()) { JsonArray array = value.getAsJsonArray(); List<Object> collection = new ArrayList<>(); setAttribute(ans, attribute.getKey(), collection); for (JsonElement element : array) { if (element.isJsonPrimitive()) collection.add(valueFromJson((element.getAsJsonPrimitive()))); else throw new UnsupportedOperationException( "Only handling collections of primitive type at this time"); } } } return ans; }
From source file:org.qcert.runtime.DataComparator.java
License:Apache License
private static DType getType(JsonElement obj) { if (obj == null) { return DType.DT_JNULL; } else if (obj.isJsonNull()) { return DType.DT_NULL; } else if (obj.isJsonPrimitive()) { final JsonPrimitive prim = obj.getAsJsonPrimitive(); if (prim.isBoolean()) { return DType.DT_BOOL; } else if (prim.isString()) { return DType.DT_STRING; } else if (prim.isNumber()) { final Number num = prim.getAsNumber(); if (num instanceof LazilyParsedNumber) { return DType.DT_LAZYNUM; } else if (num instanceof Long || num instanceof Short || num instanceof Integer) { return DType.DT_LONG; } else if (num instanceof Double || num instanceof Short || num instanceof Float) { return DType.DT_DOUBLE; } else { throw new RuntimeException( "Unknown primitive json number type: " + num + " of type " + num.getClass()); }/*from w w w.j a v a 2 s . c om*/ } else { throw new RuntimeException("Unknown primitive json type: " + prim); } } else if (obj.isJsonArray()) { return DType.DT_COLL; } else if (obj.isJsonObject()) { return DType.DT_REC; } else { throw new RuntimeException("Unknown json type: " + obj + " of type " + obj.getClass()); } }
From source file:org.qcert.runtime.UnaryOperators.java
License:Apache License
private static void tostring(StringBuilder sb, JsonElement e) { if (e == null || e.isJsonNull()) { sb.append("null"); } else if (e.isJsonPrimitive()) { tostring(sb, e.getAsJsonPrimitive()); } else if (e.isJsonArray()) { tostring(sb, e.getAsJsonArray()); } else if (e.isJsonObject()) { tostring(sb, e.getAsJsonObject()); } else {//from ww w .j a v a 2 s. c om sb.append(e.toString()); } }
From source file:org.qcert.util.SchemaUtil.java
License:Apache License
/** * Make a Type from a JsonElement representing a Type. * TODO support for embedded objects (other than collections) is incomplete. * @param type the JsonElement to be made into a Type * @param brandMap the map from type names to brands in the event of an embedded objectl * @return the Type/* www . j av a 2s. co m*/ */ private static Type makeType(JsonElement type) { if (type.isJsonPrimitive() && type.getAsJsonPrimitive().isString()) { return new PrimitiveType(type.getAsJsonPrimitive().getAsString()); } // Other than primitive types and object type references we only support the $coll convention at this time JsonElement coll = type.getAsJsonObject().get("$coll"); if (coll == null) throw new UnsupportedOperationException("Don't yet know how to handle encoded type " + type); Type element = makeType(coll); return new ListType(element); }
From source file:org.quartzpowered.protocol.data.chat.component.serialize.ComponentSerializer.java
License:Open Source License
@Override public BaseComponent deserialize(JsonElement json, Type typeOfT, JsonDeserializationContext context) throws JsonParseException { if (json.isJsonPrimitive()) { return new TextComponent(json.getAsString()); }/*from w ww . ja v a 2 s . c o m*/ JsonObject object = json.getAsJsonObject(); if (object.has("translate")) { return context.deserialize(json, TranslatableComponent.class); } return context.deserialize(json, TextComponent.class); }
From source file:org.rapla.rest.jsonpatch.mergepatch.server.ObjectMergePatch.java
License:LGPL
@Override public JsonElement apply(final JsonElement input) throws JsonPatchException { if (!input.isJsonObject()) return mapToNode(fields); final Map<String, JsonElement> map = asMap(input); // Remove all entries which must be removed map.keySet().removeAll(removals);/*from w ww . j av a 2 s . c om*/ // Now cycle through what is left String memberName; JsonElement patchNode; for (final Map.Entry<String, JsonElement> entry : map.entrySet()) { memberName = entry.getKey(); patchNode = fields.get(memberName); // Leave untouched if no mention in the patch if (patchNode == null) continue; // If the patch node is a primitive type, replace in the result. // Reminder: there cannot be a JSON null anymore if (patchNode.isJsonPrimitive()) { entry.setValue(patchNode); // no need for .deepCopy() continue; } final JsonMergePatch patch = JsonMergePatch.fromJson(patchNode); entry.setValue(patch.apply(entry.getValue())); } // Finally, if there are members in the patch not present in the input, // fill in members for (final String key : difference(fields.keySet(), map.keySet())) map.put(key, clearNulls(fields.get(key))); return mapToNode(map); }