JSONSerializer { public static Object deserialize(String json) throws ParseException { return new JSONSerializer(json).nextValue(); } protected  JSONSerializer(String string); static String serialize(Object object); static String serialize(Object object, boolean addType); static Object deserialize(String json);  }
@Test public void testDeserialize() { Assert.assertEquals(JSONSerializer.deserialize("\"c\""), "c"); Assert.assertEquals(JSONSerializer.deserialize("'c'"), "c"); List list = (List) JSONSerializer.deserialize("[]"); Assert.assertEquals(list.size(), 0); Map map = (Map) JSONSerializer.deserialize("{}"); Assert.assertEquals(map.size(), 0); String s = "{" + "\"a\": null," + " \"b\":1," + " \"c\":9999999999," + " \"d\":1.0," + " \"e\":false," + " \"f\":\"c\"," + " \"g\":[]," + " \"h\":[1,2]," + " \"i\":[\"11\",\"22\"]," + " \"j\":{}," + " \"k\":{" + " \"11\":\"22\"" + " }" + "}"; Map json = (Map) JSONSerializer.deserialize(s); Assert.assertNotNull(json); Assert.assertEquals(json.get("a"), null); Assert.assertEquals(json.get("b"), 1); Assert.assertEquals(json.get("c"), 9999999999l); Assert.assertEquals(json.get("d"), 1.0); Assert.assertEquals(json.get("e"), false); Assert.assertEquals(json.get("f"), "c"); Assert.assertEquals(((List) json.get("g")).size(), 0); Assert.assertEquals(((List) json.get("h")).get(0), 1); Assert.assertEquals(((List) json.get("i")).get(1), "22"); Assert.assertEquals(((Map) json.get("j")).size(), 0); Assert.assertEquals(((Map) json.get("k")).get("11"), "22"); }
@Test public void testDeserializeSpecialCharter() { String s = "{\"a\": \"\\b\\t\\n\\f\\r\\u771f\\u7684\\u5417\\uff1f\\u54c8\\u54c8\\u0068\\u0061\\u0068\\u0061\" }"; Map json = (Map) JSONSerializer.deserialize(s); Assert.assertEquals(json.get("a"), "\b\t\n\f\r真的吗？哈哈haha"); }
@Test public void testDeserializeWithComment() { String s = "{" + "\"a\": null, " \"b\":1, \n" + " \"c\":1, \n" + " \"d\":9999999999" + "}"; LOGGER.info(s); Map json = (Map) JSONSerializer.deserialize(s); Assert.assertNotNull(json); Assert.assertEquals(json.get("a"), null); Assert.assertEquals(json.get("b"), 1); Assert.assertEquals(json.get("c"), 1); Assert.assertEquals(json.get("d"), 9999999999l); }