01: package org.mortbay.cometd;
02:
03: import java.util.HashMap;
04: import java.util.Map;
05:
06: import junit.framework.TestCase;
07:
08: public class JSONTest extends TestCase {
09:
10: public void testToString() {
11: HashMap map = new HashMap();
12: HashMap obj6 = new HashMap();
13: HashMap obj7 = new HashMap();
14:
15: map.put("n1", null);
16: map.put("n2", new Integer(2));
17: map.put("n3", new Double(-0.00000000003));
18: map.put("n4", "4\n\r\t\"4");
19: map.put("n5", new Object[] { "a", new Character('b'),
20: new Integer(3), new String[] {}, null, Boolean.TRUE,
21: Boolean.FALSE });
22: map.put("n6", obj6);
23: map.put("n7", obj7);
24: map.put("n8", new int[] { 1, 2, 3, 4 });
25: map.put("n9", new JSON.Literal("[{}, [], {}]"));
26:
27: obj7.put("x", "value");
28:
29: String s = JSON.toString(map);
30: System.err.println(s);
31: assertTrue(s.indexOf("\"n1\":null") >= 0);
32: assertTrue(s.indexOf("\"n2\":2") >= 0);
33: assertTrue(s.indexOf("\"n3\":-3.0E-11") >= 0);
34: assertTrue(s.indexOf("\"n4\":\"4\\n") >= 0);
35: assertTrue(s.indexOf("\"n5\":[\"a\",\"b\",") >= 0);
36: assertTrue(s.indexOf("\"n6\":{}") >= 0);
37: assertTrue(s.indexOf("\"n7\":{\"x\":\"value\"}") >= 0);
38: assertTrue(s.indexOf("\"n8\":[1,2,3,4]") >= 0);
39: assertTrue(s.indexOf("\"n9\":[{}, [], {}]") >= 0);
40:
41: }
42:
43: public void testParse() {
44: String test = "\n\n\n\t\t " + "// ignore this ,a [ \" \n"
45: + "/* and this \n" + "/* and * // this \n" + "*/"
46: + "{ " + "\"onehundred\" : 100 ,"
47: + "\"name\" : \"fred\" ," + "\"empty\" : {} ,"
48: + "\"map\" : {\"a\":-1.0e2} ,"
49: + "\"array\" : [\"a\",-1.0e2,[],null,true,false] ,"
50: + "}";
51:
52: Map map = (Map) JSON.parse(test);
53: System.err.println(JSON.toString(map));
54: assertEquals(new Long(100), map.get("onehundred"));
55: assertEquals("fred", map.get("name"));
56: assertTrue(map.get("array").getClass().isArray());
57:
58: test = "{\"data\":{\"source\":\"15831407eqdaawf7\",\"widgetId\":\"Magnet_8\"},\"channel\":\"/magnets/moveStart\",\"connectionId\":null,\"clientId\":\"15831407eqdaawf7\"}";
59: map = (Map) JSON.parse(test);
60: }
61: }
|