List of usage examples for com.google.gson JsonElement toString
@Override
public String toString()
From source file:com.jayway.jsonpath.internal.spi.json.GsonJsonProvider.java
License:Apache License
@Override public int length(Object obj) { if (isArray(obj)) { return toJsonArray(obj).size(); } else if (isMap(obj)) { return toJsonObject(obj).entrySet().size(); } else {//from w w w . j ava 2 s .c o m if (obj instanceof JsonElement) { JsonElement element = toJsonElement(obj); if (element.isJsonPrimitive()) { return element.toString().length(); } } } throw new JsonPathException( "length operation can not applied to " + obj != null ? obj.getClass().getName() : "null"); }
From source file:com.jayway.jsonpath.spi.json.GsonJsonProvider.java
License:Apache License
@Override public int length(final Object obj) { if (isArray(obj)) { return toJsonArray(obj).size(); } else if (isMap(obj)) { return toJsonObject(obj).entrySet().size(); } else {//from ww w .j a v a2 s . co m if (obj instanceof JsonElement) { JsonElement element = toJsonElement(obj); if (element.isJsonPrimitive()) { return element.toString().length(); } } } throw new JsonPathException( "length operation can not applied to " + obj != null ? obj.getClass().getName() : "null"); }
From source file:com.king.tratt.FunctionFactoryProvider.java
License:Apache License
static FunctionFactory jsonField() { return new FunctionFactory() { Value pathValue;/* w ww. ja v a 2 s . com*/ Value jsonValue; JsonParser jsonParser; @Override public String getName() { return "jsonfield"; } @Override public int getNumberOfArguments() { return 2; } @Override public Value create(List<Value> arguments) { pathValue = arguments.get(0); jsonValue = arguments.get(1); jsonParser = new JsonParser(); return new Value() { @Override public String toString() { return String.format("jsonfield('%s', '%s')", pathValue, jsonValue); } @Override public String toDebugString(Event e, Context context) { return util.format(e, context, "[[source:jsonfield('~v', '~v')]]~q", pathValue, jsonValue, this); } @Override protected Object getImp(Event e, Context context) { String path = pathValue.asString(e, context); String json = jsonValue.asString(e, context); String result = null; try { result = getJsonFieldValue(path, json); } catch (Throwable t) { result = "[@ERROR malformed json string]"; } return util.parseSupportedType(result); } private String getJsonFieldValue(String path, String json) { JsonElement jsonElem = jsonParser.parse(json); for (String subPath : path.split("\\.")) { JsonObject jsonObj = jsonElem.getAsJsonObject(); jsonElem = jsonObj.get(subPath); if (jsonElem == null) { return String.format("[@ERROR incorrect json path: '%s']", path); } } if (jsonElem.isJsonPrimitive()) { return jsonElem.getAsString(); } return jsonElem.toString(); } }; } }; }
From source file:com.liferay.mobile.sdk.json.JSONArrayDeserializer.java
License:Open Source License
@Override public JSONArray deserialize(JsonElement json, Type type, JsonDeserializationContext context) throws JsonParseException { try {/*from w w w .jav a 2 s .com*/ String value = json.toString(); return new JSONArray(value); } catch (JSONException jsone) { throw new JsonParseException(jsone); } }
From source file:com.liferay.mobile.sdk.json.JSONObjectDeserializer.java
License:Open Source License
@Override public JSONObject deserialize(JsonElement json, Type type, JsonDeserializationContext context) throws JsonParseException { try {/* w w w . j a v a 2s . com*/ String value = json.toString(); return new JSONObject(value); } catch (JSONException jsone) { throw new JsonParseException(jsone); } }
From source file:com.lithium.luces.Luces.java
License:Apache License
@Override public Luces mapping(String typename, JsonObject mapping) { if (log.isDebugEnabled()) { log.debug("Adding mapping for type " + typename); }/* w w w .j av a2 s. com*/ if (log.isTraceEnabled()) { log.trace("Sending mapping: " + new GsonBuilder().setPrettyPrinting().create().toJson(mapping)); } if (null == typename || null == mapping) { if (errIfMappingNull) { throw new IllegalStateException( String.format("%1$s cannot be set to null", typename == null ? "Type" : "Mapping")); } log.warn("Setting mapping and type to null, no primitive type conversion will be done"); typeName = null; typeMap = null; } else { typeName = typename; typeMap = new HashMap<>(); JsonObject workingJson = mapping.getAsJsonObject(typename); if (null == workingJson) { throw new NoSuchElementException(typename + " type not present or misnamed in mapping"); } // TODO account for nesting workingJson = workingJson.getAsJsonObject("properties"); for (Entry<String, JsonElement> entry : workingJson.entrySet()) { JsonElement typeElt = entry.getValue().getAsJsonObject().get("type"); if (null == typeElt) { throw new NoSuchElementException( "Invalid mapping: No type defined for " + entry.getKey() + " field."); } ParseType parseType; try { parseType = ParseType.valueOf(typeElt.getAsString().toUpperCase()); } catch (UnsupportedOperationException ex) { throw new UnsupportedOperationException( "Invalid Mapping: Type defined is not a string: " + typeElt.toString()); } catch (IllegalArgumentException illegal) { throw new UnsupportedOperationException( "The " + typeElt.getAsString() + " type is not supported for conversion"); } typeMap.put(entry.getKey(), parseType); } } return this; }
From source file:com.ls.util.internal.ObjectComparator.java
License:Open Source License
private static Object convertElementToStringRepresentation(JsonElement source) { if (source.isJsonNull()) { return null; }//from ww w. j a va2 s. c o m if (source.isJsonPrimitive()) { return source.toString(); } if (source.isJsonObject()) { return getMapFromJsonElement((JsonObject) source); } if (source.isJsonArray()) { return getListFromJsonElement((JsonArray) source); } return null; }
From source file:com.microsoft.azure.sdk.iot.deps.serializer.Twin.java
License:Open Source License
/** * Update the `desired` properties information in the collection, and return a string with a json that contains a * sub-collection of added properties, or properties with new value. * * @param propertyMap - Map of `desired` property to change the collection. * @return Json with added or changed properties * @throws IllegalArgumentException This exception is thrown if the properties in the map do not fits the requirements. *///w ww .ja v a2 s.c o m public String updateDesiredProperty(Map<String, Object> propertyMap) throws IllegalArgumentException { if (propertyMap == null) { /* Codes_SRS_TWIN_21_023: [If the provided `property` map is null, the updateDesiredProperty shall not change the collection and throw IllegalArgumentException.] */ throw new IllegalArgumentException("Null desired property map."); } JsonElement jsonElement = innerUpdateDesiredProperty(propertyMap); if (jsonElement == null) { /* Codes_SRS_TWIN_21_024: [If no Desired property changed its value, the updateDesiredProperty shall return null.] */ /* Codes_SRS_TWIN_21_063: [If the provided `property` map is empty, the updateDesiredProperty shall not change the collection and return null.] */ return null; } return jsonElement.toString(); }
From source file:com.microsoft.azure.sdk.iot.deps.serializer.Twin.java
License:Open Source License
/** * Update the `reported` properties information in the collection, and return a string with a json that contains a * sub-collection of added properties, or properties with new value. * * @param propertyMap - Map of `reported` property to change the collection. * @return Json with added or changed properties * @throws IllegalArgumentException This exception is thrown if the properties in the map do not fits the requirements. */// w w w . ja v a 2 s.c o m public String updateReportedProperty(Map<String, Object> propertyMap) throws IllegalArgumentException { if (propertyMap == null) { /* Codes_SRS_TWIN_21_027: [If the provided `property` map is null, the updateReportedProperty shall not change the collection and throw IllegalArgumentException.] */ throw new IllegalArgumentException("Null reported property map."); } JsonElement jsonElement = innerUpdateReportedProperty(propertyMap); if (jsonElement == null) { /* Codes_SRS_TWIN_21_028: [If no Reported property changed its value, the updateReportedProperty shall return null.] */ return null; } return jsonElement.toString(); }
From source file:com.microsoft.azure.sdk.iot.deps.serializer.Twin.java
License:Open Source License
/** * Update the `tags` information in the collection, and return a string with a json that contains a * sub-collection of added tags, or tags with new value. * * @param tagsMap - Map of `tags` to change the collection. * @return Json with added or changed tags * @throws IllegalArgumentException This exception is thrown if the tags in the map do not fits the requirements. * @throws IOException This exception is thrown if tags the is not enabled. */// w w w. j av a 2 s . c o m public String updateTags(Map<String, Object> tagsMap) throws IllegalArgumentException, IOException { JsonElement jsonElement = innerUpdateTags(tagsMap); if (jsonElement == null) { /* Codes_SRS_TWIN_21_109: [If the provided `tagsMap` is empty, the updateTags shall not change the collection and return null.] */ /* Codes_SRS_TWIN_21_104: [The updateTags shall return a string with json representing the tags with changes.] */ return null; } return jsonElement.toString(); }