List of usage examples for com.google.gson JsonNull INSTANCE
JsonNull INSTANCE
To view the source code for com.google.gson JsonNull INSTANCE.
Click Source Link
From source file:com.balajeetm.mystique.util.gson.lever.JsonComparator.java
License:Open Source License
/** * Checks if is subset.//from w ww . j a v a 2 s . c o m * * @param tag the tag * @param subset the subset * @param actual the actual * @param result the result * @return the myst result */ private Comparison isSubset(String tag, JsonElement subset, JsonElement actual, Comparison result) { subset = jsonLever.asJsonElement(subset, JsonNull.INSTANCE); actual = jsonLever.asJsonElement(actual, JsonNull.INSTANCE); if (jsonLever.isNotNull(subset) && jsonLever.isNull(actual)) { result.setResult(Boolean.FALSE); result.addMsg(String.format("The field %s of actual is null", tag)); } else if (!subset.getClass().getCanonicalName().equals(actual.getClass().getCanonicalName())) { result.setResult(Boolean.FALSE); result.addMsg(String.format("The field %s of expected and actual are not of the same type", tag)); } else { if (subset.isJsonObject()) { JsonObject subJson = jsonLever.asJsonObject(subset); JsonObject actJson = jsonLever.asJsonObject(actual); Set<Entry<String, JsonElement>> entrySet = subJson.entrySet(); for (Entry<String, JsonElement> entry : entrySet) { String key = entry.getKey(); JsonElement value = entry.getValue(); JsonElement actualValue = actJson.get(key); isSubset(key, value, actualValue, result); } } else if (subset.isJsonArray()) { JsonArray subJson = jsonLever.asJsonArray(subset); JsonArray actJson = jsonLever.asJsonArray(actual); if (subJson.size() != actJson.size()) { result.setResult(Boolean.FALSE); result.addMsg(String.format("The field %s of expected and actual are not of same size", tag)); } else { for (int i = 0; i < subJson.size(); i++) { isSubset(tag, subJson.get(i), actJson.get(i), result); } } } else { if (!subset.equals(actual)) { result.setResult(Boolean.FALSE); result.addMsg(String.format("The field %s of expected and actual are not same", tag)); } } } return result; }
From source file:com.balajeetm.mystique.util.gson.lever.JsonLever.java
License:Open Source License
/** * Gets the json element in the specified jpath. * * @param source the source//w w w.java 2s. co m * @param jpath the fully qualified json path to the field required. eg get({'a': {'b': {'c': [1, * 2, 3, 4]}}}, ["a", "b" "c", 1]) is '2'. Array indexes need to be specified as numerals. * Strings are always presumed to be field names. * @return the json element returns 'Null' for any invalid path return the source if the input * jpath is '.' */ public JsonElement get(JsonElement source, JsonArray jpath) { JsonElement result = JsonNull.INSTANCE; if (isNotNull(jpath)) { result = source; for (JsonElement path : jpath) { try { if (isNumber(path)) { result = result.getAsJsonArray().get(path.getAsInt()); } else { result = result.getAsJsonObject().get(path.getAsString()); } } catch (Exception e) { return JsonNull.INSTANCE; } } } return result; }
From source file:com.balajeetm.mystique.util.gson.lever.JsonLever.java
License:Open Source License
/** * Sets the json element at the specified jpath. * * @param source the source// www .j av a 2s.c o m * @param jpath the fully qualified json path to the field required. eg set({'a': {'b': {'c': [1, * 2, 3, 4]}}}, ["a", "b" "c", 1, 5]) is {'a': {'b': {'c': [1, 5, 3, 4]}}}. Array indexes need * to be specified as numerals. Strings are always presumed to be field names. * @param value the value * @return the json element */ public JsonElement set(JsonElement source, JsonArray jpath, JsonElement value) { JsonElement result = JsonNull.INSTANCE; if (isNotNull(jpath)) { result = source; JsonElement field = result; if (jpath.size() > 0) { JsonElement previousPath = null; JsonElement currentPath = null; Iterator<JsonElement> iterator = jpath.iterator(); if (iterator.hasNext()) { previousPath = iterator.next(); } while (iterator.hasNext()) { currentPath = iterator.next(); // get the field field = getRepleteField(field, previousPath, currentPath); result = updateResult(result, field); field = isNumber(previousPath) ? field.getAsJsonArray().get(previousPath.getAsInt()) : field.getAsJsonObject().get(previousPath.getAsString()); previousPath = currentPath; } field = setField(field, previousPath, value); } } return result; }
From source file:com.balajeetm.mystique.util.gson.lever.JsonLever.java
License:Open Source License
/** * Gets the subset json from a source json. * * @param source the json source which can be Object, Array or a Primitive * @param jPathArray the array of jPaths defining the full qualified json paths to the required * fields/*from w w w . j a v a2 s .co m*/ * @return the json subset */ public JsonElement subset(JsonElement source, JsonArray jPathArray) { JsonElement finalValue = JsonNull.INSTANCE; if (jPathArray.size() == 0) { finalValue = source; } else { finalValue = new JsonObject(); for (JsonElement jsonElement : jPathArray) { JsonElement subset = JsonNull.INSTANCE; if (isArray(jsonElement)) { JsonArray pathArray = asJsonArray(jsonElement); subset = get(source, pathArray); finalValue = set(finalValue, pathArray, subset); } else if (isString(jsonElement)) { String jpath = asString(jsonElement); subset = get(source, jpath); finalValue = set(finalValue, jpath, subset); } else { finalValue = JsonNull.INSTANCE; break; } } } return finalValue; }
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 *//* ww w . ja va 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
/** * Gets the typed path.//from www . ja v a 2s. c om * * @param p the p * @param strToNum the str to num * @return the typed path */ private JsonElement getTypedPath(Object p, Boolean strToNum) { JsonElement result = JsonNull.INSTANCE; if (p instanceof Number) { result = new JsonPrimitive((Number) p); } else if (p instanceof String) { String sp = StringUtils.trimToEmpty((String) p); result = strToNum ? (NumberUtils.isCreatable(sp) ? new JsonPrimitive(NumberUtils.toInt(sp)) : new JsonPrimitive(sp)) : new JsonPrimitive(sp); } else if (p instanceof Boolean) { result = new JsonPrimitive((Boolean) p); } else if (p instanceof Character) { result = new JsonPrimitive((Character) p); } else if (p instanceof JsonElement) { result = (JsonElement) p; } return result; }
From source file:com.balajeetm.mystique.util.gson.lever.JsonLever.java
License:Open Source License
/** * Jsonify.//from w w w. j a v a2s.com * * @param source the source * @return the json element */ public JsonElement jsonify(Object source) { JsonElement result = JsonNull.INSTANCE; if (source instanceof JsonElement) { result = (JsonElement) source; } else { try { result = GsonConvertor.getInstance().deserialize(source, JsonElement.class); } catch (ConvertorException e) { String msg = String.format("Could not deserialise object %s to json.", source); log.error(msg); log.debug(msg, e); } } return result; }
From source file:com.balajeetm.mystique.util.gson.lever.JsonLever.java
License:Open Source License
/** * Gets the new element.// ww w . j ava 2s . c o 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
/** * Equals filter.//from www .ja va 2s . c o m * * @param json the json * @param condition the condition * @return the boolean */ private Boolean equalsFilter(JsonElement json, JsonObject condition) { return jsonLever.asJsonElement(condition.get("value"), JsonNull.INSTANCE) .equals(jsonLever.get(json, jsonLever.asJsonArray(condition.get("field"), new JsonArray()))); }
From source file:com.codenvy.ide.ext.java.server.TypeBindingConvector.java
License:Open Source License
public static String toJsonBinaryType(SourceTypeBinding binding) { JsonObject object = new JsonObject(); if (!binding.isAnnotationType()) { object.add("annotations", toJsonAnnotations(binding.getAnnotations())); } else {//from w w w . java 2 s . c o m object.add("annotations", JsonNull.INSTANCE); } object.add("enclosingMethod", null); object.add("enclosingTypeName", binding.enclosingType() == null ? JsonNull.INSTANCE : new JsonPrimitive(new String(binding.enclosingType().constantPoolName()))); object.add("fields", toJsonFields(binding.fields())); object.add("genericSignature", binding.genericSignature() == null ? JsonNull.INSTANCE : new JsonPrimitive(new String(binding.genericSignature()))); object.add("interfaceNames", toJsonInterfaces(binding.superInterfaces())); object.add("memberTypes", toJsonMemberTypes(binding.memberTypes())); object.add("methods", toJsonMethods(binding.methods())); object.add("missingTypeNames", null); object.add("name", binding.constantPoolName() == null ? JsonNull.INSTANCE : new JsonPrimitive(new String(binding.constantPoolName()))); object.add("sourceName", binding.sourceName() == null ? JsonNull.INSTANCE : new JsonPrimitive(new String(binding.sourceName()))); object.add("superclassName", binding.superclass() == null ? JsonNull.INSTANCE : new JsonPrimitive(new String(binding.superclass().constantPoolName()))); long annotationTagBits = binding.getAnnotationTagBits(); //remove AreMethodsComplete bit tag from type annotationTagBits &= ~TagBits.AreMethodsComplete; object.add("tagBits", new JsonPrimitive(String.valueOf(annotationTagBits))); object.add("anonymous", new JsonPrimitive(binding.isAnonymousType())); object.add("local", new JsonPrimitive(binding.isLocalType())); object.add("member", new JsonPrimitive(binding.isMemberType())); object.add("sourceFileName", binding.sourceName() == null ? JsonNull.INSTANCE : new JsonPrimitive(new String(binding.sourceName()))); object.add("modifiers", new JsonPrimitive(binding.modifiers)); object.add("binaryType", new JsonPrimitive(true)); object.add("fileName", null); return gson.toJson(object); }