List of usage examples for com.google.gson JsonElement isJsonObject
public boolean isJsonObject()
From source file:br.com.caelum.vraptor.serialization.gson.GsonDeserialization.java
License:Open Source License
@Override public Object[] deserialize(InputStream inputStream, ControllerMethod method) { Class<?>[] types = getTypes(method); if (types.length == 0) { throw new IllegalArgumentException( "Methods that consumes representations must receive just one argument"); }// ww w . j a v a2 s .c o m Gson gson = builder.create(); final Parameter[] parameterNames = paramNameProvider.parametersFor(method.getMethod()); final Object[] values = new Object[parameterNames.length]; final Deserializee deserializee = deserializeeInstance.get(); try { String content = getContentOfStream(inputStream); logger.debug("json retrieved: {}", content); if (!isNullOrEmpty(content)) { JsonParser parser = new JsonParser(); JsonElement jsonElement = parser.parse(content); if (jsonElement.isJsonObject()) { JsonObject root = jsonElement.getAsJsonObject(); deserializee.setWithoutRoot(isWithoutRoot(parameterNames, root)); for (Class<? extends DeserializerConfig> option : method.getMethod() .getAnnotation(Consumes.class).options()) { DeserializerConfig config = container.instanceFor(option); config.config(deserializee); } for (int i = 0; i < types.length; i++) { Parameter parameter = parameterNames[i]; JsonElement node = root.get(parameter.getName()); if (deserializee.isWithoutRoot()) { values[i] = gson.fromJson(root, fallbackTo(parameter.getParameterizedType(), types[i])); logger.info("json without root deserialized"); break; } else if (node != null) { if (node.isJsonArray()) { JsonArray jsonArray = node.getAsJsonArray(); Type type = parameter.getParameterizedType(); if (type instanceof ParameterizedType) { values[i] = gson.fromJson(jsonArray, type); } else { values[i] = gson.fromJson(jsonArray, types[i]); } } else { values[i] = gson.fromJson(node, types[i]); } } } } else if (jsonElement.isJsonArray()) { if ((parameterNames.length != 1) || (!(parameterNames[0].getParameterizedType() instanceof ParameterizedType))) throw new IllegalArgumentException( "Methods that consumes an array representation must receive only just one collection generic typed argument"); JsonArray jsonArray = jsonElement.getAsJsonArray(); values[0] = gson.fromJson(jsonArray, parameterNames[0].getParameterizedType()); } else { throw new IllegalArgumentException("This is an invalid or not supported json content"); } } } catch (Exception e) { throw new ResultException("Unable to deserialize data", e); } logger.debug("json deserialized: {}", (Object) values); return values; }
From source file:br.com.thiaguten.contrib.JsonContrib.java
License:Apache License
/** * Appends key and/or value to json//w ww . j a va 2s . c om * * @param json string json which will be modified * @param key key that will be appended * @param value value that will be appended * @return the specified JSON string with appended key and/or value */ public static String appendValueToJson(String json, String key, Object value) { JsonElement jsonElement = parseObject(json); if (jsonElement != null) { if (jsonElement.isJsonPrimitive()) { JsonArray jsonArray = new JsonArray(); jsonArray.add(jsonElement); jsonArray.add(objectToJsonElementTree(value)); return jsonArray.toString(); } else if (jsonElement.isJsonObject()) { if (key == null) { throw new IllegalArgumentException( "to append some value into a JsonObject the 'key' parameter must not be null"); } JsonObject jsonObject = (JsonObject) jsonElement; jsonObject.add(key, objectToJsonElementTree(value)); return jsonObject.toString(); } else if (jsonElement.isJsonArray()) { JsonArray jsonArray = (JsonArray) jsonElement; jsonArray.add(objectToJsonElementTree(value)); return jsonArray.toString(); } } return getJsonElementAsString(jsonElement); }
From source file:br.com.thiaguten.contrib.JsonContrib.java
License:Apache License
/** * Converts the object to json string without the complexity * of relational hierarchy between them, creating a json "flatten". * * @param object object you wish to converts into a json flatten * @return flattened json/* ww w .java2s .c o m*/ */ public static String plainJson(Object object) { JsonElement jsonElement = parseObject(object); if (jsonElement != null) { if (jsonElement.isJsonObject()) { JsonObject jsonObjectFlattened = new JsonObject(); deepJsonObjectSearchToJsonObjectFlattened((JsonObject) jsonElement, jsonObjectFlattened); return jsonObjectFlattened.toString(); } else if (jsonElement.isJsonArray()) { JsonArray jsonArrayFlattened = new JsonArray(); deepJsonArraySearchToJsonArrayFlattened((JsonArray) jsonElement, jsonArrayFlattened); return jsonArrayFlattened.toString(); } } return getJsonElementAsString(jsonElement); }
From source file:br.com.thiaguten.contrib.JsonContrib.java
License:Apache License
private static void deepJsonArraySearchToJsonArrayFlattened(JsonArray jsonArray, JsonArray jsonArrayFlattened) { if (jsonArray == null || jsonArrayFlattened == null) { throw new IllegalArgumentException("JsonArray and/or JsonArrayFlattened parameter(s) must not be null"); }// ww w . java 2 s . co m Iterator<JsonElement> iterator = jsonArray.iterator(); while (iterator.hasNext()) { JsonElement value = iterator.next(); if (value.isJsonObject()) { // Creates new flattened JsonObject instance. JsonObject jsonObjectFlattened = new JsonObject(); // Iterates recursively in JsonObject (value), checking content and populating the flattened JsonObject instance. deepJsonObjectSearchToJsonObjectFlattened((JsonObject) value, jsonObjectFlattened); // Adds the flattened JsonObject instance in the flattened JsonArray instance. jsonArrayFlattened.add(jsonObjectFlattened); } else if (value.isJsonArray()) { deepJsonArraySearchToJsonArrayFlattened((JsonArray) value, jsonArrayFlattened); } else { jsonArrayFlattened.add(value); } } }
From source file:br.com.thiaguten.contrib.JsonContrib.java
License:Apache License
private static void deepJsonObjectSearchToJsonObjectFlattened(JsonObject jsonObject, JsonObject jsonObjectFlattened) { if (jsonObject == null || jsonObjectFlattened == null) { throw new IllegalArgumentException( "JsonObject and/or JsonObjectFlattened parameter(s) must not be null"); }/*www . j a v a2 s. com*/ for (Map.Entry<String, JsonElement> entry : jsonObject.entrySet()) { String key = entry.getKey(); JsonElement value = entry.getValue(); if (value.isJsonObject()) { deepJsonObjectSearchToJsonObjectFlattened((JsonObject) value, jsonObjectFlattened); } else if (value.isJsonArray()) { // Creates new flattened JsonArray instance. JsonArray jsonArray = new JsonArray(); // Iterates recursively in JsonArray (value), checking content and populating the flattened JsonArray instance. deepJsonArraySearchToJsonArrayFlattened((JsonArray) value, jsonArray); // Adds the flattened JsonArray instance in the flattened JsonObject instance. jsonObjectFlattened.add(key, jsonArray); } else { // FIXME - WORKAROUND // Attention: A map can not contain duplicate keys. So, the // duplicate key is concatenated with a counter, to avoid data // loss. I could not think of anything better yet. if (jsonObjectFlattened.has(key)) { jsonObjectFlattened.add(key + COUNTER.getAndIncrement(), value); } else { jsonObjectFlattened.add(key, value); } } } }
From source file:br.com.thiaguten.contrib.JsonContrib.java
License:Apache License
private static void deepJsonSearchToList(JsonElement jsonElement, List<JsonElement> jsonElementList) { if (jsonElement != null && jsonElementList != null) { if (jsonElement.isJsonObject()) { for (Map.Entry<String, JsonElement> entry : ((JsonObject) jsonElement).entrySet()) { deepJsonSearchToList(entry.getValue(), jsonElementList); }/* ww w . j ava 2s.c o m*/ } else if (jsonElement.isJsonArray()) { Iterator<JsonElement> iterator = ((JsonArray) jsonElement).iterator(); while (iterator.hasNext()) { deepJsonSearchToList(iterator.next(), jsonElementList); } } else { jsonElementList.add(jsonElement); } } }
From source file:br.com.thiaguten.contrib.JsonContrib.java
License:Apache License
private static void deepJsonSearchByKeyToList(String desiredKey, JsonElement jsonElement, List<Object> jsonElementList) { if (desiredKey != null && jsonElement != null && jsonElementList != null) { if (jsonElement.isJsonObject()) { for (Map.Entry<String, JsonElement> entry : ((JsonObject) jsonElement).entrySet()) { String key = entry.getKey(); JsonElement value = entry.getValue(); if (key.equalsIgnoreCase(desiredKey)) { jsonElementList.add(getJsonElementAsString(value)); }/*from ww w. ja va 2 s . co m*/ deepJsonSearchByKeyToList(desiredKey, entry.getValue(), jsonElementList); } } else if (jsonElement.isJsonArray()) { Iterator<JsonElement> iterator = ((JsonArray) jsonElement).iterator(); while (iterator.hasNext()) { deepJsonSearchByKeyToList(desiredKey, iterator.next(), jsonElementList); } } } }
From source file:br.com.thiaguten.contrib.JsonContrib.java
License:Apache License
private static List<JsonElement> jsonToJsonElementList(String json, boolean flatten) { JsonElement jsonElement = parseObject(json); List<JsonElement> elements = null; if (jsonElement != null) { boolean isJsonObject = jsonElement.isJsonObject(); boolean isJsonArray = jsonElement.isJsonArray(); elements = new ArrayList<JsonElement>(); // Only instances of 'JsonArray' or 'JsonObject' can be flattened if (isJsonArray || isJsonObject) { if (flatten) { deepJsonSearchToList(jsonElement, elements); } else { if (isJsonObject) { for (Map.Entry<String, JsonElement> entry : ((JsonObject) jsonElement).entrySet()) { elements.add(entry.getValue()); }/*from w w w. j a v a 2 s .c om*/ } else { Iterator<JsonElement> iterator = ((JsonArray) jsonElement).iterator(); while (iterator.hasNext()) { elements.add(iterator.next()); } } } } else { elements.add(jsonElement); } } return elements; }
From source file:ca.uhn.fhir.jpa.util.jsonpatch.CopyOperation.java
License:Apache License
@Override public JsonElement apply(JsonElement original) { JsonElement result = duplicate(original); JsonElement item = path.head().navigate(result); JsonElement data = mySourcePath.head().navigate(original); if (item.isJsonObject()) { item.getAsJsonObject().add(path.tail(), data); } else if (item.isJsonArray()) { JsonArray array = item.getAsJsonArray(); int index = (path.tail().equals("-")) ? array.size() : Integer.valueOf(path.tail()); List<JsonElement> temp = new ArrayList<JsonElement>(); Iterator<JsonElement> iter = array.iterator(); while (iter.hasNext()) { JsonElement stuff = iter.next(); iter.remove();// w w w . j a va 2s .com temp.add(stuff); } temp.add(index, data); for (JsonElement stuff : temp) { array.add(stuff); } } return result; }
From source file:ccm.pay2spawn.util.Helper.java
License:Open Source License
/** * Fill in variables from a donation// w ww . ja va 2 s . c om * * @param dataToFormat data to be formatted * @param donation the donation data * * @return the fully var-replaced JsonElement */ public static JsonElement formatText(JsonElement dataToFormat, Donation donation, Reward reward) { if (dataToFormat.isJsonPrimitive() && dataToFormat.getAsJsonPrimitive().isString()) { return new JsonPrimitive(Helper.formatText(dataToFormat.getAsString(), donation, reward)); } if (dataToFormat.isJsonArray()) { JsonArray out = new JsonArray(); for (JsonElement element : dataToFormat.getAsJsonArray()) { out.add(formatText(element, donation, reward)); } return out; } if (dataToFormat.isJsonObject()) { JsonObject out = new JsonObject(); for (Map.Entry<String, JsonElement> entity : dataToFormat.getAsJsonObject().entrySet()) { out.add(entity.getKey(), Helper.formatText(entity.getValue(), donation, reward)); } return out; } return dataToFormat; }