forked from alibaba/fastjson
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathJSONTest.java
More file actions
159 lines (133 loc) · 6.88 KB
/
Copy pathJSONTest.java
File metadata and controls
159 lines (133 loc) · 6.88 KB
1
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
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
/*
* Copyright 1999-2101 Alibaba Group.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.alibaba.json.bvt;
import java.io.IOException;
import java.io.StringWriter;
import java.math.BigDecimal;
import java.util.HashMap;
import junit.framework.Assert;
import junit.framework.TestCase;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONException;
import com.alibaba.fastjson.JSONObject;
import com.alibaba.fastjson.serializer.SerializeWriter;
public class JSONTest extends TestCase {
public void test_number() throws Exception {
Assert.assertEquals("3", JSON.parse("3").toString());
Assert.assertEquals("34", JSON.parse("34").toString());
Assert.assertEquals("922337203685477580755", JSON.parse("922337203685477580755").toString());
Assert.assertEquals("-34", JSON.parse("-34").toString());
Assert.assertEquals(new BigDecimal("9.223372036854776E18"), new BigDecimal(JSON.parse("9.223372036854776E18").toString()));
Assert.assertEquals(new BigDecimal("9.223372036854776E+18"), new BigDecimal(JSON.parse("9.223372036854776E+18").toString()));
Assert.assertEquals(new BigDecimal("9.223372036854776E-18"), new BigDecimal(JSON.parse("9.223372036854776E-18").toString()));
}
public void test_string() throws Exception {
Assert.assertEquals("", JSON.parse("\"\"").toString());
Assert.assertEquals("3", JSON.parse("\"3\"").toString());
Assert.assertEquals("34", JSON.parse("\"34\"").toString());
Assert.assertEquals("3\\4", JSON.parse("\"3\\\\4\"").toString());
Assert.assertEquals("3\"4", JSON.parse("\"3\\\"4\"").toString());
Assert.assertEquals("3\\b4", JSON.parse("\"3\\\\b4\"").toString());
Assert.assertEquals("3\\f4", JSON.parse("\"3\\\\f4\"").toString());
Assert.assertEquals("3\\n4", JSON.parse("\"3\\\\n4\"").toString());
Assert.assertEquals("3\\r4", JSON.parse("\"3\\\\r4\"").toString());
Assert.assertEquals("3\\t4", JSON.parse("\"3\\\\t4\"").toString());
Assert.assertEquals("中国", JSON.parse("\"中国\"").toString());
Assert.assertEquals("中国", JSON.parse("\"\\u4E2D\\u56FD\"").toString());
Assert.assertEquals("\u001F", JSON.parse("\"\\u001F\"").toString());
}
public void test_for_jh() throws Exception {
String text = "[{\"I.13\":\"XEMwXFMweGEuMHhjOFxGy87M5VxUxOO6ww==\",\"I.18\":\"MA==\"},{\"I.13\":\"XEMwXFMweGEuMHhjOFxGy87M5VxUxOO6ww==\",\"I.18\":\"MA==\"}]";
JSON.parse(text);
JSON.parseArray(text);
}
public void test_value() throws Exception {
Assert.assertEquals(Boolean.TRUE, JSON.parse("true"));
Assert.assertEquals(Boolean.FALSE, JSON.parse("false"));
Assert.assertEquals(null, JSON.parse("null"));
}
public void test_object() throws Exception {
Assert.assertTrue(JSON.parseObject("{}").size() == 0);
Assert.assertEquals(1, JSON.parseObject("{\"K\":3}").size());
Assert.assertEquals(3, ((Number) JSON.parseObject("{\"K\":3}").get("K")).intValue());
Assert.assertEquals(2, JSON.parseObject("{\"K1\":3,\"K2\":4}").size());
Assert.assertEquals(3, ((Number) JSON.parseObject("{\"K1\":3,\"K2\":4}").get("K1")).intValue());
Assert.assertEquals(4, ((Number) JSON.parseObject("{\"K1\":3,\"K2\":4}").get("K2")).intValue());
Assert.assertEquals(1, JSON.parseObject("{\"K\":{}}").size());
Assert.assertEquals(1, JSON.parseObject("{\"K\":[]}").size());
}
public void test_array() throws Exception {
Assert.assertEquals(0, JSON.parseArray("[]").size());
Assert.assertEquals(1, JSON.parseArray("[1]").size());
Assert.assertEquals(1, ((Number) JSON.parseArray("[1]").get(0)).intValue());
Assert.assertEquals(3, JSON.parseArray("[1,2, 3]").size());
Assert.assertEquals(1, ((Number) JSON.parseArray("[1,2, 3]").get(0)).intValue());
Assert.assertEquals(2, ((Number) JSON.parseArray("[1,2, 3]").get(1)).intValue());
Assert.assertEquals(3, ((Number) JSON.parseArray("[1,2, 3]").get(2)).intValue());
}
public void test_all() throws Exception {
Assert.assertEquals(null, JSON.parse(null));
Assert.assertEquals("{}", JSON.toJSONString(new HashMap<String, Object>()));
Assert.assertEquals("{}", JSON.toJSONString(new HashMap<String, Object>(), true));
Assert.assertEquals("{}", JSON.toJSONString(new HashMap<String, Object>(), true));
Assert.assertEquals(null, JSON.parseObject(null));
Assert.assertEquals(null, JSON.parseArray(null));
Assert.assertEquals(null, JSON.parseObject(null, Object.class));
Assert.assertEquals(null, JSON.parseArray(null, Object.class));
}
public void test_writeTo_0() throws Exception {
SerializeWriter out = new SerializeWriter();
JSONObject json = new JSONObject();
json.writeJSONString(out);
Assert.assertEquals("{}", out.toString());
}
public void test_writeTo_1() throws Exception {
StringWriter out = new StringWriter();
JSONObject json = new JSONObject();
json.writeJSONString(out);
Assert.assertEquals("{}", out.toString());
}
public void test_writeTo_2() throws Exception {
StringBuffer out = new StringBuffer();
JSONObject json = new JSONObject();
json.writeJSONString(out);
Assert.assertEquals("{}", out.toString());
}
public void test_writeTo_error() throws Exception {
JSONException error = null;
try {
JSONObject json = new JSONObject();
json.writeJSONString(new ErrorAppendable());
} catch (JSONException e) {
error = e;
}
Assert.assertNotNull(error);
}
public void test_fromJavaObject_null() throws Exception {
Assert.assertEquals(null, JSON.toJSON(null));
}
private final class ErrorAppendable implements Appendable {
public Appendable append(CharSequence csq, int start, int end) throws IOException {
throw new IOException("");
}
public Appendable append(char c) throws IOException {
throw new IOException("");
}
public Appendable append(CharSequence csq) throws IOException {
throw new IOException("");
}
}
}