List of usage examples for com.google.gson JsonObject entrySet
public Set<Map.Entry<String, JsonElement>> entrySet()
From source file:com.couchbase.cbadmin.client.CouchbaseAdminImpl.java
License:Open Source License
private JsonObject mergeViews(JsonObject design, JsonObject definition) { JsonObject views = definition.get("views").getAsJsonObject(); JsonObject currentViews = design != null ? design.get("views").getAsJsonObject() : new JsonObject(); for (Entry<String, JsonElement> entry : currentViews.entrySet()) { if (views.has(entry.getKey())) { views.remove(entry.getKey()); }//from w w w. j a va 2s .c om views.add(entry.getKey(), entry.getValue()); } return definition; }
From source file:com.couchbase.cbadmin.client.RebalanceInfo.java
License:Open Source License
public RebalanceInfo(JsonObject obj) throws RestApiException { JsonElement e = obj.get("status"); if (e == null || e.isJsonPrimitive() == false) { throw new RestApiException("Expected status string", obj); }//w w w. j a v a2 s. c o m String sStatus = e.getAsString(); if (sStatus.equals("none")) { completed = true; } else if (sStatus.equals("running")) { for (Entry<String, JsonElement> ent : obj.entrySet()) { if (ent.getKey().equals("status")) { continue; } JsonObject progressObj; if (ent.getValue().isJsonObject() == false) { throw new RestApiException("Expected object", ent.getValue()); } progressObj = ent.getValue().getAsJsonObject(); JsonElement progressNum = progressObj.get("progress"); if (progressNum.isJsonPrimitive() == false || progressNum.getAsJsonPrimitive().isNumber() == false) { throw new RestApiException("Expected 'progress' to be number", progressNum); } details.put(ent.getKey(), progressNum.getAsNumber().floatValue() * 100); } } }
From source file:com.couchbase.plugin.basement.api.Client.java
License:Open Source License
public HashMap<String, Object> parse(String json) { JsonParser parser = new JsonParser(); JsonObject object = (JsonObject) parser.parse(json); Set<Map.Entry<String, JsonElement>> set = object.entrySet(); Iterator<Map.Entry<String, JsonElement>> iterator = set.iterator(); HashMap<String, Object> map = new HashMap<String, Object>(); while (iterator.hasNext()) { Map.Entry<String, JsonElement> entry = iterator.next(); String key = entry.getKey(); JsonElement value = entry.getValue(); addJsonElement(key, value, map); }// w ww . ja v a 2s.co m return map; }
From source file:com.crypticbit.diff.demo.swing.contacts.JsonJTreeNode.java
License:Apache License
public JsonElement asJsonElement() { StringBuilder sb = new StringBuilder(); buildJsonString(sb);//from w w w . j a v a 2 s .c o 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.cybozu.kintone.database.JsonParser.java
License:Apache License
/** * Reads and parses each record element. * @param elem//from w ww . j a va 2 s .com * a json element represents a record object * @return the record object created * @throws IOException */ private Record readRecord(JsonElement elem) throws IOException { Record record = new Record(); if (elem.isJsonObject()) { JsonObject obj = elem.getAsJsonObject(); Set<Map.Entry<String, JsonElement>> set = obj.entrySet(); for (Map.Entry<String, JsonElement> entry : set) { Field field = readField(entry.getKey(), entry.getValue()); if (field != null) { record.addField(field.getName(), field); } } } return record; }
From source file:com.dangdang.ddframe.job.cloud.scheduler.mesos.MesosSlaveService.java
License:Apache License
private void fillReservedResources(final Map<String, JsonObject> roleMap, final JsonObject reservedResourcesFull) { if (reservedResourcesFull.isJsonNull()) { return;/*from w w w . j a v a 2 s . com*/ } for (Map.Entry<String, JsonElement> each : reservedResourcesFull.entrySet()) { for (JsonElement eachResource : each.getValue().getAsJsonArray()) { filledResource(roleMap, eachResource.getAsJsonObject(), "reserved_resources"); } } }
From source file:com.datascience.serialization.json.JSONUtils.java
License:Open Source License
public static JsonObject tKeys(JsonObject jo) { JsonObject ret = new JsonObject(); for (Map.Entry<String, JsonElement> e : jo.entrySet()) { ret.add(t(e.getKey()), e.getValue()); }//from w w w.j a va2 s. com return ret; }
From source file:com.denimgroup.threadfix.cli.util.JsonTestUtils.java
License:Mozilla Public License
private static void innerValidate(JsonObject jsonObject, String[] fields) { assert jsonObject != null : "Null object passed to innerValidate. Fix the code."; Set<String> missingFields = new HashSet<String>(); for (String field : fields) { if (!jsonObject.has(field)) { missingFields.add(field);/*from ww w . j av a 2 s . c om*/ } } assert missingFields.isEmpty() : "JsonObject " + jsonObject + " was missing " + missingFields; if (jsonObject.entrySet().size() != fields.length) { Set<String> actualFields = new HashSet<String>(); for (Map.Entry<String, JsonElement> entry : jsonObject.entrySet()) { actualFields.add(entry.getKey()); } actualFields.removeAll(Arrays.asList(fields)); assert actualFields.isEmpty() : "The returned JSON had the extra fields : " + actualFields; } }
From source file:com.diversityarrays.dalclient.DalUtil.java
License:Open Source License
static public DalResponseRecord createFrom(String requestUrl, String tagName, JsonObject input) { DalResponseRecord result = new DalResponseRecord(requestUrl, tagName); for (Map.Entry<String, JsonElement> entry : input.entrySet()) { String key = entry.getKey(); JsonElement value = entry.getValue(); if (value == null || value.isJsonNull()) { result.rowdata.put(key, ""); } else if (value.isJsonArray()) { JsonArray array = (JsonArray) value; int count = 0; for (JsonElement elt : array) { if (elt != null && elt.isJsonObject()) { JsonObject child = (JsonObject) elt; Map<String, String> childMap = asRowdata(child); if (childMap != null) { result.addNestedData(key, childMap); }// w ww. j ava 2s .c om } else { result.warnings.add(String.format("unexpected value-type for '%s'[%d] :%s", //$NON-NLS-1$ key, count, elt == null ? "null" : elt.getClass().getName())); } ++count; } } else if (value.isJsonPrimitive()) { JsonPrimitive prim = (JsonPrimitive) value; result.rowdata.put(key, prim.getAsString()); } else if (value.isJsonObject()) { // ?? perhaps Map<String, String> childMap = asRowdata((JsonObject) value); if (childMap != null) { result.addNestedData(key, childMap); } } else { result.warnings.add(String.format("unexpected value-type for '%s' :%s", //$NON-NLS-1$ key, value.getClass().getName())); } } return result; }
From source file:com.diversityarrays.dalclient.DalUtil.java
License:Open Source License
/** * Convert the the input into a Map<String,String>. * @param input/*from w w w . j av a 2 s .c o m*/ * @return a Map<String,String> */ static private Map<String, String> asRowdata(JsonObject input) { Map<String, String> result = null; Set<Map.Entry<String, JsonElement>> entrySet = input.entrySet(); if (entrySet != null && !entrySet.isEmpty()) { for (Map.Entry<String, JsonElement> entry : entrySet) { if (result == null) { result = new LinkedHashMap<>(); } String key = entry.getKey(); JsonElement value = entry.getValue(); if (value == null || value.isJsonNull()) { result.put(key, ""); } else { result.put(key, value.getAsString()); } } } return result; }