由于最近需要通过一种跨平台、跨语言的方式来传递需要多层嵌套的数据,因此研究了一下JSON。首先试用JSON-lib做一下测试,但是未能通过测试,于是再找其他几个Java实现,但是也未能通过测试。分别测试对于一个简单的类的序列化和反序列化,对于一个有数组和Map域的对象的序列化和反序列化。对于结果比较失望。
其中的JSON-lib能通过第一个测试,第二个测试的时候则失败。但是有一个很大的bug,对于存在null域的对象反序列化的时候会失败。该bug已经在主页上了,但是不知道那个版本会修改。
其中的StringTree只能revert回一个Map对象。而对于一个复杂的对象,则无法revert。在做第二个测试的时候总是停在那里不动了。
其中的JSON-Tool根本就没有直接的序列化的功能。
由于测试情况很不理想,因此只进行了两个测试,对于特殊字符、国际化的测试没有进行。
结论:JSON的序列化和反序列化功能还不成熟。
所有代码下载地址http://www.blogjava.net/Files/justfly/jsonTest.zip
测试类如下:
1
/** */
/**
2 * created on 2006-10-2
3 */
4 package cn.shijingjia.justfly.json;
5
6 import java.util.HashMap;
7 import java.util.Map;
8
9 import junit.framework.Assert;
10 import net.sf.json.JSONObject;
11
12 import org.junit.Before;
13 import org.junit.Test;
14
15 import cn.shijingjia.justfly.json.imps.StringTreeImp;
16
17 /** */ /**
18 * @author Shi Jiemiao
19 *
20 */
21 public class JSonTest {
22
23 private SimpleClass simple1 = new SimpleClass();
24
25 private SimpleClass simple2 = new SimpleClass();
26
27 private SimpleClass simple3 = new SimpleClass();
28
29 private IJSonUtil < SimpleClass > simpUtil;
30
31 private IJSonUtil < CompositeClass > compositeUtil;
32
33 /** */ /**
34 * @throws java.lang.Exception
35 */
36 @Before
37 public void setUp() throws Exception {
38 simple1.setAInt( 1 );
39 simple1.setStr( " name1 " );
40 simple2.setAInt( 2 );
41 simple2.setStr( " name2 " );
42 simple3.setAInt( 3 );
43 simple3.setStr( " name3 " );
44
45 simpUtil = new StringTreeImp < SimpleClass > ();
46 compositeUtil = new StringTreeImp < CompositeClass > ();
47 }
48
49 /** */ /**
50 * Test if it can format a object and then reverted it back
51 */
52 @Test
53 public void simpleTest() {
54
55 String jsonString = simpUtil.toString(simple1);
56 System.out.println( " simpleClass\n " + jsonString);
57 SimpleClass revertedObj = simpUtil.toBean(jsonString);
58 Assert.assertEquals(simple1, revertedObj);
59 }
60
61 /** */ /**
62 * Test if it can format and revert a composite object with array and map
63 */
64 @Test
65 public void testComposite() {
66 // setup the origin composite class
67 CompositeClass composite = new CompositeClass();
68 composite.setName( " composite Instance " );
69 composite.setSimple(simple2);
70 composite.setSimples( new SimpleClass[] { simple1, simple3, simple2 } );
71 Map multiMap = new HashMap();
72 multiMap.put( " simpleObject " , simple3);
73 multiMap.put( " aString " , " This is a String " );
74 multiMap.put( " A integer " , 297 );
75 composite.setMultiMap(multiMap);
76 // orgin json object and jsonString
77 String jsonString = compositeUtil.toString(composite);
78 System.out.println( " composite class\n " + jsonString);
79
80 // Map atrMap = new HashMap();
81 // atrMap.put("simpleObject", SimpleClass.class);
82 // atrMap.put("aString", String.class);
83 // atrMap.put("A integer", Integer.class);
84
85 CompositeClass revertedBean = compositeUtil.toBean(jsonString);
86 Assert.assertEquals(composite, revertedBean);
87 }
88
89 }
90
2 * created on 2006-10-2
3 */
4 package cn.shijingjia.justfly.json;
5
6 import java.util.HashMap;
7 import java.util.Map;
8
9 import junit.framework.Assert;
10 import net.sf.json.JSONObject;
11
12 import org.junit.Before;
13 import org.junit.Test;
14
15 import cn.shijingjia.justfly.json.imps.StringTreeImp;
16
17 /** */ /**
18 * @author Shi Jiemiao
19 *
20 */
21 public class JSonTest {
22
23 private SimpleClass simple1 = new SimpleClass();
24
25 private SimpleClass simple2 = new SimpleClass();
26
27 private SimpleClass simple3 = new SimpleClass();
28
29 private IJSonUtil < SimpleClass > simpUtil;
30
31 private IJSonUtil < CompositeClass > compositeUtil;
32
33 /** */ /**
34 * @throws java.lang.Exception
35 */
36 @Before
37 public void setUp() throws Exception {
38 simple1.setAInt( 1 );
39 simple1.setStr( " name1 " );
40 simple2.setAInt( 2 );
41 simple2.setStr( " name2 " );
42 simple3.setAInt( 3 );
43 simple3.setStr( " name3 " );
44
45 simpUtil = new StringTreeImp < SimpleClass > ();
46 compositeUtil = new StringTreeImp < CompositeClass > ();
47 }
48
49 /** */ /**
50 * Test if it can format a object and then reverted it back
51 */
52 @Test
53 public void simpleTest() {
54
55 String jsonString = simpUtil.toString(simple1);
56 System.out.println( " simpleClass\n " + jsonString);
57 SimpleClass revertedObj = simpUtil.toBean(jsonString);
58 Assert.assertEquals(simple1, revertedObj);
59 }
60
61 /** */ /**
62 * Test if it can format and revert a composite object with array and map
63 */
64 @Test
65 public void testComposite() {
66 // setup the origin composite class
67 CompositeClass composite = new CompositeClass();
68 composite.setName( " composite Instance " );
69 composite.setSimple(simple2);
70 composite.setSimples( new SimpleClass[] { simple1, simple3, simple2 } );
71 Map multiMap = new HashMap();
72 multiMap.put( " simpleObject " , simple3);
73 multiMap.put( " aString " , " This is a String " );
74 multiMap.put( " A integer " , 297 );
75 composite.setMultiMap(multiMap);
76 // orgin json object and jsonString
77 String jsonString = compositeUtil.toString(composite);
78 System.out.println( " composite class\n " + jsonString);
79
80 // Map atrMap = new HashMap();
81 // atrMap.put("simpleObject", SimpleClass.class);
82 // atrMap.put("aString", String.class);
83 // atrMap.put("A integer", Integer.class);
84
85 CompositeClass revertedBean = compositeUtil.toBean(jsonString);
86 Assert.assertEquals(composite, revertedBean);
87 }
88
89 }
90