List of usage examples for com.google.gson JsonObject entrySet
public Set<Map.Entry<String, JsonElement>> entrySet()
From source file:com.azure.webapi.MobileServiceTableBase.java
License:Open Source License
/** * Updates the JsonObject to have an id property * @param json// w w w .j av a 2 s . c o m * the element to evaluate */ protected void updateIdProperty(final JsonObject json) throws IllegalArgumentException { for (Map.Entry<String, JsonElement> entry : json.entrySet()) { String key = entry.getKey(); if (key.equalsIgnoreCase("id")) { JsonElement element = entry.getValue(); if (isValidTypeId(element)) { if (!key.equals("id")) { //force the id name to 'id', no matter the casing json.remove(key); // Create a new id property using the given property name json.addProperty("id", entry.getValue().getAsNumber()); } return; } else { throw new IllegalArgumentException("The id must be numeric"); } } } }
From source file:com.balajeetm.mystique.core.lever.MystiqueLever.java
License:Open Source License
/** * Gets the updated aces./*from ww w. j a v a 2 s. co m*/ * * @param source the source * @param aces the aces * @param dependencies the dependencies * @param updated the updated * @return the updated aces */ public JsonObject getUpdatedAces(JsonElement source, JsonObject aces, JsonObject dependencies, JsonObject updated) { if (isNotNull(aces)) { for (Entry<String, JsonElement> entry : aces.entrySet()) { JsonObject value = asJsonObject(entry.getValue()); // Null check required, since for all other purposes, no turn // means a default turn. In this case, turn needs to be executed // only if it is explicitly specified MystTurn mystique = null != value ? factory().getMystTurn(value) : null; if (null != mystique) { JsonElement transform = mystique.transform(Lists.newArrayList(source), dependencies, aces, value, new JsonObject()); updated.add(entry.getKey(), transform); } } } return updated; }
From source file:com.balajeetm.mystique.core.module.GsonSerialiser.java
License:Open Source License
@Override public void serialize(JsonElement value, JsonGenerator gen, SerializerProvider provider) throws IOException { if (jsonLever.isNull(value)) { gen.writeNull();/*from www. j a v a 2s . c o m*/ } else if (jsonLever.isObject(value)) { gen.writeStartObject(); JsonObject jsonObject = value.getAsJsonObject(); for (Entry<String, JsonElement> entry : jsonObject.entrySet()) { gen.writeFieldName(entry.getKey()); serialize(entry.getValue(), gen, provider); } gen.writeEndObject(); } else if (jsonLever.isArray(value)) { gen.writeStartArray(); JsonArray jsonArray = value.getAsJsonArray(); for (JsonElement jsonElement : jsonArray) { serialize(jsonElement, gen, provider); } gen.writeEndArray(); } else if (jsonLever.isPrimitive(value)) { JsonPrimitive jsonPrimitive = value.getAsJsonPrimitive(); if (jsonPrimitive.isBoolean()) { gen.writeBoolean(jsonPrimitive.getAsBoolean()); } if (jsonPrimitive.isNumber()) { Number nnode = jsonPrimitive.getAsNumber(); if (nnode instanceof LazilyParsedNumber) { gen.writeNumber(nnode.toString()); } else if (nnode instanceof Integer) { gen.writeNumber(nnode.intValue()); } else if (nnode instanceof Short) { gen.writeNumber(nnode.shortValue()); } else if (nnode instanceof BigInteger || nnode instanceof Long) { gen.writeNumber(nnode.longValue()); } else if (nnode instanceof Float) { gen.writeNumber(nnode.floatValue()); } else if (nnode instanceof Double || nnode instanceof BigDecimal) { gen.writeNumber(nnode.doubleValue()); } } if (jsonPrimitive.isString()) { gen.writeString(jsonPrimitive.getAsString()); } } }
From source file:com.balajeetm.mystique.util.gson.lever.JsonComparator.java
License:Open Source License
/** * Checks if is subset./*from w w w . j a v a 2 s. c om*/ * * @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
/** * Simple merge of two json objects. Json fields are not recursively merged * * @param to the json object to which the other json must be merged * @param from the json object which should be merged * @return the merged json object// www . j av a2 s . co m */ public JsonObject simpleMerge(JsonObject to, JsonObject from) { from = asJsonObject(from, new JsonObject()); to = asJsonObject(to, new JsonObject()); for (Entry<String, JsonElement> entry : from.entrySet()) { to.add(entry.getKey(), entry.getValue()); } return to; }
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 */// w w w .j a va 2s. 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.baomidou.springwind.util.yuntongxin.CCPRestSDK.java
License:Open Source License
private HashMap<String, Object> jsonToMap(String result) { HashMap<String, Object> hashMap = new HashMap<String, Object>(); JsonParser parser = new JsonParser(); JsonObject asJsonObject = parser.parse(result).getAsJsonObject(); Set<Entry<String, JsonElement>> entrySet = asJsonObject.entrySet(); HashMap<String, Object> hashMap2 = new HashMap<String, Object>(); for (Entry<String, JsonElement> m : entrySet) { if ("statusCode".equals(m.getKey()) || "statusMsg".equals(m.getKey())) hashMap.put(m.getKey(), m.getValue().getAsString()); else {/*from www .j a va 2s . c o m*/ if ("SubAccount".equals(m.getKey()) || "totalCount".equals(m.getKey()) || "smsTemplateList".equals(m.getKey()) || "token".equals(m.getKey()) || "callSid".equals(m.getKey()) || "state".equals(m.getKey()) || "downUrl".equals(m.getKey())) { if (!"SubAccount".equals(m.getKey()) && !"smsTemplateList".equals(m.getKey())) hashMap2.put(m.getKey(), m.getValue().getAsString()); else { try { if ((m.getValue().toString().trim().length() <= 2) && !m.getValue().toString().contains("[")) { hashMap2.put(m.getKey(), m.getValue().getAsString()); hashMap.put("data", hashMap2); break; } if (m.getValue().toString().contains("[]")) { hashMap2.put(m.getKey(), new JsonArray()); hashMap.put("data", hashMap2); continue; } JsonArray asJsonArray = parser.parse(m.getValue().toString()).getAsJsonArray(); ArrayList<HashMap<String, Object>> arrayList = new ArrayList<HashMap<String, Object>>(); for (JsonElement j : asJsonArray) { Set<Entry<String, JsonElement>> entrySet2 = j.getAsJsonObject().entrySet(); HashMap<String, Object> hashMap3 = new HashMap<String, Object>(); for (Entry<String, JsonElement> m2 : entrySet2) { hashMap3.put(m2.getKey(), m2.getValue().getAsString()); } arrayList.add(hashMap3); } hashMap2.put(m.getKey(), arrayList); } catch (Exception e) { JsonObject asJsonObject2 = parser.parse(m.getValue().toString()).getAsJsonObject(); Set<Entry<String, JsonElement>> entrySet2 = asJsonObject2.entrySet(); HashMap<String, Object> hashMap3 = new HashMap<String, Object>(); for (Entry<String, JsonElement> m2 : entrySet2) { hashMap3.put(m2.getKey(), m2.getValue().getAsString()); } hashMap2.put(m.getKey(), hashMap3); hashMap.put("data", hashMap2); } } hashMap.put("data", hashMap2); } else { JsonObject asJsonObject2 = parser.parse(m.getValue().toString()).getAsJsonObject(); Set<Entry<String, JsonElement>> entrySet2 = asJsonObject2.entrySet(); HashMap<String, Object> hashMap3 = new HashMap<String, Object>(); for (Entry<String, JsonElement> m2 : entrySet2) { hashMap3.put(m2.getKey(), m2.getValue().getAsString()); } if (hashMap3.size() != 0) { hashMap2.put(m.getKey(), hashMap3); } else { hashMap2.put(m.getKey(), m.getValue().getAsString()); } hashMap.put("data", hashMap2); } } } return hashMap; }
From source file:com.bedatadriven.rebar.appcache.server.DefaultSelectionServlet.java
License:Apache License
private boolean matches(Map<String, String> properties, JsonObject permutation) { String strongName = permutation.get("permutation").getAsString(); JsonObject permProperties = permutation.getAsJsonObject("properties"); for (Map.Entry<String, JsonElement> property : permProperties.entrySet()) { String expected = property.getValue().getAsString(); String actual = properties.get(property.getKey()); if (actual != null && !expected.equals(actual)) { logger.finest("Rejecting " + strongName + ", expected property '" + property.getValue() + "' " + "with value '" + expected + "', found '" + actual + "'"); return false; }//w w w . j a v a 2 s . c o m } return true; }
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"); }/*from w w w. j a v a 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); }
From source file:com.bizideal.whoami.utils.cloopen.CCPRestSmsSDK.java
License:Open Source License
private HashMap<String, Object> jsonToMap(String result) { HashMap<String, Object> hashMap = new HashMap<String, Object>(); JsonParser parser = new JsonParser(); JsonObject asJsonObject = parser.parse(result).getAsJsonObject(); Set<Entry<String, JsonElement>> entrySet = asJsonObject.entrySet(); HashMap<String, Object> hashMap2 = new HashMap<String, Object>(); for (Map.Entry<String, JsonElement> m : entrySet) { if ("statusCode".equals(m.getKey()) || "statusMsg".equals(m.getKey())) hashMap.put(m.getKey(), m.getValue().getAsString()); else {// w w w . ja v a 2 s .c o m if ("SubAccount".equals(m.getKey()) || "totalCount".equals(m.getKey()) || "token".equals(m.getKey()) || "downUrl".equals(m.getKey())) { if (!"SubAccount".equals(m.getKey())) hashMap2.put(m.getKey(), m.getValue().getAsString()); else { try { if ((m.getValue().toString().trim().length() <= 2) && !m.getValue().toString().contains("[")) { hashMap2.put(m.getKey(), m.getValue().getAsString()); hashMap.put("data", hashMap2); break; } if (m.getValue().toString().contains("[]")) { hashMap2.put(m.getKey(), new JsonArray()); hashMap.put("data", hashMap2); continue; } JsonArray asJsonArray = parser.parse(m.getValue().toString()).getAsJsonArray(); ArrayList<HashMap<String, Object>> arrayList = new ArrayList<HashMap<String, Object>>(); for (JsonElement j : asJsonArray) { Set<Entry<String, JsonElement>> entrySet2 = j.getAsJsonObject().entrySet(); HashMap<String, Object> hashMap3 = new HashMap<String, Object>(); for (Map.Entry<String, JsonElement> m2 : entrySet2) { hashMap3.put(m2.getKey(), m2.getValue().getAsString()); } arrayList.add(hashMap3); } hashMap2.put("SubAccount", arrayList); } catch (Exception e) { JsonObject asJsonObject2 = parser.parse(m.getValue().toString()).getAsJsonObject(); Set<Entry<String, JsonElement>> entrySet2 = asJsonObject2.entrySet(); HashMap<String, Object> hashMap3 = new HashMap<String, Object>(); for (Map.Entry<String, JsonElement> m2 : entrySet2) { hashMap3.put(m2.getKey(), m2.getValue().getAsString()); } hashMap2.put(m.getKey(), hashMap3); hashMap.put("data", hashMap2); } } hashMap.put("data", hashMap2); } else { JsonObject asJsonObject2 = parser.parse(m.getValue().toString()).getAsJsonObject(); Set<Entry<String, JsonElement>> entrySet2 = asJsonObject2.entrySet(); HashMap<String, Object> hashMap3 = new HashMap<String, Object>(); for (Map.Entry<String, JsonElement> m2 : entrySet2) { hashMap3.put(m2.getKey(), m2.getValue().getAsString()); } if (hashMap3.size() != 0) { hashMap2.put(m.getKey(), hashMap3); } else { hashMap2.put(m.getKey(), m.getValue().getAsString()); } hashMap.put("data", hashMap2); } } } return hashMap; }