List of usage examples for com.google.gson JsonElement isJsonObject
public boolean isJsonObject()
From source file:com.baidu.rigel.biplatform.ac.util.deserialize.MeasureDeserialize.java
License:Open Source License
@Override public Measure deserialize(JsonElement json, Type typeOfT, JsonDeserializationContext context) throws JsonParseException { if (json.isJsonObject()) { JsonObject jsObj = json.getAsJsonObject(); String agg = jsObj.get("aggregator").getAsString(); if (agg.equals(Aggregator.CALCULATED.name())) { String type = jsObj.get("type").getAsString(); if (type.equals(MeasureType.CALLBACK.name())) { return context.deserialize(jsObj, CallbackMeasure.class); }// w w w . j a va 2 s .c om return context.deserialize(jsObj, ExtendMinicubeMeasure.class); } return context.deserialize(jsObj, MiniCubeMeasure.class); } return null; }
From source file:com.baidu.rigel.biplatform.ac.util.deserialize.MetaConditionDeserialize.java
License:Open Source License
@Override public MetaCondition deserialize(JsonElement json, Type typeOfT, JsonDeserializationContext context) throws JsonParseException { if (json.isJsonObject()) { JsonObject conditionObj = json.getAsJsonObject(); MetaType metaType = context.deserialize(conditionObj.get("metaType"), MetaType.class); if (metaType.equals(MetaType.Dimension)) { return context.deserialize(json, DimensionCondition.class); } else {/*w w w . j av a 2s.c o m*/ return context.deserialize(json, MeasureCondition.class); } } return null; }
From source file:com.balajeetm.mystique.util.gson.lever.JsonComparator.java
License:Open Source License
/** * Checks if is subset./*from ww w . j a va2 s. com*/ * * @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 object.//from w ww. j a v a 2 s . c om * * @param source the source * @return the boolean */ public Boolean isObject(JsonElement source) { return isNotNull(source) && source.isJsonObject(); }
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 */// www. j ava2s .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.bing.maps.rest.services.impl.BaseBingMapsApiQuery.java
License:Apache License
@Override public List<T> list() { InputStream jsonContent = null; try {// w ww . j a v a 2s.c o m jsonContent = callApiGet(apiUrlBuilder.buildUrl()); JsonElement response = parser.parse(new InputStreamReader(jsonContent, UTF_8_CHAR_SET)); if (response.isJsonObject()) { List<T> responseList = unmarshallList(response.getAsJsonObject()); notifyObservers(responseList); return responseList; } throw new BingMapsException("Unknown content found in response:" + response.toString()); } catch (Exception e) { throw new BingMapsException(e); } finally { closeStream(jsonContent); } }
From source file:com.birbit.jsonapi.JsonApiDeserializer.java
License:Apache License
public JsonApiResponse deserialize(JsonElement json, Type typeOfT, JsonDeserializationContext context) throws JsonParseException { if (!json.isJsonObject()) { throw new JsonParseException("JSON API response root should be a json object"); }/*from w ww . j av a2 s. c o m*/ if (!(typeOfT instanceof ParameterizedType)) { throw new JsonParseException("JSON API response should be requested with a parameterized type where the" + " type parameter represents the `data` field's type"); } ParameterizedType parameterizedType = (ParameterizedType) typeOfT; JsonObject jsonObject = json.getAsJsonObject(); JsonApiLinks links = parseLinks(context, jsonObject); T[] data = parseData(context, parameterizedType, jsonObject); List<JsonApiError> errors = parserErrors(context, jsonObject); if ((data == null) == (errors == null)) { throw new JsonParseException("The JSON API response should have data or errors"); } if (errors != null) { return new JsonApiResponse(errors, typeMapping, links); } Map<String, Map<String, Object>> included = parseIncluded(context, jsonObject); //noinspection unchecked return new JsonApiResponse(data, included, typeMapping, links); }
From source file:com.birbit.jsonapi.JsonApiDeserializer.java
License:Apache License
private JsonApiLinks parseLinks(JsonDeserializationContext context, JsonObject jsonObject) { JsonElement links = jsonObject.get("links"); if (links == null || !links.isJsonObject()) { return JsonApiLinks.EMPTY; }//from w w w.j av a 2s . c om return context.deserialize(links, JsonApiLinks.class); }
From source file:com.birbit.jsonapi.JsonApiDeserializer.java
License:Apache License
private T[] parseData(JsonDeserializationContext context, ParameterizedType parameterizedType, JsonObject jsonObject) {// www . j a va 2 s . c om 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.JsonApiLinksDeserializer.java
License:Apache License
@Override public JsonApiLinks deserialize(JsonElement json, Type typeOfT, JsonDeserializationContext context) throws JsonParseException { if (!json.isJsonObject()) { throw new JsonParseException("JsonApiLinks json element must be an object"); }// w ww. j a va 2s . c o m JsonObject asJsonObject = json.getAsJsonObject(); Set<Map.Entry<String, JsonElement>> entries = asJsonObject.entrySet(); if (entries.isEmpty()) { return JsonApiLinks.EMPTY; } Map<String, JsonApiLinkItem> result = new HashMap<String, JsonApiLinkItem>(); for (Map.Entry<String, JsonElement> entry : asJsonObject.entrySet()) { JsonElement value = entry.getValue(); if (value.isJsonPrimitive()) { result.put(entry.getKey(), new JsonApiLinkItem(entry.getValue().getAsString())); } else { result.put(entry.getKey(), context.<JsonApiLinkItem>deserialize(entry.getValue(), JsonApiLinkItem.class)); } } return new JsonApiLinks(result); }