List of usage examples for com.google.gson JsonObject entrySet
public Set<Map.Entry<String, JsonElement>> entrySet()
From source file:com.doitnext.swing.widgets.json.JSONJTreeNode.java
License:Apache License
public JsonElement asJsonElement() { StringBuilder sb = new StringBuilder(); buildJsonString(sb);// w ww .j a va2s .co m String json = sb.toString().trim(); if (json.startsWith("{") || json.startsWith("[")) return new JsonParser().parse(sb.toString()); else { // Safety check the JSON, if it is of a named value object // We cheat a little if it is an orphan name value pair then // if we wrap it in {} chars it will parse if it isn't the parse // fails. String testValue = "{" + json + "}"; try { JsonElement wrapperElt = new JsonParser().parse(testValue); JsonObject obj = (JsonObject) wrapperElt; Iterator<Entry<String, JsonElement>> it = obj.entrySet().iterator(); Entry<String, JsonElement> entry = it.next(); return entry.getValue(); } catch (JsonSyntaxException jse) { JsonElement rawElement = new JsonParser().parse(json); return rawElement; } } }
From source file:com.dsh105.powermessage.core.PowerMessage.java
License:Open Source License
/** * Converts a raw JSON string to a PowerMessage object. This JSON string is in the format given by {@link #toJson()} * @param json the JSON string which represents a PowerMessage * @return A PowerMessage representing the given JSON string */// w w w .ja v a 2s.c o m public static PowerMessage fromJson(String json) { JsonArray serialised = jsonParser.parse(json).getAsJsonObject().getAsJsonArray("extra"); PowerMessage powerMessage = new PowerMessage(); for (JsonElement serialisedElement : serialised) { PowerSnippet snippet = new PowerSnippet(""); JsonObject message = serialisedElement.getAsJsonObject(); for (Map.Entry<String, JsonElement> entry : message.entrySet()) { String key = entry.getKey(); JsonElement element = entry.getValue(); if (key.equals("text")) { snippet.setText(element.getAsString()); } else if (PowerSnippet.STYLE_TO_NAME_MAP.containsValue(key)) { if (element.getAsBoolean()) { snippet.withColour(PowerSnippet.STYLE_TO_NAME_MAP.inverse().get(key)); } } else if (key.equals("color")) { snippet.withColour(ChatColor.valueOf(element.getAsString().toUpperCase())); } else if (key.equals("clickEvent") || key.equals("hoverEvent")) { JsonObject event = element.getAsJsonObject(); snippet.withEvent(key.substring(0, key.indexOf("Event")), event.get("action").getAsString(), event.get("value").getAsString()); } } powerMessage.then(snippet); } return powerMessage; }
From source file:com.edduarte.argus.reader.JsonReader.java
License:Apache License
private void readRecursive(JsonElement root, MutableString collector) { if (root.isJsonObject()) { JsonObject object = (JsonObject) root; object.entrySet().forEach(e -> { collector.append(e.getKey()); collector.append(' '); readRecursive(e.getValue(), collector); collector.append(' '); });//from w w w .j ava 2 s. co m } else if (root.isJsonArray()) { JsonArray array = (JsonArray) root; array.forEach(child -> { readRecursive(child, collector); collector.append(' '); }); } else if (root.isJsonPrimitive()) { JsonPrimitive primitive = (JsonPrimitive) root; collector.append(primitive.getAsString()); collector.append(' '); } }
From source file:com.ericsson.eiffel.remrem.semantics.schemas.SchemaFile.java
License:Apache License
/** * /*from w w w . j a v a 2 s.co m*/ * This method is used to adding the attributes JavaType ,ExtendsJavaType * and other required properties to input jsons * * @param jsonContent * -Eiffel repo json file content sent as an input parameter * @param jsonElementName * -Name of the json elements as an input parameter * @param jsonObject * -Json object is sent as an input to this to add required json * properties to generate event pojo's */ private void addAttributesToJsonSchema(JsonObject jsonContent, String jsonElementName, JsonObject jsonObject) { Set<Entry<String, JsonElement>> values = jsonContent.entrySet(); Iterator<Entry<String, JsonElement>> keyValue = values.iterator(); while (keyValue.hasNext()) { Entry<String, JsonElement> valueSet = keyValue.next(); if (valueSet.getValue().isJsonObject()) { String name = valueSet.getKey(); String previousObjectName = jsonElementName; if (name.equals(EiffelConstants.META)) { isMeta = true; } if (name.equals(EiffelConstants.TYPE) && isMeta) { isMeta = false; } addingItemsProperties(name, valueSet.getValue(), jsonObject, previousObjectName); } else { if (EiffelConstants.TYPE.equals(valueSet.getKey())) { if (EiffelConstants.OBJECTTYPE.equals(valueSet.getValue().getAsString())) { jsonObject.add(EiffelConstants.TYPE, valueSet.getValue()); if (isEvent) { jsonObject.add(EiffelConstants.JAVA_TYPE, parser.parse(EiffelConstants.COM_ERICSSON_EIFFEL_SEMANTICS_EVENTS .concat(StringUtils.capitalize(jsonElementName)))); jsonObject.add(EiffelConstants.EXTENDS_JAVA_CLASS, parser.parse(EiffelConstants.COM_ERICSSON_EIFFEL_SEMANTICS_EVENTS_EVENT)); isEvent = false; } else { jsonElementName = modifyClassName(jsonElementName); String newClassName = StringUtils.capitalize(jsonElementName); if (jsonElementName.equals(EiffelConstants.META)) { // To generate event specific Meta class jsonObject.add(EiffelConstants.JAVA_TYPE, parser.parse(EiffelConstants.COM_ERICSSON_EIFFEL_SEMANTICS_EVENTS .concat(this.eventName + "" + newClassName))); JsonArray list = new JsonArray(); list.add("com.ericsson.eiffel.semantics.events.Meta"); jsonObject.add(EiffelConstants.JAVA_INTERFACES, list); } else if (jsonElementName.equals(EiffelConstants.DATA) || jsonElementName.equals(EiffelConstants.OUTCOME)) { // Data and Outcome is different at event level jsonObject.add(EiffelConstants.JAVA_TYPE, parser.parse(EiffelConstants.COM_ERICSSON_EIFFEL_SEMANTICS_EVENTS .concat(this.eventName + "" + newClassName))); } else if (jsonElementName.equals(EiffelConstants.ISSUE)) { jsonObject.add(EiffelConstants.JAVA_TYPE, parser.parse(EiffelConstants.COM_ERICSSON_EIFFEL_SEMANTICS_EVENTS .concat(this.eventName + "" + newClassName))); } else { jsonObject.add(EiffelConstants.JAVA_TYPE, parser.parse( EiffelConstants.COM_ERICSSON_EIFFEL_SEMANTICS_EVENTS.concat(newClassName))); } } } else { jsonObject.add(EiffelConstants.TYPE, valueSet.getValue()); if (jsonElementName.equals(EiffelConstants.TIME)) { jsonObject.add(EiffelConstants.FORMAT, parser.parse(EiffelConstants.UTC_MILLISEC)); } } } else { jsonObject.add(valueSet.getKey(), valueSet.getValue()); } } } if (values.isEmpty()) { // If value field doesn't have any data type we make it to // accept either object or string value JsonArray array = new JsonArray(); JsonObject obj1 = new JsonObject(); JsonObject obj2 = new JsonObject(); JsonObject obj3 = new JsonObject(); JsonObject obj4 = new JsonObject(); obj1.add(EiffelConstants.TYPE, parser.parse(EiffelConstants.OBJECTTYPE)); obj2.add(EiffelConstants.TYPE, parser.parse(EiffelConstants.STRING)); obj3.add(EiffelConstants.TYPE, parser.parse(EiffelConstants.ARRAY)); obj4.add(EiffelConstants.TYPE, parser.parse(EiffelConstants.NUMBER)); array.add(obj1); array.add(obj2); array.add(obj3); array.add(obj4); jsonObject.add(EiffelConstants.ANYOF, array); } }
From source file:com.etermax.conversations.repository.impl.elasticsearch.mapper.RuntimeTypeAdapterFactory.java
License:Apache License
public <R> TypeAdapter<R> create(Gson gson, TypeToken<R> type) { if (type.getRawType() != baseType) { return null; }//from w ww . jav a 2 s .co m final Map<String, TypeAdapter<?>> labelToDelegate = new LinkedHashMap<String, TypeAdapter<?>>(); final Map<Class<?>, TypeAdapter<?>> subtypeToDelegate = new LinkedHashMap<Class<?>, TypeAdapter<?>>(); for (Map.Entry<String, Class<?>> entry : labelToSubtype.entrySet()) { TypeAdapter<?> delegate = gson.getDelegateAdapter(this, TypeToken.get(entry.getValue())); labelToDelegate.put(entry.getKey(), delegate); subtypeToDelegate.put(entry.getValue(), delegate); } return new TypeAdapter<R>() { @Override public R read(JsonReader in) throws IOException { JsonElement jsonElement = Streams.parse(in); JsonElement labelJsonElement = jsonElement.getAsJsonObject().remove(typeFieldName); if (labelJsonElement == null) { throw new JsonParseException("cannot deserialize " + baseType + " because it does not define a field named " + typeFieldName); } String label = labelJsonElement.getAsString(); @SuppressWarnings("unchecked") // registration requires that subtype extends T TypeAdapter<R> delegate = (TypeAdapter<R>) labelToDelegate.get(label); if (delegate == null) { throw new JsonParseException("cannot deserialize " + baseType + " subtype named " + label + "; did you forget to register a subtype?"); } return delegate.fromJsonTree(jsonElement); } @Override public void write(JsonWriter out, R value) throws IOException { Class<?> srcType = value.getClass(); String label = subtypeToLabel.get(srcType); @SuppressWarnings("unchecked") // registration requires that subtype extends T TypeAdapter<R> delegate = (TypeAdapter<R>) subtypeToDelegate.get(srcType); if (delegate == null) { throw new JsonParseException( "cannot serialize " + srcType.getName() + "; did you forget to register a subtype?"); } JsonObject jsonObject = delegate.toJsonTree(value).getAsJsonObject(); if (jsonObject.has(typeFieldName)) { throw new JsonParseException("cannot serialize " + srcType.getName() + " because it already defines a field named " + typeFieldName); } JsonObject clone = new JsonObject(); clone.add(typeFieldName, new JsonPrimitive(label)); for (Map.Entry<String, JsonElement> e : jsonObject.entrySet()) { clone.add(e.getKey(), e.getValue()); } Streams.write(clone, out); } }.nullSafe(); }
From source file:com.evolveum.polygon.connector.googleapps.GoogleAppsUtil.java
License:Open Source License
private static ArrayMap<String, Object> parseJson(String json) { JsonObject object = (JsonObject) new com.google.gson.JsonParser().parse(json); Set<Map.Entry<String, JsonElement>> set = object.entrySet(); Iterator<Map.Entry<String, JsonElement>> iterator = set.iterator(); ArrayMap<String, Object> map = new ArrayMap<String, Object>(); while (iterator.hasNext()) { Map.Entry<String, JsonElement> entry = iterator.next(); String key = entry.getKey(); JsonElement value = entry.getValue(); if (!value.isJsonPrimitive()) { map.put(key, parseJson(value.toString())); } else {/*from w w w . j a v a2 s. c o m*/ map.put(key, value.getAsString()); } } return map; }
From source file:com.facebook.ads.sdk.AdAccount.java
License:Open Source License
public static APINodeList<AdAccount> parseResponse(String json, APIContext context, APIRequest request) throws MalformedResponseException { APINodeList<AdAccount> adAccounts = new APINodeList<AdAccount>(request, json); JsonArray arr;//w w w.j a va2 s.com JsonObject obj; JsonParser parser = new JsonParser(); Exception exception = null; try { JsonElement result = parser.parse(json); if (result.isJsonArray()) { // First, check if it's a pure JSON Array arr = result.getAsJsonArray(); for (int i = 0; i < arr.size(); i++) { adAccounts.add(loadJSON(arr.get(i).getAsJsonObject().toString(), context)); } ; return adAccounts; } else if (result.isJsonObject()) { obj = result.getAsJsonObject(); if (obj.has("data")) { if (obj.has("paging")) { JsonObject paging = obj.get("paging").getAsJsonObject().get("cursors").getAsJsonObject(); String before = paging.has("before") ? paging.get("before").getAsString() : null; String after = paging.has("after") ? paging.get("after").getAsString() : null; adAccounts.setPaging(before, after); } if (obj.get("data").isJsonArray()) { // Second, check if it's a JSON array with "data" arr = obj.get("data").getAsJsonArray(); for (int i = 0; i < arr.size(); i++) { adAccounts.add(loadJSON(arr.get(i).getAsJsonObject().toString(), context)); } ; } else if (obj.get("data").isJsonObject()) { // Third, check if it's a JSON object with "data" obj = obj.get("data").getAsJsonObject(); boolean isRedownload = false; for (String s : new String[] { "campaigns", "adsets", "ads" }) { if (obj.has(s)) { isRedownload = true; obj = obj.getAsJsonObject(s); for (Map.Entry<String, JsonElement> entry : obj.entrySet()) { adAccounts.add(loadJSON(entry.getValue().toString(), context)); } break; } } if (!isRedownload) { adAccounts.add(loadJSON(obj.toString(), context)); } } return adAccounts; } else if (obj.has("images")) { // Fourth, check if it's a map of image objects obj = obj.get("images").getAsJsonObject(); for (Map.Entry<String, JsonElement> entry : obj.entrySet()) { adAccounts.add(loadJSON(entry.getValue().toString(), context)); } return adAccounts; } else { // Fifth, check if it's an array of objects indexed by id boolean isIdIndexedArray = true; for (Map.Entry entry : obj.entrySet()) { String key = (String) entry.getKey(); if (key.equals("__fb_trace_id__")) { continue; } JsonElement value = (JsonElement) entry.getValue(); if (value != null && value.isJsonObject() && value.getAsJsonObject().has("id") && value.getAsJsonObject().get("id") != null && value.getAsJsonObject().get("id").getAsString().equals(key)) { adAccounts.add(loadJSON(value.toString(), context)); } else { isIdIndexedArray = false; break; } } if (isIdIndexedArray) { return adAccounts; } // Sixth, check if it's pure JsonObject adAccounts.clear(); adAccounts.add(loadJSON(json, context)); return adAccounts; } } } catch (Exception e) { exception = e; } throw new MalformedResponseException("Invalid response string: " + json, exception); }
From source file:com.facebook.ads.sdk.AdAccountDeliveryEstimate.java
License:Open Source License
public static APINodeList<AdAccountDeliveryEstimate> parseResponse(String json, APIContext context, APIRequest request) throws MalformedResponseException { APINodeList<AdAccountDeliveryEstimate> adAccountDeliveryEstimates = new APINodeList<AdAccountDeliveryEstimate>( request, json);/*www .j ava2 s . com*/ JsonArray arr; JsonObject obj; JsonParser parser = new JsonParser(); Exception exception = null; try { JsonElement result = parser.parse(json); if (result.isJsonArray()) { // First, check if it's a pure JSON Array arr = result.getAsJsonArray(); for (int i = 0; i < arr.size(); i++) { adAccountDeliveryEstimates.add(loadJSON(arr.get(i).getAsJsonObject().toString(), context)); } ; return adAccountDeliveryEstimates; } else if (result.isJsonObject()) { obj = result.getAsJsonObject(); if (obj.has("data")) { if (obj.has("paging")) { JsonObject paging = obj.get("paging").getAsJsonObject().get("cursors").getAsJsonObject(); String before = paging.has("before") ? paging.get("before").getAsString() : null; String after = paging.has("after") ? paging.get("after").getAsString() : null; adAccountDeliveryEstimates.setPaging(before, after); } if (obj.get("data").isJsonArray()) { // Second, check if it's a JSON array with "data" arr = obj.get("data").getAsJsonArray(); for (int i = 0; i < arr.size(); i++) { adAccountDeliveryEstimates .add(loadJSON(arr.get(i).getAsJsonObject().toString(), context)); } ; } else if (obj.get("data").isJsonObject()) { // Third, check if it's a JSON object with "data" obj = obj.get("data").getAsJsonObject(); boolean isRedownload = false; for (String s : new String[] { "campaigns", "adsets", "ads" }) { if (obj.has(s)) { isRedownload = true; obj = obj.getAsJsonObject(s); for (Map.Entry<String, JsonElement> entry : obj.entrySet()) { adAccountDeliveryEstimates.add(loadJSON(entry.getValue().toString(), context)); } break; } } if (!isRedownload) { adAccountDeliveryEstimates.add(loadJSON(obj.toString(), context)); } } return adAccountDeliveryEstimates; } else if (obj.has("images")) { // Fourth, check if it's a map of image objects obj = obj.get("images").getAsJsonObject(); for (Map.Entry<String, JsonElement> entry : obj.entrySet()) { adAccountDeliveryEstimates.add(loadJSON(entry.getValue().toString(), context)); } return adAccountDeliveryEstimates; } else { // Fifth, check if it's an array of objects indexed by id boolean isIdIndexedArray = true; for (Map.Entry entry : obj.entrySet()) { String key = (String) entry.getKey(); if (key.equals("__fb_trace_id__")) { continue; } JsonElement value = (JsonElement) entry.getValue(); if (value != null && value.isJsonObject() && value.getAsJsonObject().has("id") && value.getAsJsonObject().get("id") != null && value.getAsJsonObject().get("id").getAsString().equals(key)) { adAccountDeliveryEstimates.add(loadJSON(value.toString(), context)); } else { isIdIndexedArray = false; break; } } if (isIdIndexedArray) { return adAccountDeliveryEstimates; } // Sixth, check if it's pure JsonObject adAccountDeliveryEstimates.clear(); adAccountDeliveryEstimates.add(loadJSON(json, context)); return adAccountDeliveryEstimates; } } } catch (Exception e) { exception = e; } throw new MalformedResponseException("Invalid response string: " + json, exception); }
From source file:com.facebook.ads.sdk.AdAccountGroup.java
License:Open Source License
public static APINodeList<AdAccountGroup> parseResponse(String json, APIContext context, APIRequest request) throws MalformedResponseException { APINodeList<AdAccountGroup> adAccountGroups = new APINodeList<AdAccountGroup>(request, json); JsonArray arr;//from ww w . j a va2 s . co m JsonObject obj; JsonParser parser = new JsonParser(); Exception exception = null; try { JsonElement result = parser.parse(json); if (result.isJsonArray()) { // First, check if it's a pure JSON Array arr = result.getAsJsonArray(); for (int i = 0; i < arr.size(); i++) { adAccountGroups.add(loadJSON(arr.get(i).getAsJsonObject().toString(), context)); } ; return adAccountGroups; } else if (result.isJsonObject()) { obj = result.getAsJsonObject(); if (obj.has("data")) { if (obj.has("paging")) { JsonObject paging = obj.get("paging").getAsJsonObject().get("cursors").getAsJsonObject(); String before = paging.has("before") ? paging.get("before").getAsString() : null; String after = paging.has("after") ? paging.get("after").getAsString() : null; adAccountGroups.setPaging(before, after); } if (obj.get("data").isJsonArray()) { // Second, check if it's a JSON array with "data" arr = obj.get("data").getAsJsonArray(); for (int i = 0; i < arr.size(); i++) { adAccountGroups.add(loadJSON(arr.get(i).getAsJsonObject().toString(), context)); } ; } else if (obj.get("data").isJsonObject()) { // Third, check if it's a JSON object with "data" obj = obj.get("data").getAsJsonObject(); boolean isRedownload = false; for (String s : new String[] { "campaigns", "adsets", "ads" }) { if (obj.has(s)) { isRedownload = true; obj = obj.getAsJsonObject(s); for (Map.Entry<String, JsonElement> entry : obj.entrySet()) { adAccountGroups.add(loadJSON(entry.getValue().toString(), context)); } break; } } if (!isRedownload) { adAccountGroups.add(loadJSON(obj.toString(), context)); } } return adAccountGroups; } else if (obj.has("images")) { // Fourth, check if it's a map of image objects obj = obj.get("images").getAsJsonObject(); for (Map.Entry<String, JsonElement> entry : obj.entrySet()) { adAccountGroups.add(loadJSON(entry.getValue().toString(), context)); } return adAccountGroups; } else { // Fifth, check if it's an array of objects indexed by id boolean isIdIndexedArray = true; for (Map.Entry entry : obj.entrySet()) { String key = (String) entry.getKey(); if (key.equals("__fb_trace_id__")) { continue; } JsonElement value = (JsonElement) entry.getValue(); if (value != null && value.isJsonObject() && value.getAsJsonObject().has("id") && value.getAsJsonObject().get("id") != null && value.getAsJsonObject().get("id").getAsString().equals(key)) { adAccountGroups.add(loadJSON(value.toString(), context)); } else { isIdIndexedArray = false; break; } } if (isIdIndexedArray) { return adAccountGroups; } // Sixth, check if it's pure JsonObject adAccountGroups.clear(); adAccountGroups.add(loadJSON(json, context)); return adAccountGroups; } } } catch (Exception e) { exception = e; } throw new MalformedResponseException("Invalid response string: " + json, exception); }
From source file:com.facebook.ads.sdk.AdAccountGroupResult.java
License:Open Source License
public static APINodeList<AdAccountGroupResult> parseResponse(String json, APIContext context, APIRequest request) throws MalformedResponseException { APINodeList<AdAccountGroupResult> adAccountGroupResults = new APINodeList<AdAccountGroupResult>(request, json);/* w w w . ja v a 2s .c o m*/ JsonArray arr; JsonObject obj; JsonParser parser = new JsonParser(); Exception exception = null; try { JsonElement result = parser.parse(json); if (result.isJsonArray()) { // First, check if it's a pure JSON Array arr = result.getAsJsonArray(); for (int i = 0; i < arr.size(); i++) { adAccountGroupResults.add(loadJSON(arr.get(i).getAsJsonObject().toString(), context)); } ; return adAccountGroupResults; } else if (result.isJsonObject()) { obj = result.getAsJsonObject(); if (obj.has("data")) { if (obj.has("paging")) { JsonObject paging = obj.get("paging").getAsJsonObject().get("cursors").getAsJsonObject(); String before = paging.has("before") ? paging.get("before").getAsString() : null; String after = paging.has("after") ? paging.get("after").getAsString() : null; adAccountGroupResults.setPaging(before, after); } if (obj.get("data").isJsonArray()) { // Second, check if it's a JSON array with "data" arr = obj.get("data").getAsJsonArray(); for (int i = 0; i < arr.size(); i++) { adAccountGroupResults.add(loadJSON(arr.get(i).getAsJsonObject().toString(), context)); } ; } else if (obj.get("data").isJsonObject()) { // Third, check if it's a JSON object with "data" obj = obj.get("data").getAsJsonObject(); boolean isRedownload = false; for (String s : new String[] { "campaigns", "adsets", "ads" }) { if (obj.has(s)) { isRedownload = true; obj = obj.getAsJsonObject(s); for (Map.Entry<String, JsonElement> entry : obj.entrySet()) { adAccountGroupResults.add(loadJSON(entry.getValue().toString(), context)); } break; } } if (!isRedownload) { adAccountGroupResults.add(loadJSON(obj.toString(), context)); } } return adAccountGroupResults; } else if (obj.has("images")) { // Fourth, check if it's a map of image objects obj = obj.get("images").getAsJsonObject(); for (Map.Entry<String, JsonElement> entry : obj.entrySet()) { adAccountGroupResults.add(loadJSON(entry.getValue().toString(), context)); } return adAccountGroupResults; } else { // Fifth, check if it's an array of objects indexed by id boolean isIdIndexedArray = true; for (Map.Entry entry : obj.entrySet()) { String key = (String) entry.getKey(); if (key.equals("__fb_trace_id__")) { continue; } JsonElement value = (JsonElement) entry.getValue(); if (value != null && value.isJsonObject() && value.getAsJsonObject().has("id") && value.getAsJsonObject().get("id") != null && value.getAsJsonObject().get("id").getAsString().equals(key)) { adAccountGroupResults.add(loadJSON(value.toString(), context)); } else { isIdIndexedArray = false; break; } } if (isIdIndexedArray) { return adAccountGroupResults; } // Sixth, check if it's pure JsonObject adAccountGroupResults.clear(); adAccountGroupResults.add(loadJSON(json, context)); return adAccountGroupResults; } } } catch (Exception e) { exception = e; } throw new MalformedResponseException("Invalid response string: " + json, exception); }