List of usage examples for com.google.gson JsonElement isJsonArray
public boolean isJsonArray()
From source file:bind.JsonTreeReader.java
License:Apache License
@Override public JsonToken peek() throws IOException { if (stack.isEmpty()) { return JsonToken.END_DOCUMENT; }//from w ww.j a v a2 s . c o m Object o = peekStack(); if (o instanceof Iterator) { Object secondToTop = stack.get(stack.size() - 2); boolean isObject = secondToTop instanceof JsonElement && ((JsonElement) secondToTop).isJsonObject(); Iterator<?> iterator = (Iterator<?>) o; if (iterator.hasNext()) { if (isObject) { return JsonToken.NAME; } else { stack.add(iterator.next()); return peek(); } } else { return isObject ? JsonToken.END_OBJECT : JsonToken.END_ARRAY; } } else if (o instanceof JsonElement) { JsonElement el = (JsonElement) o; if (el.isJsonObject()) { return JsonToken.BEGIN_OBJECT; } else if (el.isJsonArray()) { return JsonToken.BEGIN_ARRAY; } else if (el.isJsonPrimitive()) { JsonPrimitive primitive = (JsonPrimitive) o; if (primitive.isString()) { return JsonToken.STRING; } else if (primitive.isBoolean()) { return JsonToken.BOOLEAN; } else if (primitive.isNumber()) { return JsonToken.NUMBER; } else { throw new AssertionError(); } } else if (el.isJsonNull()) { return JsonToken.NULL; } throw new AssertionError(); } else if (o == SENTINEL_CLOSED) { throw new IllegalStateException("JsonReader is closed"); } else { throw new AssertionError(); } }
From source file:bind.JsonTreeWriter.java
License:Apache License
private void put(JsonElement value) { if (pendingName != null) { if (!value.isJsonNull() || getSerializeNulls()) { JsonObject object = (JsonObject) peek(); object.add(pendingName, value); }//from w w w .j av a 2 s .c om pendingName = null; } else if (stack.isEmpty()) { product = value; } else { JsonElement element = peek(); if (element.isJsonArray()) { element.getAsJsonArray().add(value); } else { throw new IllegalStateException(); } } }
From source file:bind.JsonTreeWriter.java
License:Apache License
@Override public JsonWriter endArray() throws IOException { if (stack.isEmpty() || pendingName != null) { throw new IllegalStateException(); }//from w w w .j a va2 s.co m JsonElement element = peek(); if (element.isJsonArray()) { stack.remove(stack.size() - 1); return this; } throw new IllegalStateException(); }
From source file:blusunrize.immersiveengineering.client.models.multilayer.MultiLayerModel.java
@Nonnull @Override//from w w w . ja v a 2 s. c o m public IModel process(ImmutableMap<String, String> customData) { Map<BlockRenderLayer, List<ModelData>> newSubs = new HashMap<>(); JsonParser parser = new JsonParser(); Map<String, String> unused = new HashMap<>(); for (String layerStr : customData.keySet()) if (LAYERS_BY_NAME.containsKey(layerStr)) { BlockRenderLayer layer = LAYERS_BY_NAME.get(layerStr); JsonElement ele = parser.parse(customData.get(layerStr)); if (ele.isJsonObject()) { ModelData data = ModelData.fromJson(ele.getAsJsonObject(), ImmutableList.of(), ImmutableMap.of()); newSubs.put(layer, ImmutableList.of(data)); } else if (ele.isJsonArray()) { JsonArray array = ele.getAsJsonArray(); List<ModelData> models = new ArrayList<>(); for (JsonElement subEle : array) if (subEle.isJsonObject()) models.add(ModelData.fromJson(ele.getAsJsonObject(), ImmutableList.of(), ImmutableMap.of())); newSubs.put(layer, models); } } else unused.put(layerStr, customData.get(layerStr)); JsonObject unusedJson = ModelData.asJsonObject(unused); for (Entry<BlockRenderLayer, List<ModelData>> entry : newSubs.entrySet()) for (ModelData d : entry.getValue()) for (Entry<String, JsonElement> entryJ : unusedJson.entrySet()) if (!d.data.has(entryJ.getKey())) d.data.add(entryJ.getKey(), entryJ.getValue()); if (!newSubs.equals(subModels)) return new MultiLayerModel(newSubs); return this; }
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"); }/* www. j av a2 s . c om*/ 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//from ww w. j a v a2 s .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/*from www . j a v a 2s .com*/ */ 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"); }/*from w w w.ja va 2 s . c o 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"); }/*from w ww . j a va 2s .c o m*/ 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 a va 2 s. c o m*/ } else if (jsonElement.isJsonArray()) { Iterator<JsonElement> iterator = ((JsonArray) jsonElement).iterator(); while (iterator.hasNext()) { deepJsonSearchToList(iterator.next(), jsonElementList); } } else { jsonElementList.add(jsonElement); } } }