List of usage examples for com.google.gson JsonElement isJsonArray
public boolean isJsonArray()
From source file:com.balajeetm.mystique.core.lever.MystiqueLever.java
License:Open Source License
/** * Gets the subset./* w w w.ja v a 2 s . c o m*/ * * @param source the source * @param deps the deps * @param aces the aces * @param valueObject the value object * @return the subset */ public JsonElement subset(JsonElement source, JsonObject deps, JsonObject aces, JsonElement valueObject) { JsonElement finalValue = null; if (valueObject.isJsonArray()) { finalValue = subset(source, deps, aces, valueObject.getAsJsonArray()); } else if (isString(valueObject)) { finalValue = subset(source, deps, aces, getJpath(valueObject)); } else if (isObject(valueObject)) { // This is a turn JsonObject valueJson = valueObject.getAsJsonObject(); MystTurn mystique = factory().getMystTurn(valueJson); finalValue = mystique.transform(Lists.newArrayList(source), deps, aces, valueJson, new JsonObject()); } return finalValue; }
From source file:com.balajeetm.mystique.util.gson.lever.JsonComparator.java
License:Open Source License
/** * Checks if is subset./* www . ja va2 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
/** * Checks if is json array./*from w ww . j av a 2s. co m*/ * * @param source the source * @return the boolean */ public Boolean isArray(JsonElement source) { return isNotNull(source) && source.isJsonArray(); }
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.j a v a2 s .co 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.bbk.cgac.controller.pagecontroller.ax.SampleAjaxPageController.java
@Override public void execute() throws IOException { //Sample Code //Test Request JsonElement jel = mJson; if (jel.isJsonArray()) { JsonArray jarr = jel.getAsJsonArray(); if (jarr.get(0).isJsonObject()) { JsonObject jobj = jarr.get(0).getAsJsonObject(); System.out.println("JsonObject ."); System.out.println("a: " + jobj.get("a")); }/* ww w . j a va 2 s . c o m*/ if (jarr.get(1).isJsonArray()) { JsonArray jarr2 = jarr.get(1).getAsJsonArray(); System.out.println("jarr[1]? JsonArray ."); } } //Test Response (Example) TempObj obj = new TempObj("Kim"); String arg01 = JSON.parseToString(obj); TempObj obj1 = new TempObj("Lee"); String arg02 = JSON.parseToString(obj1); mResponse.setContentType("text/html;charset=UTF-8"); mResponse.getWriter().write("[" + arg01 + "," + arg02 + "]"); }
From source file:com.betafase.mcmanager.api.SpigotUpdateChecker.java
@Override public UpdateInfo checkUpdate() { ServerRequest r = new ServerRequest("resources/" + spigot_id + "/versions?sort=-name"); JsonElement e = r.getAsJsonElement(); if (e.isJsonArray()) { JsonObject current = e.getAsJsonArray().get(0).getAsJsonObject(); String version = current.get("name").getAsString(); if (!version.equalsIgnoreCase(plugin.getDescription().getVersion())) { UpdateInfo info = new UpdateInfo(plugin, null, version, "8Update found via SpigotUpdater", current.has("url") ? current.get("url").getAsString() : null) { @Override/*from w ww .ja v a 2 s . c o m*/ public void performUpdate(Player notifier) { SpigetPlugin d = new SpigetPlugin(spigot_id); d.loadInformation(); if (d.hasDirectDownload()) { notifier.sendMessage( "aCached Download from spiget.org found. Downloading directly..."); d.downloadDirectly(notifier, "plugins/" + getJarName()); } else if (d.isExternalDownload()) { notifier.sendMessage( "clError elThis Plugin has an external download. You must download it manually: " + d.getDownloadURL()); } else { notifier.sendMessage("aDownload started. Please wait..."); notifier.performCommand("wget plugins " + d.getDownloadURL()); } } }; return info; } } else { System.out.println("Could not check update for " + plugin.getName() + " : " + r.getAsString()); } return null; }
From source file:com.birbit.jsonapi.JsonApiDeserializer.java
License:Apache License
private List<JsonApiError> parserErrors(JsonDeserializationContext context, JsonObject jsonObject) { JsonElement errors = jsonObject.get("errors"); if (errors == null || !errors.isJsonArray()) { return null; }//from w w w . ja v a 2 s. co m JsonArray asJsonArray = errors.getAsJsonArray(); int size = asJsonArray.size(); List<JsonApiError> result = new ArrayList<JsonApiError>(size); for (int i = 0; i < size; i++) { result.add(context.<JsonApiError>deserialize(asJsonArray.get(i), JsonApiError.class)); } return result; }
From source file:com.birbit.jsonapi.JsonApiDeserializer.java
License:Apache License
private Map<String, Map<String, Object>> parseIncluded(JsonDeserializationContext context, JsonObject jsonObject) {// w ww . j ava2 s . c om JsonElement includedElm = jsonObject.get("included"); Map<String, Map<String, Object>> included; if (includedElm != null && includedElm.isJsonArray()) { included = new HashMap<String, Map<String, Object>>(); JsonArray includedArray = includedElm.getAsJsonArray(); final int size = includedArray.size(); for (int i = 0; i < size; i++) { ResourceWithIdAndType parsed = parseResource(includedArray.get(i), context); if (parsed.resource != null) { Map<String, Object> itemMap = included.get(parsed.apiType); if (itemMap == null) { itemMap = new HashMap<String, Object>(); included.put(parsed.apiType, itemMap); } itemMap.put(parsed.id, parsed.resource); } } } else { included = Collections.emptyMap(); } return included; }
From source file:com.birbit.jsonapi.JsonApiDeserializer.java
License:Apache License
private T[] parseData(JsonDeserializationContext context, ParameterizedType parameterizedType, JsonObject jsonObject) {/*from w ww .j av a 2s. com*/ JsonElement dataElm = jsonObject.get("data"); if (dataElm != null) { Type typeArg = parameterizedType.getActualTypeArguments()[0]; if (dataElm.isJsonArray()) { JsonArray jsonArray = dataElm.getAsJsonArray(); final int size = jsonArray.size(); // boolean isArray = typeArg instanceof GenericArrayType; // if (isArray) { TypeToken<?> typeToken = TypeToken.get(typeArg); T[] result = (T[]) Array.newInstance(typeToken.getRawType().getComponentType(), size); for (int i = 0; i < size; i++) { ResourceWithIdAndType<T> resourceWithIdAndType = parseResource(jsonArray.get(i), context); result[i] = resourceWithIdAndType.resource; } return result; // } else { // TypeToken<?> typeToken = TypeToken.get(typeArg); // T[] result = (T[]) Array.newInstance(typeToken.getRawType().getComponentType(), size); // for (int i = 0; i < size; i ++) { // ResourceWithIdAndType<T> resourceWithIdAndType = parseResource(jsonArray.get(i), context); // //noinspection unchecked // result[i] = resourceWithIdAndType.resource; // } // return result; // } } else if (dataElm.isJsonObject()) { T resource = parseResource(dataElm, context).resource; Object[] result = (Object[]) Array.newInstance(resource.getClass(), 1); result[0] = resource; return (T[]) result; } } return null; }
From source file:com.birbit.jsonapi.JsonApiResourceDeserializer.java
License:Apache License
private void parseRelationships(JsonDeserializationContext context, T t, JsonObject jsonObject) throws IllegalAccessException, InvocationTargetException { JsonElement relationships = jsonObject.get("relationships"); if (relationships != null && relationships.isJsonObject()) { JsonObject relationshipsObject = relationships.getAsJsonObject(); for (Map.Entry<String, Setter> entry : relationshipSetters.entrySet()) { JsonElement relationship = relationshipsObject.get(entry.getKey()); if (relationship != null && relationship.isJsonObject()) { if (entry.getValue().type() == JsonApiRelationshipList.class) { entry.getValue().setOnObject(t, context.deserialize(relationship, JsonApiRelationshipList.class)); } else if (entry.getValue().type() == JsonApiRelationship.class) { // JsonApiRelationship entry.getValue().setOnObject(t, context.deserialize(relationship, JsonApiRelationship.class)); } else { // String list or id JsonElement data = relationship.getAsJsonObject().get("data"); if (data != null) { if (data.isJsonObject()) { JsonElement relationshipIdElement = data.getAsJsonObject().get("id"); if (relationshipIdElement != null) { if (relationshipIdElement.isJsonPrimitive()) { entry.getValue().setOnObject(t, relationshipIdElement.getAsString()); }/*from w w w .j a v a 2 s . c o m*/ } } else if (data.isJsonArray()) { List<String> idList = parseIds(data.getAsJsonArray()); entry.getValue().setOnObject(t, idList); } } } } } } }