private boolean compareObjects(JSONObject expectedMap, JSONObject actualMap) { // each key-value of expected map must exist in actual map for (Map.Entry<Object, Object> expectedEntry : (Set<Map.Entry<Object, Object>>) expectedMap.entrySet()) { Object key = expectedEntry.getKey(); if (shouldIgnoreField(key)) { continue; } if (!actualMap.containsKey(key)) { return false; } if (!compare(expectedEntry.getValue(), actualMap.get(key))) { return false; } } return true; }
/** * Reloads from the file. */ public void load() { entries.clear(); if (file.exists()) { try (Reader reader = new FileReader(file)) { JSONArray jsonArray = (JSONArray) new JSONParser().parse(reader); for (Object object : jsonArray) { JSONObject jsonObj = (JSONObject) object; Map<String, String> map = new HashMap<>(jsonObj.size()); for (Object jsonEntry : jsonObj.entrySet()) { Entry<?, ?> entry = (Map.Entry<?, ?>) jsonEntry; map.put(entry.getKey().toString(), entry.getValue().toString()); } entries.add(readEntry(map)); } } catch (Exception ex) { GlowServer.logger.log(Level.SEVERE, "Error reading from: " + file, ex); } } else { //importLegacy(); save(); } }
JSONParser parser = new JSONParser(); JSONObject json = (JSONObject) parser.parse(new FileReader(statsFile)); for (Object obj : json.entrySet()) { Map.Entry<String, Object> entry = (Map.Entry<String, Object>) obj; Long longValue = null;
private Map<String, String> prepareProperties(JSONObject jo) { Map<String, String> properties = Maps.newHashMap(); if(jo == null) { return properties; } for(Object o: jo.entrySet()) { @SuppressWarnings("rawtypes") Map.Entry e = (Map.Entry) o; Object key = e.getKey(); Object value = e.getValue(); properties.put(valueOf(key), valueOf(value)); } return properties; }
private Map<Integer, Double> prepareBoosts(JSONObject jo) { Map<Integer, Double> dynamicBoosts = Maps.newHashMap(); if(jo == null) { return dynamicBoosts; } for(Object o: jo.entrySet()) { @SuppressWarnings("rawtypes") Map.Entry e = (Map.Entry) o; Object key = e.getKey(); Object value = e.getValue(); dynamicBoosts.put(parseInt(valueOf(key)), parseDouble(valueOf(value))); } return dynamicBoosts; }
private Object truncateJSONObject(JSONObject pValue, int pMaxDepth) { if (pMaxDepth == 0) { return 1; } JSONObject ret = new JSONObject(); Set<Map.Entry> entries = pValue.entrySet(); for (Map.Entry entry : entries) { Object value = entry.getValue(); Object key = entry.getKey(); if (value instanceof JSONObject) { ret.put(key, truncateJSONObject((JSONObject) value, pMaxDepth - 1)); } else { ret.put(key,value); } } return ret; }
public static Map<Payload,Object> parseData(byte[] pData, int pLength) throws IOException { JSONParser parser = new JSONParser(); ByteArrayInputStream is = new ByteArrayInputStream(pData,0,pLength); try { JSONObject inMsg = (JSONObject) parser.parse(new InputStreamReader(is, "UTF-8")); Map<Payload, Object> data = new HashMap<Payload, Object>(); for (Map.Entry entry : (Set<Map.Entry>) inMsg.entrySet()) { try { data.put(Payload.fromKey(entry.getKey().toString()), entry.getValue()); } catch (IllegalArgumentException exp) { // We simply ignore key which are unknown } } return data; } catch (ParseException e) { throw new IOException("Cannot parse discovery message as JSON",e); } }
public static void deserializeParameters( JSONObject parameters, PayloadTypePacketExtension payloadTypeIQ) { if (parameters != null) { for (Map.Entry<Object, Object> e : (Iterable<Map.Entry<Object, Object>>) parameters .entrySet()) { Object name = e.getKey(); Object value = e.getValue(); if ((name != null) || (value != null)) { payloadTypeIQ.addParameter( new ParameterPacketExtension( Objects.toString(name, null), Objects.toString(value, null))); } } } }
private EnrichmentValue getValue( JSONObject message , Set<String> keyColumns , Set<String> valueColumns ) { Map<String, Object> metadata = new HashMap<>(); if(valueColumns == null || valueColumns.isEmpty()) { for (Object kv : message.entrySet()) { Map.Entry<Object, Object> entry = (Map.Entry<Object, Object>) kv; if (!keyColumns.contains(entry.getKey())) { addMetadataEntry(metadata, entry); } } return new EnrichmentValue(metadata); } else { for (Object kv : message.entrySet()) { Map.Entry<Object, Object> entry = (Map.Entry<Object, Object>) kv; if (valueColumns.contains(entry.getKey())) { addMetadataEntry(metadata, entry); } } return new EnrichmentValue(metadata); } }
@Test public void testOutput() throws ParseException { List<MetricName> inputMetricNames = new ArrayList<MetricName>() {{ add(new MetricName("foo", false)); add(new MetricName("bar", false)); }}; String output = handler.getSerializedJSON(inputMetricNames); JSONParser jsonParser = new JSONParser(); JSONArray tokenInfos = (JSONArray) jsonParser.parse(output); Assert.assertEquals("Unexpected result size", 2, tokenInfos.size()); Set<String> expectedOutputSet = new HashSet<String>(); for (MetricName metricName : inputMetricNames) { expectedOutputSet.add(metricName.getName() + "|" + metricName.isCompleteName()); } Set<String> outputSet = new HashSet<String>(); for (int i = 0; i< inputMetricNames.size(); i++) { JSONObject object = (JSONObject) tokenInfos.get(i); Iterator it = object.entrySet().iterator(); while (it.hasNext()) { Map.Entry entry = (Map.Entry) it.next(); outputSet.add(entry.getKey() + "|" + entry.getValue()); } } Assert.assertEquals("Unexpected size", expectedOutputSet.size(), outputSet.size()); Assert.assertTrue("Output contains no more elements than expected", expectedOutputSet.containsAll(outputSet)); Assert.assertTrue("Output contains no less elements than expected", outputSet.containsAll(expectedOutputSet)); }
private static Map<String, String> getMap(JSONObject jsonMap) { Map<String,String> map = new HashMap<>(); for(Map.Entry<String,String> entry : (Set<Map.Entry<String,String>>)jsonMap.entrySet()) { map.put(entry.getKey(),entry.getValue()); } return map; }
JSONObject json = getJsonFromFile(filePath); HashMap<String, Number> entries = new HashMap<>(); for (Object obj : json.entrySet()) { Entry<String, Number> mapEntry = (Entry<String, Number>) obj; System.out.println("key: " + mapEntry.getKey() + " value: " + mapEntry.getValue()); entries.put(mapEntry.getKey(), mapEntry.getValue()); }
JSONObject parse = JSON.parseObject(""); for (Map.Entry<String, Object> entry : parse.entrySet()) { System.out.println(entry.getKey() + "=" + entry.getValue()); }
String serializedMap = prop.getProperty("wordMap"); JSONObject json = (JSONObject) JSONValue.parse(serializedMap); Map<String, String> wordMap = new HashMap<>(); for (Map.Entry<String,Object> entry : json.entrySet()) { wordMap.put(entry.getKey(), (String) entry.getValue()); }
JSONObject obj = JSONObject.fromObject(strRepresentation); Iterator i = obj.entrySet().iterator(); while (i.hasNext()) { Map.Entry e = (Map.Entry)i.next(); System.out.println("Key: " + e.getKey()); System.out.println("Value: " + e.getValue()); }
public void toString( StringBuilder sb ){ sb.append( "{" ); Iterator iter=entrySet().iterator(); boolean first = true; while(iter.hasNext()){ if ( first ){ first = false; }else{ sb.append( "," ); } Map.Entry entry=(Map.Entry)iter.next(); toString(sb, entry.getKey().toString(),entry.getValue()); } sb.append( "}" ); }
public String toString(){ ItemList list=new ItemList(); Iterator<Map.Entry<String, Object>> iter=entrySet().iterator(); while(iter.hasNext()){ Map.Entry<String, Object> entry=iter.next(); list.add(toString(entry.getKey().toString(),entry.getValue())); } return "{"+list.toString()+"}"; }
public Void call() throws Exception { URL url = createURL(RestConstants.ADMIN_UPDATE_SHARELIB, Collections.EMPTY_MAP); HttpURLConnection conn = (HttpURLConnection) url.openConnection(); conn.setRequestMethod("GET"); assertEquals(HttpServletResponse.SC_OK, conn.getResponseCode()); assertTrue(conn.getHeaderField("content-type").startsWith(RestConstants.JSON_CONTENT_TYPE)); JSONObject json = (JSONObject) JSONValue.parse(new InputStreamReader(conn.getInputStream())); assertEquals(1, json.entrySet().size()); return null; } });