4
4
import com .github .webee .json .JSONObject ;
5
5
import com .github .webee .json .WritableJSONObject ;
6
6
7
+ import org .junit .Assert ;
7
8
import org .junit .Test ;
8
9
9
- import java .util .Map ;
10
-
11
10
/**
12
11
* Created by webee on 16/11/25.
13
12
*/
@@ -22,68 +21,81 @@ public void testEncoding() {
22
21
jsonObject .set ("address" , null );
23
22
jsonObject .set ("height" , 1.74 );
24
23
jsonObject .set ("graduated" , true );
25
- jsonObject .set ("languages" , new Object []{"java" , "python" , "golang" });
24
+ jsonObject .set ("languages" , new Object []{"java" , "python" , "golang" , null });
26
25
27
26
WritableJSONObject scores = json .newObject ();
28
27
scores .set ("java" , 80 );
29
28
scores .set ("python" , 85.0 );
30
29
scores .set ("golang" , 82.5 );
30
+ scores .set ("xxx" , null );
31
31
jsonObject .set ("scores" , scores );
32
32
33
- System .out .println (jsonObject .get ("languages" ).getClass ());
34
- System .out .println (jsonObject .get ("scores" ).getClass ());
33
+ Assert .assertEquals (jsonObject .getString ("name" ), "webee.易" );
34
+ Assert .assertEquals (jsonObject .getInteger ("age" ), Integer .valueOf (27 ));
35
+ Assert .assertEquals (jsonObject .getDouble ("height" ), Double .valueOf (1.74 ));
36
+ Assert .assertEquals (jsonObject .getString ("address" ), null );
37
+ Assert .assertEquals (jsonObject .isNull ("address" ), true );
38
+ Assert .assertEquals (jsonObject .getBoolean ("graduated" ), true );
39
+ Assert .assertEquals (jsonObject .getArray ("languages" ).getString (0 ), "java" );
40
+ Assert .assertEquals (jsonObject .getArray ("languages" ).getString (3 ), null );
41
+ Assert .assertEquals (jsonObject .getArray ("languages" ).isNull (3 ), true );
42
+ Assert .assertEquals (jsonObject .getObject ("scores" ).getDouble ("java" ), Double .valueOf (80 ));
43
+ Assert .assertEquals (jsonObject .getObject ("scores" ).getDouble ("xxx" ), null );
44
+ Assert .assertEquals (jsonObject .getObject ("scores" ).isNull ("xxx" ), true );
45
+
46
+ //System.out.println(jsonObject.get("languages").getClass());
47
+ //System.out.println(jsonObject.get("scores").getClass());
35
48
System .out .println (jsonObject .toJSONString ());
36
49
}
37
50
38
51
@ Test
39
52
public void testDecoding () {
40
- String text = "{\" age\" :27,\" graduated\" :true,\" height\" :1.74,\" languages\" :[\" java\" ,\" python\" ,\" golang\" ],\" name\" :\" webee.易\" ,\" scores\" :{\" golang\" :82.5,\" java\" :80,\" python\" :85}}" ;
53
+ String text = "{\" address \" :null, \" age\" :27,\" graduated\" :true,\" height\" :1.74,\" languages\" :[\" java\" ,\" python\" ,\" golang\" ,null ],\" name\" :\" webee.易\" ,\" scores\" :{\" golang\" :82.5,\" java\" :80,\" python\" :85, \" xxx \" :null }}" ;
41
54
JSONObject jsonObject = json .parseObject (text );
42
55
43
- System . out . println (jsonObject .isNull ( "address" ) );
44
- System . out . println (jsonObject .get ( "languages" ). getClass ( ));
45
- System . out . println (jsonObject .get ( "scores" ). getClass ( ));
46
- System . out . println (jsonObject .toJSONString () );
47
- }
48
-
49
- @ Test
50
- public void test () {
51
- WritableJSONObject jsonObject = json . newObject ( );
52
-
53
- jsonObject .set ( "key" , "中国 \uD83D \uDE00 " );
54
- System . out . println (jsonObject .toJSONString () );
56
+ Assert . assertEquals (jsonObject .getString ( "name" ), "webee.易" );
57
+ Assert . assertEquals (jsonObject .getInteger ( "age" ), Integer . valueOf ( 27 ));
58
+ Assert . assertEquals (jsonObject .getDouble ( "height" ), Double . valueOf ( 1.74 ));
59
+ Assert . assertEquals (jsonObject .isNull ( "address" ), true );
60
+ Assert . assertEquals ( jsonObject . getString ( "address" ), null );
61
+ Assert . assertEquals ( jsonObject . getBoolean ( "graduated" ), true );
62
+ Assert . assertEquals ( jsonObject . getArray ( "languages" ). getString ( 0 ), "java" );
63
+ Assert . assertEquals ( jsonObject . getArray ( "languages" ). getString ( 3 ), null );
64
+ Assert . assertEquals ( jsonObject . getArray ( "languages" ). isNull ( 3 ), true );
65
+ Assert . assertEquals ( jsonObject . getObject ( "scores" ). getDouble ( "java" ), Double . valueOf ( 80 ));
66
+ Assert . assertEquals ( jsonObject .getObject ( "scores" ). getDouble ( "xxx" ), null );
67
+ Assert . assertEquals (jsonObject .getObject ( "scores" ). isNull ( "xxx" ), true );
55
68
}
56
69
57
70
@ Test
58
71
public void testParse () {
59
- System .out .println (json .parse ("null" ));
60
- System .out .println (json .parse ("true" ).getClass ());
61
- System .out .println (json .parse ("\" abc\" " ).getClass ());
62
- System .out .println (json .parse ("0" ).getClass ());
63
- System .out .println (json .parse ("123456789" ).getClass ());
64
- System .out .println (json .parse ("1234567890123456" ).getClass ());
65
- System .out .println (json .parse ("1234.0" ).getClass ());
66
- System .out .println (json .parse ("[]" ).getClass ());
67
- System .out .println (json .parseArray ("[]" ).get ().getClass ());
68
- System .out .println (json .parse ("{}" ).getClass ());
69
- System .out .println (json .parseObject ("{}" ).get ().getClass ());
70
- System .out .println (json .parseObject ("{\" a\" :{}}" ).get ("a" ).getClass ());
72
+ Assert .assertEquals (json .parse ("null" ), null );
73
+ Assert .assertEquals (json .parse ("true" ), true );
74
+ Assert .assertEquals (json .parse ("\" abc\" " ), "abc" );
75
+ Assert .assertEquals (json .parse ("0" ), 0 );
76
+ Assert .assertEquals (json .parse ("123456789" ), 123456789 );
77
+ Assert .assertEquals (json .parse ("1234567890123456" ), 1234567890123456L );
78
+ Assert .assertEquals (json .parse ("[]" ) instanceof com .github .webee .json .JSONArray , true );
79
+ Assert .assertArrayEquals (json .parseArray ("[]" ).get (), new Object [0 ]);
80
+ Assert .assertEquals (json .parse ("{}" ) instanceof com .github .webee .json .JSONObject , true );
81
+ Assert .assertEquals (json .parseObject ("{}" ) instanceof com .github .webee .json .JSONObject , true );
82
+ /*
71
83
Map<String, Object> a = (Map<String, Object>) json.parseObject("{\"a\":{\"b\":[1,2.3,{},999999999999999999999999999999999999999999999]}}").get("a");
72
84
System.out.println(a.get("b").getClass());
73
85
Object[] b = (Object[]) a.get("b");
74
86
System.out.println(b[0].getClass());
75
87
System.out.println(b[1].getClass());
76
88
System.out.println(b[2].getClass());
77
89
System.out.println(b[3].getClass());
90
+ */
78
91
}
79
92
80
93
@ Test
81
- public void testParseMsg () {
82
- String msg = "{\" messageType\" :0,\" text\" :\" txt2\" }" ;
83
- JSONObject value = json .parseObject (msg );
84
- Map <String , Object > map = value .get ();
85
- Integer t = value .getInteger ("messageType" );
86
- System .out .println (map );
87
- System .out .println (t );
94
+ public void testEmoji () {
95
+ WritableJSONObject jsonObject = json .newObject ();
96
+
97
+ String value = "中国\uD83D \uDE00 " ;
98
+ jsonObject .set ("key" , value );
99
+ Assert .assertEquals (jsonObject .getString ("key" ), value );
88
100
}
89
101
}
0 commit comments