由于最近需要通過一種跨平臺、跨語言的方式來傳遞需要多層嵌套的數據,因此研究了一下JSON。首先試用JSON-lib做一下測試,但是未能通過測試,于是再找其他幾個Java實現,但是也未能通過測試。分別測試對于一個簡單的類的序列化和反序列化,對于一個有數組和Map域的對象的序列化和反序列化。對于結果比較失望。
其中的JSON-lib能通過第一個測試,第二個測試的時候則失敗。但是有一個很大的bug,對于存在null域的對象反序列化的時候會失敗。該bug已經在主頁上了,但是不知道那個版本會修改。
其中的StringTree只能revert回一個Map對象。而對于一個復雜的對象,則無法revert。在做第二個測試的時候總是停在那里不動了。
其中的JSON-Tool根本就沒有直接的序列化的功能。
由于測試情況很不理想,因此只進行了兩個測試,對于特殊字符、國際化的測試沒有進行。
結論:JSON的序列化和反序列化功能還不成熟。
所有代碼下載地址http://www.aygfsteel.com/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

?3

?4

?5

?6

?7

?8

?9

10

11

12

13

14

15

16

17


18

19

20

21



22

23

24

25

26

27

28

29

30

31

32

33


34

35

36

37



38

39

40

41

42

43

44

45

46

47

48

49


50

51

52

53



54

55

56

57

58

59

60

61


62

63

64

65



66

67

68

69

70



71

72

73

74

75

76

77

78

79

80

81

82

83

84

85

86

87

88

89

90
