List of usage examples for com.google.gson JsonArray JsonArray
public JsonArray()
From source file:com.balajeetm.mystique.util.gson.lever.JsonLever.java
License:Open Source License
/** * Gets the jpath for a '.' separated string defining the fully qualified path of a field in Json. * Array Indexes are referred via numbers. * * @param jpath the jpath/*from ww w.j ava 2s . co m*/ * @return the fully qualified path as a JsonArray. Array Indexes are represented as numbers */ public JsonArray getJpath(String jpath) { JsonArray jlist = new JsonArray(); if (!StringUtils.contains(jpath, ".")) { jlist.add(getTypedPath(jpath)); } else { String[] split = StringUtils.split(jpath, "."); for (String sp : split) { jlist.add(getTypedPath(sp)); } } return jlist; }
From source file:com.balajeetm.mystique.util.gson.lever.JsonLever.java
License:Open Source License
/** * New json array./*from w w w . ja va2 s. c om*/ * * @param path the path * @return the json array */ public JsonArray newJsonArray(Object... path) { JsonArray output = new JsonArray(); for (Object p : path) { output.add(getTypedPath(p, Boolean.FALSE)); } return output; }
From source file:com.balajeetm.mystique.util.gson.lever.JsonLever.java
License:Open Source License
/** * A recursive merge of two json elements. * * @param source1 the first json element * @param source2 the second json element * @param mergeArray the flag to denote if arrays should be merged * @return the recursively merged json element *//*from w w w . ja v a 2 s .c o m*/ public JsonElement merge(JsonElement source1, JsonElement source2, Boolean mergeArray) { mergeArray = null == mergeArray ? Boolean.FALSE : mergeArray; JsonElement result = JsonNull.INSTANCE; source1 = asJsonElement(source1, JsonNull.INSTANCE); source2 = asJsonElement(source2, JsonNull.INSTANCE); if (source1.getClass().equals(source2.getClass())) { if (source1.isJsonObject()) { JsonObject obj1 = asJsonObject(source1); JsonObject obj2 = asJsonObject(source2); result = obj1; JsonObject resultObj = result.getAsJsonObject(); for (Entry<String, JsonElement> entry : obj1.entrySet()) { String key = entry.getKey(); JsonElement value1 = entry.getValue(); JsonElement value2 = obj2.get(key); JsonElement merge = merge(value1, value2, mergeArray); resultObj.add(key, merge); } for (Entry<String, JsonElement> entry : obj2.entrySet()) { String key = entry.getKey(); if (!resultObj.has(key)) { resultObj.add(key, entry.getValue()); } } } else if (source1.isJsonArray()) { result = new JsonArray(); JsonArray resultArray = result.getAsJsonArray(); JsonArray array1 = asJsonArray(source1); JsonArray array2 = asJsonArray(source2); int index = 0; int a1size = array1.size(); int a2size = array2.size(); if (!mergeArray) { for (; index < a1size && index < a2size; index++) { resultArray.add(merge(array1.get(index), array2.get(index), mergeArray)); } } for (; index < a1size; index++) { resultArray.add(array1.get(index)); } index = mergeArray ? 0 : index; for (; index < a2size; index++) { resultArray.add(array2.get(index)); } } else { result = source1 != null ? source1 : source2; } } else { result = isNotNull(source1) ? source1 : source2; } return result; }
From source file:com.balajeetm.mystique.util.gson.lever.JsonLever.java
License:Open Source License
/** * Sets the field.//www . j a v a2 s. c om * * @param field the field * @param path the path * @param value the value * @return the json element */ protected JsonElement setField(JsonElement field, JsonElement path, JsonElement value) { if (isNumber(path)) { JsonArray jArray = asJsonArray(field.getAsJsonArray(), new JsonArray()); int index = path.getAsInt(); repleteArray(jArray, index, JsonNull.class); jArray.set(index, value); field = jArray; } else { JsonObject jObject = asJsonObject(field, new JsonObject()); jObject.add(path.getAsString(), value); field = jObject; } return field; }
From source file:com.balajeetm.mystique.util.gson.lever.JsonLever.java
License:Open Source License
/** * Gets the replete field./*from w ww . j ava 2 s . co m*/ * * @param field the field * @param path the path * @param nextPath the next path * @return the replete field */ protected JsonElement getRepleteField(JsonElement field, JsonElement path, JsonElement nextPath) { if (isNumber(path)) { Integer index = path.getAsInt(); if (!isArray(field)) { field = new JsonArray(); } JsonArray fArray = repleteArray(field.getAsJsonArray(), index, isNumber(nextPath) ? JsonArray.class : JsonObject.class); field = fArray; } else { String fieldName = path.getAsString(); if (!isObject(field)) { field = new JsonObject(); } JsonObject fJson = repleteJson(field.getAsJsonObject(), fieldName, isNumber(nextPath) ? JsonArray.class : JsonObject.class); field = fJson; } return field; }
From source file:com.balajeetm.mystique.util.gson.lever.JsonLever.java
License:Open Source License
/** * Replete array.//from w ww . j a v a 2s. co m * * @param source the source * @param index the index * @param type the type * @return the json array */ private JsonArray repleteArray(JsonArray source, Integer index, Class<? extends JsonElement> type) { source = isNull(source) ? new JsonArray() : source; for (int i = 0; i <= index; i++) { try { source.get(i); } catch (IndexOutOfBoundsException e) { Class<? extends JsonElement> newType = JsonNull.class; if (i == index) { newType = type; } source.add(getNewElement(newType)); } } if (!source.get(index).getClass().equals(type)) { log.warn(String.format( "The element at index %s in the source %s, does not match %s. Creating a new element.", index, source, type)); source.add(getNewElement(type)); } return source; }
From source file:com.balajeetm.mystique.util.gson.lever.JsonLever.java
License:Open Source License
/** * Gets the new element.//from w w w . j ava 2 s .co m * * @param type the type * @return the new element */ private JsonElement getNewElement(Class<? extends JsonElement> type) { JsonElement element = JsonNull.INSTANCE; if (JsonObject.class.equals(type)) { element = new JsonObject(); } else if (JsonArray.class.equals(type)) { element = new JsonArray(); } else { try { element = type.newInstance(); } catch (InstantiationException | IllegalAccessException e) { log.error(String.format("Could not instantiate json element of type %s", type)); log.debug(String.format("Could not instantiate json element of type %s.", type), e); } } return element; }
From source file:com.balajeetm.mystique.util.gson.lever.JsonQuery.java
License:Open Source License
/** * Query./*ww w . jav a2s. co m*/ * * @param query the query * @return the json element */ public JsonElement query(JsonObject query) { JsonElement result = null; try { Mono<JsonArray> reduce = queryAsync(query).reduce(new JsonArray(), (resultarray, json) -> { resultarray.add(json); return resultarray; }); result = reduce.block(); } catch (RuntimeException e) { result = new JsonPrimitive(e.getMessage()); } return result; }
From source file:com.balajeetm.mystique.util.gson.lever.JsonQuery.java
License:Open Source License
/** * Select./*from w ww . ja va 2s . c om*/ * * @param json the json * @param selectElement the select element * @return the json element */ private JsonElement select(JsonElement json, JsonElement selectElement) { JsonElement result = null; String selectStr = jsonLever.asString(selectElement); if (null != selectStr) { if (equalsAny(selectStr, "*")) result = json; } else { if (jsonLever.isArray(selectElement)) { result = new JsonArray(); for (JsonElement ele : jsonLever.asJsonArray(selectElement)) { jsonLever.asJsonArray(result).add(jsonLever.get(json, ele)); } if (jsonLever.asJsonArray(result).size() == 1) { result = jsonLever.getFirst(jsonLever.asJsonArray(result)); } } } return result; }
From source file:com.balajeetm.mystique.util.gson.lever.JsonQuery.java
License:Open Source License
/** * Filter./*from w ww .j a va2s.co m*/ * * @param json the json * @param whereElement the where element * @return the boolean */ private Boolean filter(JsonElement json, JsonElement whereElement) { Boolean filter = Boolean.TRUE; JsonArray where = jsonLever.asJsonArray(whereElement, new JsonArray()); for (JsonElement condition : where) { if (jsonLever.isString(condition)) { String operator = StringUtils.lowerCase(jsonLever.asString(condition)); if (equalsAny(operator, "&", "&&", "and")) { if (filter) { continue; } else { return filter; } } if (equalsAny(operator, "|", "||", "or")) { if (!filter) { continue; } else { return filter; } } } else if (jsonLever.isObject(condition)) { String type = jsonLever.getString(condition, "type"); type = null != type && queryTypes.containsKey(type) ? type : "="; filter = queryTypes.get(type).apply(json, condition.getAsJsonObject()); } } return filter; }