@org.testng.annotations.Test public void testJSONEncoding() throws ParseException { String json = "{ 'str' : 'asdfasd' , 'long' : 123123123123 , 'int' : 5 , 'float' : 0.4 , 'bool' : false , 'date' : { '$date' : '2011-05-18T18:56:00Z'} , 'pat' : { '$regex' : '.*' , '$options' : ''} , 'oid' : { '$oid' : '4d83ab3ea39562db9c1ae2ae'} , 'ref' : { '$ref' : 'test.test' , '$id' : { '$oid' : '4d83ab59a39562db9c1ae2af'}} , 'code' : { '$code' : 'asdfdsa'} , 'codews' : { '$code' : 'ggggg' , '$scope' : { }} , 'ts' : { '$ts' : 1300474885 , '$inc' : 10} , 'null' : null, 'uuid' : { '$uuid' : '60f65152-6d4a-4f11-9c9b-590b575da7b5' }}"; BasicDBObject a = (BasicDBObject) JSON.parse(json); assert (a.get("str").equals("asdfasd")); assert (a.get("int").equals(5)); assert (a.get("long").equals(123123123123L)); assert (a.get("float").equals(0.4d)); assert (a.get("bool").equals(false)); SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss'Z'"); format.setCalendar(new GregorianCalendar(new SimpleTimeZone(0, "GMT"))); assert (a.get("date").equals(format.parse("2011-05-18T18:56:00Z"))); Pattern pat = (Pattern) a.get("pat"); Pattern pat2 = Pattern.compile(".*", BSON.regexFlags("")); assert (pat.pattern().equals(pat2.pattern())); assert (pat.flags() == (pat2.flags())); ObjectId oid = (ObjectId) a.get("oid"); assert (oid.equals(new ObjectId("4d83ab3ea39562db9c1ae2ae"))); DBRef ref = (DBRef) a.get("ref"); assert (ref.equals(new DBRef(null, "test.test", new ObjectId("4d83ab59a39562db9c1ae2af")))); assert (a.get("code").equals(new Code("asdfdsa"))); assert (a.get("codews").equals(new CodeWScope("ggggg", new BasicBSONObject()))); assert (a.get("ts").equals(new BSONTimestamp(1300474885, 10))); assert (a.get("uuid").equals(UUID.fromString("60f65152-6d4a-4f11-9c9b-590b575da7b5"))); String json2 = JSON.serialize(a); BasicDBObject b = (BasicDBObject) JSON.parse(json2); a.equals(b); assert (a.equals(b)); }
/* ------------------------------------------------------------ */ public void testBigDecimal() { Object obj = JSON.parse("1.0E7"); assertTrue(obj instanceof Double); BigDecimal bd = new BigDecimal(1000.1D); String string = JSON.toString(new Object[] {bd}); obj = Array.get(JSON.parse(string), 0); assertTrue(obj instanceof Double); }
@org.testng.annotations.Test(groups = {"basic"}) public void testString() { assertEquals(JSON.serialize(JSON.parse("{'csdf' : \"foo\"}")), "{ \"csdf\" : \"foo\"}"); assertEquals(JSON.serialize(JSON.parse("{'csdf' : \'foo\'}")), "{ \"csdf\" : \"foo\"}"); assertEquals(JSON.serialize(JSON.parse("{'csdf' : \"a\\\"b\"}")), "{ \"csdf\" : \"a\\\"b\"}"); assertEquals( JSON.serialize(JSON.parse("{\n\t\"id\":\"1689c12eb234c54a84ebd100\",\n}")), "{ \"id\" : \"1689c12eb234c54a84ebd100\"}"); }
@org.testng.annotations.Test(groups = {"basic"}) public void testUnicode() { assertEquals(JSON.serialize(JSON.parse("{'x' : \"hi\\u0020\"}")), "{ \"x\" : \"hi \"}"); assertEquals( JSON.serialize(JSON.parse("{ \"x\" : \"\\u0E01\\u2702\\uF900\"}")), "{ \"x\" : \"\u0E01\u2702\uF900\"}"); assertEquals( JSON.serialize(JSON.parse("{ \"x\" : \"foo\\u0020bar\"}")), "{ \"x\" : \"foo bar\"}"); }
@org.testng.annotations.Test(groups = {"basic"}) public void testObject() { assertEquals(JSON.serialize(JSON.parse("{'csdf' : {}}")), "{ \"csdf\" : { }}"); assertEquals( JSON.serialize(JSON.parse("{'csdf' : {\"foo\":\"bar\"}}")), "{ \"csdf\" : { \"foo\" : \"bar\"}}"); assertEquals( JSON.serialize(JSON.parse("{'csdf' : {\'hi\':{\'hi\':[{}]}}}")), "{ \"csdf\" : { \"hi\" : { \"hi\" : [ { }]}}}"); }
void _escapeChar(String s) { String thingy = "va" + s + "lue"; DBObject x = new BasicDBObject("name", thingy); x = (DBObject) JSON.parse(x.toString()); assertEquals(thingy, x.get("name")); thingy = "va" + s + s + s + "lue" + s; x = new BasicDBObject("name", thingy); x = (DBObject) JSON.parse(x.toString()); assertEquals(thingy, x.get("name")); }
@org.testng.annotations.Test public void testNumbers2() { DBObject x = new BasicDBObject("x", 123); assertEquals(x, JSON.parse(x.toString())); x = new BasicDBObject("x", 123123123123L); assertEquals(x, JSON.parse(x.toString())); x = new BasicDBObject("x", 123123123); assertEquals(x, JSON.parse(x.toString())); }
/* ------------------------------------------------------------ */ public void testParseReader() throws Exception { Map map = (Map) JSON.parse(new StringReader(test)); assertEquals(new Long(100), map.get("onehundred")); assertEquals("fred", map.get("name")); assertTrue(map.get("array").getClass().isArray()); assertTrue(map.get("w0") instanceof Woggle); assertTrue(((Woggle) map.get("w0")).nested instanceof Woggle); test = "{\"data\":{\"source\":\"15831407eqdaawf7\",\"widgetId\":\"Magnet_8\"},\"channel\":\"/magnets/moveStart\",\"connectionId\":null,\"clientId\":\"15831407eqdaawf7\"}"; map = (Map) JSON.parse(test); }
@org.testng.annotations.Test(groups = {"basic"}) public void testArray() { assertEquals(JSON.serialize(JSON.parse("{'csdf' : [\"foo\"]}")), "{ \"csdf\" : [ \"foo\"]}"); assertEquals( JSON.serialize(JSON.parse("{'csdf' : [3, 5, \'foo\', null]}")), "{ \"csdf\" : [ 3 , 5 , \"foo\" , null ]}"); assertEquals( JSON.serialize(JSON.parse("{'csdf' : [3.0, 5.0, \'foo\', null]}")), "{ \"csdf\" : [ 3.0 , 5.0 , \"foo\" , null ]}"); assertEquals( JSON.serialize(JSON.parse("{'csdf' : [[],[[]],false]}")), "{ \"csdf\" : [ [ ] , [ [ ]] , false]}"); }
@org.testng.annotations.Test(groups = {"basic"}) public void testNumbers() { assertEquals(JSON.serialize(JSON.parse("{'x' : 5 }")), "{ \"x\" : 5}"); assertEquals(JSON.serialize(JSON.parse("{'x' : 5.0 }")), "{ \"x\" : 5.0}"); assertEquals(JSON.serialize(JSON.parse("{'x' : 0 }")), "{ \"x\" : 0}"); assertEquals(JSON.serialize(JSON.parse("{'x' : 0.0 }")), "{ \"x\" : 0.0}"); assertEquals(JSON.serialize(JSON.parse("{'x' : 500 }")), "{ \"x\" : 500}"); assertEquals(JSON.serialize(JSON.parse("{'x' : 500.0 }")), "{ \"x\" : 500.0}"); assertEquals(JSON.serialize(JSON.parse("{'x' : 0.500 }")), "{ \"x\" : 0.5}"); assertEquals(JSON.serialize(JSON.parse("{'x' : 5. }")), "{ \"x\" : 5.0}"); assertEquals(JSON.serialize(JSON.parse("{'x' : 5.0e+1 }")), "{ \"x\" : 50.0}"); assertEquals(JSON.serialize(JSON.parse("{'x' : 5.0E-1 }")), "{ \"x\" : 0.5}"); }
@org.testng.annotations.Test public void testEscape1() { String raw = "a\nb"; DBObject x = new BasicDBObject("x", raw); assertEquals("\"a\\nb\"", JSON.serialize(raw)); assertEquals(x, JSON.parse(x.toString())); assertEquals(raw, ((DBObject) JSON.parse(x.toString())).get("x")); x = new BasicDBObject("x", "a\nb\bc\td\re"); assertEquals(x, JSON.parse(x.toString())); String thingy = "va\"lue"; x = new BasicDBObject("name", thingy); x = (DBObject) JSON.parse(x.toString()); assertEquals(thingy, x.get("name")); thingy = "va\\lue"; x = new BasicDBObject("name", thingy); x = (DBObject) JSON.parse(x.toString()); assertEquals(thingy, x.get("name")); assertEquals("va/lue", (String) JSON.parse("\"va\\/lue\"")); assertEquals("value", (String) JSON.parse("\"va\\lue\"")); assertEquals("va\\lue", (String) JSON.parse("\"va\\\\lue\"")); _escapeChar("\t"); _escapeChar("\b"); _escapeChar("\n"); _escapeChar("\r"); _escapeChar("\'"); _escapeChar("\""); _escapeChar("\\"); }
/* ------------------------------------------------------------ */ @Test public void testQuote() { String test = "\"abc123|\\\"|\\\\|\\/|\\b|\\f|\\n|\\r|\\t|\\uaaaa|\""; String result = (String) JSON.parse(test, false); assertEquals("abc123|\"|\\|/|\b|\f|\n|\r|\t|\uaaaa|", result); }
@org.testng.annotations.Test(groups = {"basic"}) public void testLongValues() { Long bigVal = Integer.MAX_VALUE + 1L; String test = String.format("{ \"x\" : %d}", bigVal); assertEquals(JSON.serialize(JSON.parse(test)), test); Long smallVal = Integer.MIN_VALUE - 1L; String test2 = String.format("{ \"x\" : %d}", smallVal); assertEquals(JSON.serialize(JSON.parse(test2)), test2); try { JSON.parse("{\"ReallyBigNumber\": 10000000000000000000 }"); fail("JSONParseException should have been thrown"); } catch (JSONParseException e) { // fall through } }
/* ------------------------------------------------------------ */ public void testParse() { Map map = (Map) JSON.parse(test); assertEquals(new Long(100), map.get("onehundred")); assertEquals("fred", map.get("name")); assertEquals(new Double(-0.2), map.get("small")); assertTrue(map.get("array").getClass().isArray()); assertTrue(map.get("w0") instanceof Woggle); assertTrue(((Woggle) map.get("w0")).nested instanceof Woggle); assertEquals(-101, ((Woggle) ((Woggle) map.get("w0")).nested).number); assertTrue(map.containsKey("NaN")); assertEquals(null, map.get("NaN")); assertTrue(map.containsKey("undefined")); assertEquals(null, map.get("undefined")); test = "{\"data\":{\"source\":\"15831407eqdaawf7\",\"widgetId\":\"Magnet_8\"},\"channel\":\"/magnets/moveStart\",\"connectionId\":null,\"clientId\":\"15831407eqdaawf7\"}"; map = (Map) JSON.parse(test); }
/* ------------------------------------------------------------ */ public void testStripComment() { String test = "\n\n\n\t\t " + "// ignore this ,a [ \" \n" + "/* " + "{ " + "\"onehundred\" : 100 ," + "\"name\" : \"fred\" ," + "\"empty\" : {} ," + "\"map\" : {\"a\":-1.0e2} ," + "\"array\" : [\"a\",-1.0e2,[],null,true,false] ," + "} */"; Object o = JSON.parse(test, false); assertTrue(o == null); o = JSON.parse(test, true); assertTrue(o instanceof Map); assertEquals("fred", ((Map) o).get("name")); }
@org.testng.annotations.Test(groups = {"basic"}) public void testSerializationMethods() { // basic test of each of JSON class' serialization methods String json = "{ \"x\" : \"basic test\"}"; StringBuilder buf = new StringBuilder(); Object obj = JSON.parse(json); assertEquals(JSON.serialize(obj), json); }
@org.testng.annotations.Test public void testObjectId() { ObjectId oid = new ObjectId(new Date()); String serialized = JSON.serialize(oid); assertEquals("{ \"$oid\" : \"" + oid + "\"}", serialized); ObjectId oid2 = (ObjectId) JSON.parse(serialized); assertEquals(oid, oid2); }
@org.testng.annotations.Test(groups = {"basic"}) public void testErrors() { boolean threw = false; try { JSON.parse("{\"x\" : \""); } catch (JSONParseException e) { threw = true; } assertEquals(threw, true); threw = false; try { JSON.parse("{\"x\" : \"\\"); } catch (JSONParseException e) { threw = true; } assertEquals(threw, true); threw = false; try { JSON.parse("{\"x\" : 5.2"); } catch (JSONParseException e) { threw = true; } assertEquals(threw, true); threw = false; try { JSON.parse("{\"x\" : 5"); } catch (JSONParseException e) { threw = true; } assertEquals(threw, true); threw = false; try { JSON.parse("{\"x\" : 5,"); } catch (JSONParseException e) { threw = true; } assertEquals(threw, true); threw = false; }
@org.testng.annotations.Test public void testDate() { Date d = new Date(); SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSS'Z'"); format.setCalendar(new GregorianCalendar(new SimpleTimeZone(0, "GMT"))); String formattedDate = format.format(d); String serialized = JSON.serialize(d); assertEquals("{ \"$date\" : \"" + formattedDate + "\"}", serialized); Date d2 = (Date) JSON.parse(serialized); assertEquals(d.toString(), d2.toString()); assertTrue(d.equals(d2)); }
@org.testng.annotations.Test(groups = {"basic"}) public void testBasic() { assertEquals(JSON.serialize(JSON.parse("{}")), "{ }"); assertEquals(JSON.parse(""), null); assertEquals(JSON.parse(" "), null); assertEquals(JSON.parse(null), null); boolean threw = false; try { JSON.parse("{"); } catch (JSONParseException e) { threw = true; } assertEquals(threw, true); threw = false; try { JSON.parse("}"); } catch (JSONParseException e) { threw = true; } assertEquals(threw, true); threw = false; try { JSON.parse("{{}"); } catch (JSONParseException e) { threw = true; } assertEquals(threw, true); threw = false; try { JSON.parse("4"); } catch (JSONParseException e) { threw = true; } assertEquals(threw, false); threw = false; assertEquals(4, JSON.parse("4")); }
@org.testng.annotations.Test public void testRegexNoOptions() { String x = "^Hello$"; String serializedPattern = "{ \"$regex\" : \"" + x + "\"}"; Pattern pattern = Pattern.compile(x); assertEquals(serializedPattern, JSON.serialize(pattern)); BasicDBObject a = new BasicDBObject("x", pattern); assertEquals("{ \"x\" : " + serializedPattern + "}", a.toString()); DBObject b = (DBObject) JSON.parse(a.toString()); assertEquals(b.get("x").getClass(), Pattern.class); assertEquals(a.toString(), b.toString()); }
@Test public void testEnumConvertor() { JSON json = new JSON(); Locale l = new Locale("en", "US"); json.addConvertor( Date.class, new JSONDateConvertor(DateCache.DEFAULT_FORMAT, TimeZone.getTimeZone("GMT"), false, l)); json.addConvertor(Enum.class, new JSONEnumConvertor(false)); json.addConvertor(Object.class, new JSONObjectConvertor()); Woggle w0 = new Woggle(); Gizmo g0 = new Gizmo(); w0.name = "woggle0"; w0.nested = g0; w0.number = 100; w0.other = Color.Blue; g0.name = "woggle1"; g0.nested = null; g0.number = -101; g0.tested = true; g0.other = Color.Green; HashMap map = new HashMap(); map.put("date", new Date(1)); map.put("w0", w0); map.put("g0", g0); StringBuffer buf = new StringBuffer(); json.append((Appendable) buf, map); String js = buf.toString(); assertTrue(js.indexOf("\"date\":\"Thu Jan 01 00:00:00 GMT 1970\"") >= 0); assertTrue(js.indexOf("org.eclipse.jetty.util.ajax.JSONTest$Woggle") >= 0); assertTrue(js.indexOf("org.eclipse.jetty.util.ajax.JSONTest$Gizmo") < 0); assertTrue(js.indexOf("\"tested\":true") >= 0); assertTrue(js.indexOf("\"Green\"") >= 0); assertTrue(js.indexOf("\"Blue\"") < 0); json.addConvertor( Date.class, new JSONDateConvertor(DateCache.DEFAULT_FORMAT, TimeZone.getTimeZone("GMT"), true, l)); json.addConvertor(Enum.class, new JSONEnumConvertor(false)); w0.nested = null; buf = new StringBuffer(); json.append((Appendable) buf, map); js = buf.toString(); assertTrue(js.indexOf("\"date\":\"Thu Jan 01 00:00:00 GMT 1970\"") < 0); assertTrue(js.indexOf("org.eclipse.jetty.util.ajax.JSONTest$Woggle") >= 0); assertTrue(js.indexOf("org.eclipse.jetty.util.ajax.JSONTest$Gizmo") < 0); Map map2 = (HashMap) json.parse(new JSON.StringSource(js)); assertTrue(map2.get("date") instanceof Date); assertTrue(map2.get("w0") instanceof Woggle); assertEquals(null, ((Woggle) map2.get("w0")).getOther()); assertEquals(Color.Green.toString(), ((Map) map2.get("g0")).get("other")); json.addConvertor( Date.class, new JSONDateConvertor(DateCache.DEFAULT_FORMAT, TimeZone.getTimeZone("GMT"), true, l)); json.addConvertor(Enum.class, new JSONEnumConvertor(true)); buf = new StringBuffer(); json.append((Appendable) buf, map); js = buf.toString(); map2 = (HashMap) json.parse(new JSON.StringSource(js)); assertTrue(map2.get("date") instanceof Date); assertTrue(map2.get("w0") instanceof Woggle); assertEquals(null, ((Woggle) map2.get("w0")).getOther()); Object o = ((Map) map2.get("g0")).get("other"); assertEquals(Color.Green, o); }
/* ------------------------------------------------------------ */ @Test public void testConvertor() { // test case#1 - force timezone to GMT JSON json = new JSON(); json.addConvertor( Date.class, new JSONDateConvertor("MM/dd/yyyy HH:mm:ss zzz", TimeZone.getTimeZone("GMT"), false)); json.addConvertor(Object.class, new JSONObjectConvertor()); Woggle w0 = new Woggle(); Gizmo g0 = new Gizmo(); w0.name = "woggle0"; w0.nested = g0; w0.number = 100; g0.name = "woggle1"; g0.nested = null; g0.number = -101; g0.tested = true; HashMap map = new HashMap(); Date dummyDate = new Date(1); map.put("date", dummyDate); map.put("w0", w0); StringBuffer buf = new StringBuffer(); json.append(buf, map); String js = buf.toString(); assertTrue(js.indexOf("\"date\":\"01/01/1970 00:00:00 GMT\"") >= 0); assertTrue(js.indexOf("org.eclipse.jetty.util.ajax.JSONTest$Woggle") >= 0); assertTrue(js.indexOf("org.eclipse.jetty.util.ajax.JSONTest$Gizmo") < 0); assertTrue(js.indexOf("\"tested\":true") >= 0); // test case#3 TimeZone tzone = TimeZone.getTimeZone("JST"); String tzone3Letter = tzone.getDisplayName(false, TimeZone.SHORT); String format = "EEE MMMMM dd HH:mm:ss zzz yyyy"; Locale l = new Locale("ja", "JP"); if (l != null) { json.addConvertor(Date.class, new JSONDateConvertor(format, tzone, false, l)); buf = new StringBuffer(); json.append(buf, map); js = buf.toString(); // assertTrue(js.indexOf("\"date\":\"\u6728 1\u6708 01 09:00:00 JST 1970\"")>=0); assertTrue(js.indexOf(" 01 09:00:00 JST 1970\"") >= 0); assertTrue(js.indexOf("org.eclipse.jetty.util.ajax.JSONTest$Woggle") >= 0); assertTrue(js.indexOf("org.eclipse.jetty.util.ajax.JSONTest$Gizmo") < 0); assertTrue(js.indexOf("\"tested\":true") >= 0); } // test case#4 json.addConvertor(Date.class, new JSONDateConvertor(true)); w0.nested = null; buf = new StringBuffer(); json.append(buf, map); js = buf.toString(); assertTrue(js.indexOf("\"date\":\"Thu Jan 01 00:00:00 GMT 1970\"") < 0); assertTrue(js.indexOf("org.eclipse.jetty.util.ajax.JSONTest$Woggle") >= 0); assertTrue(js.indexOf("org.eclipse.jetty.util.ajax.JSONTest$Gizmo") < 0); map = (HashMap) json.parse(new JSON.StringSource(js)); assertTrue(map.get("date") instanceof Date); assertTrue(map.get("w0") instanceof Woggle); }
@org.testng.annotations.Test(groups = {"basic"}) public void testMulti() { assertEquals( JSON.serialize(JSON.parse("{\'\' : \"\", \"34\" : -52.5}")), "{ \"\" : \"\" , \"34\" : -52.5}"); }
@org.testng.annotations.Test(groups = {"basic"}) public void testLongValues() { Long val = Integer.MAX_VALUE + 1L; String test = String.format("{ \"x\" : %d}", val); assertEquals(JSON.serialize(JSON.parse(test)), test); }
@org.testng.annotations.Test(groups = {"basic"}) public void testSimple() { assertEquals(JSON.serialize(JSON.parse("{'csdf' : true}")), "{ \"csdf\" : true}"); assertEquals(JSON.serialize(JSON.parse("{'csdf' : false}")), "{ \"csdf\" : false}"); assertEquals(JSON.serialize(JSON.parse("{'csdf' : null}")), "{ \"csdf\" : null }"); }