JSON { public static Object parse(String json) throws ParseException { StringReader reader = new StringReader(json); try { return parse(reader); } catch (IOException e) { throw new ParseException(e.getMessage()); } finally { reader.close(); } } private  JSON(); static String json(Object obj); static void json(Object obj, Writer writer); static void json(Object obj, Writer writer, boolean writeClass); static String json(Object obj, String[] properties); static void json(Object obj, final String[] properties, Writer writer); static void json(Object obj, final String[] properties, Writer writer, boolean writeClass); static Object parse(String json); static Object parse(Reader reader); static T parse(String json, Class<T> type); @SuppressWarnings("unchecked") static T parse(Reader reader, Class<T> type); static Object[] parse(String json, Class<?>[] types); static Object[] parse(Reader reader, Class<?>[] types); static Object parse(String json, JSONVisitor handler); static Object parse(Reader reader, JSONVisitor handler); static final char LBRACE; static final char LSQUARE; static final char COMMA; static final String NULL; static final byte END; }
@Test public void testParse2JSONObject() throws Exception { JSONObject jo = (JSONObject) JSON.parse("{name:'qianlei',array:[1,2,3,4,98.123],b1:TRUE,$1:NULL,$2:FALSE,__3:NULL}"); assertEquals(jo.getString("name"), "qianlei"); assertEquals(jo.getArray("array").length(), 5); assertEquals(jo.get("$2"), Boolean.FALSE); assertEquals(jo.get("__3"), null); for (int i = 0; i < 10000; i++) JSON.parse("{\"name\":\"qianlei\",\"array\":[1,2,3,4,98.123],\"displayName\":\"钱磊\"}"); long now = System.currentTimeMillis(); for (int i = 0; i < 10000; i++) JSON.parse("{\"name\":\"qianlei\",\"array\":[1,2,3,4,98.123],\"displayName\":\"钱磊\"}"); System.out.println("parse to JSONObject 10000 times in: " + (System.currentTimeMillis() - now)); }
@Test @SuppressWarnings("unchecked") public void testParse2Class() throws Exception { int[] o1 = {1, 2, 3, 4, 5}, o2 = JSON.parse("[1.2,2,3,4,5]", int[].class); assertEquals(o2.length, 5); for (int i = 0; i < 5; i++) assertEquals(o1[i], o2[i]); List l1 = (List) JSON.parse("[1.2,2,3,4,5]", List.class); assertEquals(l1.size(), 5); for (int i = 0; i < 5; i++) assertEquals(o1[i], ((Number) l1.get(i)).intValue()); Bean bean = JSON.parse("{name:'qianlei',array:[1,2,3,4,98.123],displayName:'钱磊',$$:214726,$b:TRUE}", Bean.class); assertEquals(bean.getName(), "qianlei"); assertEquals(bean.getDisplayName(), "钱磊"); assertEquals(bean.array.length, 5); assertEquals(bean.$$, 214726); assertEquals(bean.$b, true); for (int i = 0; i < 10000; i++) JSON.parse("{name:'qianlei',array:[1,2,3,4,98.123],displayName:'钱磊'}", Bean1.class); long now = System.currentTimeMillis(); for (int i = 0; i < 10000; i++) JSON.parse("{name:'qianlei',array:[1,2,3,4,98.123],displayName:'钱磊'}", Bean1.class); System.out.println("parse to Class 10000 times in: " + (System.currentTimeMillis() - now)); }
@Test public void testParse2JSONObject() throws Exception { JSONObject jo = (JSONObject) JSON.parse("{name:'qianlei',array:[1,2,3,4,98.123],b1:TRUE,$1:NULL,$2:FALSE,__3:NULL}"); assertEquals(jo.getString("name"), "qianlei"); assertEquals(jo.getArray("array").length(), 5); assertEquals(jo.get("$2"), Boolean.FALSE); assertNull(jo.get("__3")); for (int i = 0; i < 10000; i++) JSON.parse("{\"name\":\"qianlei\",\"array\":[1,2,3,4,98.123],\"displayName\":\"钱磊\"}"); long now = System.currentTimeMillis(); for (int i = 0; i < 10000; i++) JSON.parse("{\"name\":\"qianlei\",\"array\":[1,2,3,4,98.123],\"displayName\":\"钱磊\"}"); System.out.println("parse to JSONObject 10000 times in: " + (System.currentTimeMillis() - now)); }
@Test @SuppressWarnings("unchecked") public void testParse2Class() throws Exception { int[] o1 = {1, 2, 3, 4, 5}, o2 = JSON.parse("[1.2,2,3,4,5]", int[].class); assertEquals(o2.length, 5); for (int i = 0; i < 5; i++) assertEquals(o1[i], o2[i]); List l1 = (List) JSON.parse("[1.2,2,3,4,5]", List.class); assertEquals(l1.size(), 5); for (int i = 0; i < 5; i++) assertEquals(o1[i], ((Number) l1.get(i)).intValue()); Bean bean = JSON.parse("{name:'qianlei',array:[1,2,3,4,98.123],displayName:'钱磊',$$:214726,$b:TRUE}", Bean.class); assertEquals(bean.getName(), "qianlei"); assertEquals(bean.getDisplayName(), "钱磊"); assertEquals(bean.array.length, 5); assertEquals(bean.$$, 214726); assertTrue(bean.$b); for (int i = 0; i < 10000; i++) JSON.parse("{name:'qianlei',array:[1,2,3,4,98.123],displayName:'钱磊'}", Bean1.class); long now = System.currentTimeMillis(); for (int i = 0; i < 10000; i++) JSON.parse("{name:'qianlei',array:[1,2,3,4,98.123],displayName:'钱磊'}", Bean1.class); System.out.println("parse to Class 10000 times in: " + (System.currentTimeMillis() - now)); }
@Test public void testParse2Arguments() throws Exception { Object[] test = JSON.parse("[1.2, 2, {name:'qianlei',array:[1,2,3,4,98.123]} ]", new Class<?>[]{int.class, int.class, Bean.class}); assertEquals(test[1], 2); assertEquals(test[2].getClass(), Bean.class); test = JSON.parse("[1.2, 2]", new Class<?>[]{int.class, int.class}); assertEquals(test[0], 1); }