public void testReadMapAsSingleObject() { String s = "{'description':{'type':'text','hint':'enter something here'}}"; s = swapQuotes(s); json(s); Map map = (Map) json.next(); assertTrue(map.containsKey("description")); }
public void testStreamConstructor() throws UnsupportedEncodingException { String orig = "[0,1,2,3,\"hello\"]"; InputStream stream = new ByteArrayInputStream(orig.getBytes("UTF-8")); json = new JSONParser(stream); Object a = json.next(); assertTrue(a instanceof ArrayList); enc().encode(a); String s = enc.toString(); assertStringsMatch(s, orig); }
public void testMap() { String orig = "{\"u\":14,\"m\":false,\"w\":null,\"k\":true}"; json(orig); json.enterMap(); Map m = new HashMap(); for (int i = 0; i < 4; i++) { String key = json.nextKey(); Object value = json.next(); assertFalse(m.containsKey(key)); m.put(key, value); } json.exit(); assertStringsMatch(m.get("u"), "14.0"); assertStringsMatch(m.get("m"), "false"); assertTrue(m.get("w") == null); assertStringsMatch(m.get("k"), "true"); }
public void testSymmetry() { String script[] = { // "0", // "1", // "-1.2352E20", // "-1.2352E-20", // "0.5", // "{'hey':42}", // "[1,2,3,4]", // "{'hey':42,'you':43}", // "{'hey':{'you':17},'array':[1,2,3,4]}", // "{'trailing number':5}", // "{ 'favorite song': { '_skip_order': 3, 'type': 'text','hint': 'name of song','zminlines': 5 },'wow':12 } ", // }; for (int i = 0; i < script.length; i++) { String s = swapQuotes(script[i]); if (db) pr("\n testing '" + s + "'"); json(s); Object obj = json.next(); assertFalse(json.hasNext()); if (db) pr(" parsed " + obj + " (type = " + obj.getClass() + ")"); newEnc().encode(obj); String s2 = enc.toString(); if (db) pr(" encoded is " + s2); Object obj2 = json(s2).next(); if (db) pr("parsed object: " + obj2); newEnc().encode(obj2); String s3 = enc.toString(); assertStringsMatch(s2, s3); } }