List of usage examples for com.google.gson.internal LinkedTreeMap entrySet
EntrySet entrySet
To view the source code for com.google.gson.internal LinkedTreeMap entrySet.
Click Source Link
From source file:com.circles.model.ApplicationJSON.java
public Object PopulateFromJSONObjectMap(LinkedTreeMap<String, Object> jobj) { Hashtable<String, Object> childhash = new Hashtable<String, Object>(); System.out.println("map" + jobj.getClass().toString()); for (Map.Entry<String, Object> entry : jobj.entrySet()) { String childkey = entry.getKey(); Object objchild = processObject(jobj.get(childkey)); processParent(childkey, childhash, objchild); }/*w ww. j a v a 2 s. c o m*/ return childhash; }
From source file:com.microsoft.azure.sdk.iot.deps.serializer.TwinProperty.java
License:Open Source License
private void updateVersion(LinkedTreeMap<String, Object> jsonTree) { for (Map.Entry<String, Object> entry : jsonTree.entrySet()) { if (entry.getKey().equals(VERSION_TAG)) { version = new Integer((int) ((double) entry.getValue())); break; }// www .ja v a 2s . com } }
From source file:com.microsoft.azure.sdk.iot.deps.serializer.TwinProperty.java
License:Open Source License
private Map<String, Object> updateMetadata(LinkedTreeMap<String, Object> jsonTree) { Map<String, Object> diff = new HashMap<>(); for (Map.Entry<String, Object> entry : jsonTree.entrySet()) { if (entry.getKey().equals(METADATA_TAG)) { LinkedTreeMap<String, Object> metadataTree = (LinkedTreeMap<String, Object>) entry.getValue(); for (LinkedTreeMap.Entry<String, Object> item : metadataTree.entrySet()) { synchronized (lock) { if (property.containsKey(item.getKey())) { LinkedTreeMap<String, Object> itemTree = (LinkedTreeMap<String, Object>) item .getValue(); String lastUpdated = null; Integer lastUpdatedVersion = null; for (LinkedTreeMap.Entry<String, Object> metadataItem : itemTree.entrySet()) { if (metadataItem.getKey().equals(LAST_UPDATE_TAG)) { lastUpdated = metadataItem.getValue().toString(); } else if (metadataItem.getKey().equals(LAST_UPDATE_VERSION_TAG)) { lastUpdatedVersion = (int) ((double) metadataItem.getValue()); }//from w ww . j a v a 2s.co m } if (property.get(item.getKey()).metadata.update(lastUpdated, lastUpdatedVersion)) { diff.put(item.getKey(), item.getValue().toString()); } } } } break; } } return diff; }
From source file:com.microsoft.azure.sdk.iot.deps.serializer.TwinProperty.java
License:Open Source License
private Map<String, Object> updateFields(LinkedTreeMap<String, Object> jsonTree) throws IllegalArgumentException { Map<String, Object> diff = new HashMap<>(); for (Map.Entry<String, Object> entry : jsonTree.entrySet()) { if (entry.getKey().isEmpty()) { throw new IllegalArgumentException("Invalid Key on Json"); }/* w ww. jav a 2 s . c om*/ if (!entry.getKey().contains("$")) { synchronized (lock) { /* Codes_SRS_TWINPARSER_21_040: [The updateTwin shall not change fields that is not reported in the json string.] */ if (property.containsKey(entry.getKey())) { if (entry.getValue() == null) { /* Codes_SRS_TWINPARSER_21_042: [If a valid key has a null value, the updateTwin shall delete this property.] */ property.remove(entry.getKey()); diff.put(entry.getKey(), null); } else if (!property.get(entry.getKey()).value.equals(entry.getValue())) { property.put(entry.getKey(), new Property(entry.getValue(), null)); diff.put(entry.getKey(), entry.getValue().toString()); } } else if (entry.getValue() != null) { property.put(entry.getKey(), new Property(entry.getValue(), null)); diff.put(entry.getKey(), entry.getValue().toString()); } } } } return diff; }
From source file:io.bitsquare.pricefeed.providers.BtcAverageProvider.java
License:Open Source License
private Map<String, PriceData> getMap(String json) { Map<String, PriceData> marketPriceMap = new HashMap<>(); LinkedTreeMap<String, Object> treeMap = new Gson().<LinkedTreeMap<String, Object>>fromJson(json, LinkedTreeMap.class); treeMap.entrySet().stream().forEach(e -> { Object value = e.getValue(); // We need to check the type as we get an unexpected "timestamp" object at the end: if (value instanceof LinkedTreeMap) { LinkedTreeMap<String, Object> data = (LinkedTreeMap) value; String currencyCode = e.getKey().substring(3); marketPriceMap.put(currencyCode, new PriceData(currencyCode, (double) data.get("ask"), (double) data.get("bid"), (double) data.get("last"))); }/*from w w w .j a v a 2 s . co m*/ }); return marketPriceMap; }
From source file:org.cgiar.ccafs.marlo.action.center.json.autosave.AutoSaveWriterAction.java
License:Open Source License
@Override public String execute() throws Exception { String fileId = ""; String fileClass = ""; String nameClass = ""; String fileAction = ""; status = new HashMap<String, Object>(); if (autoSave.length > 0) { Gson gson = new Gson(); byte ptext[] = autoSave[0].getBytes(ISO_8859_1); String value = new String(ptext, UTF_8); @SuppressWarnings("unchecked") LinkedTreeMap<String, Object> result = gson.fromJson(value, LinkedTreeMap.class); String userModifiedBy = fileId = (String) result.get("modifiedBy.id"); if (result.containsKey("id")) { fileId = (String) result.get("id"); } else {// w w w. j av a 2s. c o m fileId = "XX"; } if (result.containsKey("className")) { String ClassName = (String) result.get("className"); String[] composedClassName = ClassName.split("\\."); nameClass = ClassName; fileClass = composedClassName[composedClassName.length - 1]; } if (result.containsKey("actionName")) { fileAction = (String) result.get("actionName"); fileAction = fileAction.replace("/", "_"); } ArrayList<String> keysRemove = new ArrayList<>(); for (Map.Entry<String, Object> entry : result.entrySet()) { if (entry.getKey().contains("__")) { keysRemove.add(entry.getKey()); } } for (String string : keysRemove) { result.remove(string); } Date generatedDate = new Date(); result.put("activeSince", generatedDate); String jSon = gson.toJson(result); if (nameClass.equals(CenterOutcome.class.getName())) { jSon = jSon.replaceAll("outcome\\.", ""); } if (nameClass.equals(CenterOutput.class.getName())) { jSon = jSon.replaceAll("output\\.", ""); } if (nameClass.equals(CenterProject.class.getName())) { jSon = jSon.replaceAll("project\\.", ""); } if (nameClass.equals(CenterDeliverable.class.getName())) { jSon = jSon.replaceAll("deliverable\\.", ""); } if (nameClass.equals(CapacityDevelopment.class.getName())) { jSon = jSon.replaceAll("capdev\\.", ""); } try { String fileName = fileId + "_" + fileClass + "_" + fileAction + ".json"; String pathFile = config.getAutoSaveFolder(); Path path = Paths.get(pathFile); if (Files.exists(path, LinkOption.NOFOLLOW_LINKS)) { File file = new File(config.getAutoSaveFolder() + fileName); Writer out = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(file), "UTF8")); out.append(jSon).append("\r\n"); ; out.flush(); out.close(); } else { Files.createDirectories(path); File file = new File(config.getAutoSaveFolder() + fileName); Writer out = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(file), "UTF8")); out.append(jSon).append("\r\n"); ; out.flush(); out.close(); } status.put("status", true); SimpleDateFormat dt = new SimpleDateFormat("yyyyy-mm-dd hh:mm:ss"); status.put("modifiedBy", userService.getUser(Long.parseLong(userModifiedBy)).getComposedCompleteName()); status.put("activeSince", dt.format(generatedDate)); } catch (IOException e) { status.put("status", false); e.printStackTrace(); } catch (Exception e) { status.put("status", false); e.printStackTrace(); } } return Action.SUCCESS; }
From source file:org.cgiar.ccafs.marlo.action.json.autosave.AutoSaveWriterAction.java
License:Open Source License
@Override public String execute() throws Exception { String fileId = ""; String fileClass = ""; String nameClass = ""; String fileAction = ""; status = new HashMap<String, Object>(); if (autoSave.length > 0) { Gson gson = new Gson(); byte ptext[] = autoSave[0].getBytes(ISO_8859_1); String value = new String(ptext, UTF_8); @SuppressWarnings("unchecked") LinkedTreeMap<String, Object> result = gson.fromJson(value, LinkedTreeMap.class); String userModifiedBy = fileId = (String) result.get("modifiedBy.id"); if (result.containsKey("id")) { fileId = (String) result.get("id"); } else {/* ww w . j a v a 2s.c o m*/ fileId = "XX"; } if (result.containsKey("className")) { String ClassName = (String) result.get("className"); String[] composedClassName = ClassName.split("\\."); nameClass = ClassName; fileClass = composedClassName[composedClassName.length - 1]; } if (result.containsKey("actionName")) { fileAction = (String) result.get("actionName"); fileAction = fileAction.replace("/", "_"); } ArrayList<String> keysRemove = new ArrayList<>(); for (Map.Entry<String, Object> entry : result.entrySet()) { if (entry.getKey().contains("__")) { keysRemove.add(entry.getKey()); } } for (String string : keysRemove) { result.remove(string); } Date generatedDate = new Date(); result.put("activeSince", generatedDate); String jSon = gson.toJson(result); if (nameClass.equals(ResearchOutcome.class.getName())) { jSon = jSon.replaceAll("outcome\\.", ""); } if (nameClass.equals(ResearchOutput.class.getName())) { jSon = jSon.replaceAll("output\\.", ""); } try { String fileName = fileId + "_" + fileClass + "_" + fileAction + ".json"; String pathFile = config.getAutoSaveFolder(); System.out.println(pathFile); Path path = Paths.get(pathFile); if (Files.exists(path, LinkOption.NOFOLLOW_LINKS)) { File file = new File(config.getAutoSaveFolder() + fileName); Writer out = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(file), "UTF8")); out.append(jSon).append("\r\n"); ; out.flush(); out.close(); } else { Files.createDirectories(path); File file = new File(config.getAutoSaveFolder() + fileName); Writer out = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(file), "UTF8")); out.append(jSon).append("\r\n"); ; out.flush(); out.close(); } status.put("status", true); SimpleDateFormat dt = new SimpleDateFormat("yyyyy-mm-dd hh:mm:ss"); status.put("modifiedBy", userService.getUser(Long.parseLong(userModifiedBy)).getComposedCompleteName()); status.put("activeSince", dt.format(generatedDate)); } catch (IOException e) { status.put("status", false); e.printStackTrace(); } catch (Exception e) { status.put("status", false); e.printStackTrace(); } } return Action.SUCCESS; }
From source file:org.cgiar.ccafs.marlo.utils.AutoSaveReader.java
License:Open Source License
private HashMap<String, Object> convertJSONFormat(String json) { HashMap<String, Object> jsonNew = new HashMap<>(); Gson gson = new Gson(); LinkedTreeMap<String, Object> result = gson.fromJson(json, LinkedTreeMap.class); HashMap<String, Object> onetoMany = new HashMap<>(); JsonObject jobj = new Gson().fromJson(json, JsonObject.class); for (Map.Entry<String, Object> entry : result.entrySet()) { String value = entry.getValue().toString(); String key = entry.getKey(); if (!key.contains("[")) { String oneToManys[] = key.split("\\."); if (oneToManys.length > 1) { onetoMany.put(key, entry.getValue()); jobj.remove(key);/*from w w w .j av a2s. c o m*/ } else { if (!key.contains("__")) { jsonNew.put(key, value); jobj.remove(key); } } } } if (!onetoMany.isEmpty()) { jsonNew.putAll(this.getOneToMany(gson.toJson(onetoMany))); } result = gson.fromJson(jobj, LinkedTreeMap.class); Set<String> listNames = new HashSet<>(); for (Map.Entry<String, Object> entry : result.entrySet()) { String key = entry.getKey(); String keys[] = key.split("\\."); String keyList = keys[0]; keyList = keyList.split("\\[")[0]; listNames.add(keyList); } for (String keyList : listNames) { HashMap<String, Object> list = this.getListJsonParent(keyList, jobj); if (list.size() > 0) { jsonNew.putAll(list); } } return jsonNew; }
From source file:org.cgiar.ccafs.marlo.utils.AutoSaveReader.java
License:Open Source License
private HashMap<String, Object> getListJson(String keyParent, JsonObject jobj, int i) { HashMap<String, Object> jsonNew = new HashMap<>(); Gson gson = new Gson(); HashMap<String, Object> relations = new HashMap<>(); HashMap<String, Object> onetoMany = new HashMap<>(); LinkedTreeMap<String, Object> result = gson.fromJson(jobj, LinkedTreeMap.class); for (Map.Entry<String, Object> entry : result.entrySet()) { String key = entry.getKey().split("\\.")[0]; if (key.equals(keyParent + "[" + i + "]")) { String newKey = entry.getKey().replace(keyParent + "[" + i + "].", ""); if (!newKey.contains("[")) { String oneToManys[] = newKey.split("\\."); if (oneToManys.length > 1) { onetoMany.put(newKey, entry.getValue()); } else { jsonNew.put(entry.getKey().replace(keyParent + "[" + i + "].", ""), entry.getValue()); jobj.remove(key);/*from w w w . j ava 2 s . c om*/ } } else { relations.put(newKey, entry.getValue()); jobj.remove(key); } } } if (!onetoMany.isEmpty()) { jsonNew.putAll(this.getOneToMany(gson.toJson(onetoMany))); } if (!relations.isEmpty()) { jsonNew.putAll(this.convertJSONFormat(gson.toJson(relations))); } return jsonNew; }
From source file:org.cgiar.ccafs.marlo.utils.AutoSaveReader.java
License:Open Source License
private HashMap<String, Object> getListJsonParent(String keyParent, JsonObject jobj) { HashMap<String, Object> jsonNew = new HashMap<>(); Gson gson = new Gson(); int index = 0; LinkedTreeMap<String, Object> result = gson.fromJson(jobj, LinkedTreeMap.class); for (Map.Entry<String, Object> entry : result.entrySet()) { String key = entry.getKey().split("\\.")[0]; String indexStr = key.split("\\[")[1].substring(0); String keyName = key.split("\\[")[0].substring(0); if (keyName.equals(keyParent)) { indexStr = indexStr.substring(0, indexStr.length() - 1); int tempIndex = Integer.parseInt(indexStr); if (tempIndex > index) { index = tempIndex;//w ww .j a va 2 s. c o m } } } List<HashMap<String, Object>> list = new ArrayList<HashMap<String, Object>>(); for (int i = 0; i <= index; i++) { list.add(this.getListJson(keyParent, jobj, i)); } jsonNew.put(keyParent, list); return jsonNew; }