50 |
26 Feb 19 |
nicklas |
1 |
/* |
50 |
26 Feb 19 |
nicklas |
* $Id: Test.java,v 1.1 2006/04/15 14:40:06 platform Exp $ |
50 |
26 Feb 19 |
nicklas |
* Created on 2006-4-15 |
50 |
26 Feb 19 |
nicklas |
4 |
*/ |
50 |
26 Feb 19 |
nicklas |
5 |
package org.json.simple; |
50 |
26 Feb 19 |
nicklas |
6 |
|
50 |
26 Feb 19 |
nicklas |
7 |
import java.io.IOException; |
50 |
26 Feb 19 |
nicklas |
8 |
import java.io.StringWriter; |
50 |
26 Feb 19 |
nicklas |
9 |
import java.util.ArrayList; |
50 |
26 Feb 19 |
nicklas |
10 |
import java.util.HashMap; |
50 |
26 Feb 19 |
nicklas |
11 |
import java.util.Iterator; |
50 |
26 Feb 19 |
nicklas |
12 |
import java.util.LinkedHashMap; |
50 |
26 Feb 19 |
nicklas |
13 |
import java.util.LinkedList; |
50 |
26 Feb 19 |
nicklas |
14 |
import java.util.List; |
50 |
26 Feb 19 |
nicklas |
15 |
import java.util.Map; |
50 |
26 Feb 19 |
nicklas |
16 |
|
50 |
26 Feb 19 |
nicklas |
17 |
import junit.framework.TestCase; |
50 |
26 Feb 19 |
nicklas |
18 |
|
50 |
26 Feb 19 |
nicklas |
19 |
import org.json.simple.parser.ContainerFactory; |
50 |
26 Feb 19 |
nicklas |
20 |
import org.json.simple.parser.ContentHandler; |
50 |
26 Feb 19 |
nicklas |
21 |
import org.json.simple.parser.JSONParser; |
50 |
26 Feb 19 |
nicklas |
22 |
import org.json.simple.parser.ParseException; |
50 |
26 Feb 19 |
nicklas |
23 |
|
50 |
26 Feb 19 |
nicklas |
24 |
/** |
50 |
26 Feb 19 |
nicklas |
* @author FangYidong<fangyidong@yahoo.com.cn> |
50 |
26 Feb 19 |
nicklas |
26 |
*/ |
50 |
26 Feb 19 |
nicklas |
27 |
public class Test extends TestCase{ |
50 |
26 Feb 19 |
nicklas |
28 |
|
50 |
26 Feb 19 |
nicklas |
29 |
public void testDecode() throws Exception{ |
50 |
26 Feb 19 |
nicklas |
30 |
System.out.println("=======decode======="); |
50 |
26 Feb 19 |
nicklas |
31 |
|
50 |
26 Feb 19 |
nicklas |
32 |
String s="[0,{\"1\":{\"2\":{\"3\":{\"4\":[5,{\"6\":7}]}}}}]"; |
50 |
26 Feb 19 |
nicklas |
33 |
Object obj=JSONValue.parse(s); |
50 |
26 Feb 19 |
nicklas |
34 |
JSONArray array=(JSONArray)obj; |
50 |
26 Feb 19 |
nicklas |
35 |
System.out.println("======the 2nd element of array======"); |
50 |
26 Feb 19 |
nicklas |
36 |
System.out.println(array.get(1)); |
50 |
26 Feb 19 |
nicklas |
37 |
System.out.println(); |
50 |
26 Feb 19 |
nicklas |
38 |
assertEquals("{\"1\":{\"2\":{\"3\":{\"4\":[5,{\"6\":7}]}}}}",array.get(1).toString()); |
50 |
26 Feb 19 |
nicklas |
39 |
|
50 |
26 Feb 19 |
nicklas |
40 |
JSONObject obj2=(JSONObject)array.get(1); |
50 |
26 Feb 19 |
nicklas |
41 |
System.out.println("======field \"1\"=========="); |
50 |
26 Feb 19 |
nicklas |
42 |
System.out.println(obj2.get("1")); |
50 |
26 Feb 19 |
nicklas |
43 |
assertEquals("{\"2\":{\"3\":{\"4\":[5,{\"6\":7}]}}}",obj2.get("1").toString()); |
50 |
26 Feb 19 |
nicklas |
44 |
|
50 |
26 Feb 19 |
nicklas |
45 |
s="{}"; |
50 |
26 Feb 19 |
nicklas |
46 |
obj=JSONValue.parse(s); |
50 |
26 Feb 19 |
nicklas |
47 |
assertEquals("{}",obj.toString()); |
50 |
26 Feb 19 |
nicklas |
48 |
|
50 |
26 Feb 19 |
nicklas |
49 |
s="[5,]"; |
50 |
26 Feb 19 |
nicklas |
50 |
obj=JSONValue.parse(s); |
50 |
26 Feb 19 |
nicklas |
51 |
assertEquals("[5]",obj.toString()); |
50 |
26 Feb 19 |
nicklas |
52 |
|
50 |
26 Feb 19 |
nicklas |
53 |
s="[5,,2]"; |
50 |
26 Feb 19 |
nicklas |
54 |
obj=JSONValue.parse(s); |
50 |
26 Feb 19 |
nicklas |
55 |
assertEquals("[5,2]",obj.toString()); |
50 |
26 Feb 19 |
nicklas |
56 |
|
50 |
26 Feb 19 |
nicklas |
57 |
s="[\"hello\\bworld\\\"abc\\tdef\\\\ghi\\rjkl\\n123\\u4e2d\"]"; |
50 |
26 Feb 19 |
nicklas |
58 |
obj=JSONValue.parse(s); |
50 |
26 Feb 19 |
nicklas |
59 |
assertEquals("hello\bworld\"abc\tdef\\ghi\rjkl\n123δΈ",((List)obj).get(0).toString()); |
50 |
26 Feb 19 |
nicklas |
60 |
|
50 |
26 Feb 19 |
nicklas |
61 |
JSONParser parser = new JSONParser(); |
50 |
26 Feb 19 |
nicklas |
62 |
s="{\"name\":"; |
50 |
26 Feb 19 |
nicklas |
63 |
try{ |
50 |
26 Feb 19 |
nicklas |
64 |
obj = parser.parse(s); |
50 |
26 Feb 19 |
nicklas |
65 |
} |
50 |
26 Feb 19 |
nicklas |
66 |
catch(ParseException pe){ |
50 |
26 Feb 19 |
nicklas |
67 |
assertEquals(ParseException.ERROR_UNEXPECTED_TOKEN, pe.getErrorType()); |
50 |
26 Feb 19 |
nicklas |
68 |
assertEquals(8, pe.getPosition()); |
50 |
26 Feb 19 |
nicklas |
69 |
} |
50 |
26 Feb 19 |
nicklas |
70 |
|
50 |
26 Feb 19 |
nicklas |
71 |
s="{\"name\":}"; |
50 |
26 Feb 19 |
nicklas |
72 |
try{ |
50 |
26 Feb 19 |
nicklas |
73 |
obj = parser.parse(s); |
50 |
26 Feb 19 |
nicklas |
74 |
} |
50 |
26 Feb 19 |
nicklas |
75 |
catch(ParseException pe){ |
50 |
26 Feb 19 |
nicklas |
76 |
assertEquals(ParseException.ERROR_UNEXPECTED_TOKEN, pe.getErrorType()); |
50 |
26 Feb 19 |
nicklas |
77 |
assertEquals(8, pe.getPosition()); |
50 |
26 Feb 19 |
nicklas |
78 |
} |
50 |
26 Feb 19 |
nicklas |
79 |
|
50 |
26 Feb 19 |
nicklas |
80 |
|
50 |
26 Feb 19 |
nicklas |
81 |
s="{\"name"; |
50 |
26 Feb 19 |
nicklas |
82 |
try{ |
50 |
26 Feb 19 |
nicklas |
83 |
obj = parser.parse(s); |
50 |
26 Feb 19 |
nicklas |
84 |
} |
50 |
26 Feb 19 |
nicklas |
85 |
catch(ParseException pe){ |
50 |
26 Feb 19 |
nicklas |
86 |
assertEquals(ParseException.ERROR_UNEXPECTED_TOKEN, pe.getErrorType()); |
50 |
26 Feb 19 |
nicklas |
87 |
assertEquals(6, pe.getPosition()); |
50 |
26 Feb 19 |
nicklas |
88 |
} |
50 |
26 Feb 19 |
nicklas |
89 |
|
50 |
26 Feb 19 |
nicklas |
90 |
|
50 |
26 Feb 19 |
nicklas |
91 |
s = "[[null, 123.45, \"a\\\tb c\"}, true]"; |
50 |
26 Feb 19 |
nicklas |
92 |
try{ |
50 |
26 Feb 19 |
nicklas |
93 |
parser.parse(s); |
50 |
26 Feb 19 |
nicklas |
94 |
} |
50 |
26 Feb 19 |
nicklas |
95 |
catch(ParseException pe){ |
50 |
26 Feb 19 |
nicklas |
96 |
assertEquals(24, pe.getPosition()); |
50 |
26 Feb 19 |
nicklas |
97 |
System.out.println("Error at character position: " + pe.getPosition()); |
50 |
26 Feb 19 |
nicklas |
98 |
switch(pe.getErrorType()){ |
50 |
26 Feb 19 |
nicklas |
99 |
case ParseException.ERROR_UNEXPECTED_TOKEN: |
50 |
26 Feb 19 |
nicklas |
100 |
System.out.println("Unexpected token: " + pe.getUnexpectedObject()); |
50 |
26 Feb 19 |
nicklas |
101 |
break; |
50 |
26 Feb 19 |
nicklas |
102 |
case ParseException.ERROR_UNEXPECTED_CHAR: |
50 |
26 Feb 19 |
nicklas |
103 |
System.out.println("Unexpected character: " + pe.getUnexpectedObject()); |
50 |
26 Feb 19 |
nicklas |
104 |
break; |
50 |
26 Feb 19 |
nicklas |
105 |
case ParseException.ERROR_UNEXPECTED_EXCEPTION: |
50 |
26 Feb 19 |
nicklas |
106 |
((Exception)pe.getUnexpectedObject()).printStackTrace(); |
50 |
26 Feb 19 |
nicklas |
107 |
break; |
50 |
26 Feb 19 |
nicklas |
108 |
} |
50 |
26 Feb 19 |
nicklas |
109 |
} |
50 |
26 Feb 19 |
nicklas |
110 |
|
50 |
26 Feb 19 |
nicklas |
111 |
s = "{\"first\": 123, \"second\": [4, 5, 6], \"third\": 789}"; |
50 |
26 Feb 19 |
nicklas |
112 |
ContainerFactory containerFactory = new ContainerFactory(){ |
50 |
26 Feb 19 |
nicklas |
113 |
public List creatArrayContainer() { |
50 |
26 Feb 19 |
nicklas |
114 |
return new LinkedList(); |
50 |
26 Feb 19 |
nicklas |
115 |
} |
50 |
26 Feb 19 |
nicklas |
116 |
|
50 |
26 Feb 19 |
nicklas |
117 |
public Map createObjectContainer() { |
50 |
26 Feb 19 |
nicklas |
118 |
return new LinkedHashMap(); |
50 |
26 Feb 19 |
nicklas |
119 |
} |
50 |
26 Feb 19 |
nicklas |
120 |
|
50 |
26 Feb 19 |
nicklas |
121 |
}; |
50 |
26 Feb 19 |
nicklas |
122 |
|
50 |
26 Feb 19 |
nicklas |
123 |
try{ |
50 |
26 Feb 19 |
nicklas |
124 |
Map json = (Map)parser.parse(s, containerFactory); |
50 |
26 Feb 19 |
nicklas |
125 |
Iterator iter = json.entrySet().iterator(); |
50 |
26 Feb 19 |
nicklas |
126 |
System.out.println("==iterate result=="); |
50 |
26 Feb 19 |
nicklas |
127 |
while(iter.hasNext()){ |
50 |
26 Feb 19 |
nicklas |
128 |
Map.Entry entry = (Map.Entry)iter.next(); |
50 |
26 Feb 19 |
nicklas |
129 |
System.out.println(entry.getKey() + "=>" + entry.getValue()); |
50 |
26 Feb 19 |
nicklas |
130 |
} |
50 |
26 Feb 19 |
nicklas |
131 |
|
50 |
26 Feb 19 |
nicklas |
132 |
System.out.println("==toJSONString()=="); |
50 |
26 Feb 19 |
nicklas |
133 |
System.out.println(JSONValue.toJSONString(json)); |
50 |
26 Feb 19 |
nicklas |
134 |
assertEquals("{\"first\":123,\"second\":[4,5,6],\"third\":789}", JSONValue.toJSONString(json)); |
50 |
26 Feb 19 |
nicklas |
135 |
} |
50 |
26 Feb 19 |
nicklas |
136 |
catch(ParseException pe){ |
50 |
26 Feb 19 |
nicklas |
137 |
pe.printStackTrace(); |
50 |
26 Feb 19 |
nicklas |
138 |
} |
50 |
26 Feb 19 |
nicklas |
139 |
|
50 |
26 Feb 19 |
nicklas |
140 |
s = "{\"first\": 123, \"second\": [{\"s1\":{\"s11\":\"v11\"}}, 4, 5, 6], \"third\": 789}"; |
50 |
26 Feb 19 |
nicklas |
141 |
ContentHandler myHandler = new ContentHandler() { |
50 |
26 Feb 19 |
nicklas |
142 |
|
50 |
26 Feb 19 |
nicklas |
143 |
public boolean endArray() throws ParseException { |
50 |
26 Feb 19 |
nicklas |
144 |
System.out.println("endArray()"); |
50 |
26 Feb 19 |
nicklas |
145 |
return true; |
50 |
26 Feb 19 |
nicklas |
146 |
} |
50 |
26 Feb 19 |
nicklas |
147 |
|
50 |
26 Feb 19 |
nicklas |
148 |
public void endJSON() throws ParseException { |
50 |
26 Feb 19 |
nicklas |
149 |
System.out.println("endJSON()"); |
50 |
26 Feb 19 |
nicklas |
150 |
} |
50 |
26 Feb 19 |
nicklas |
151 |
|
50 |
26 Feb 19 |
nicklas |
152 |
public boolean endObject() throws ParseException { |
50 |
26 Feb 19 |
nicklas |
153 |
System.out.println("endObject()"); |
50 |
26 Feb 19 |
nicklas |
154 |
return true; |
50 |
26 Feb 19 |
nicklas |
155 |
} |
50 |
26 Feb 19 |
nicklas |
156 |
|
50 |
26 Feb 19 |
nicklas |
157 |
public boolean endObjectEntry() throws ParseException { |
50 |
26 Feb 19 |
nicklas |
158 |
System.out.println("endObjectEntry()"); |
50 |
26 Feb 19 |
nicklas |
159 |
return true; |
50 |
26 Feb 19 |
nicklas |
160 |
} |
50 |
26 Feb 19 |
nicklas |
161 |
|
50 |
26 Feb 19 |
nicklas |
162 |
public boolean primitive(Object value) throws ParseException { |
50 |
26 Feb 19 |
nicklas |
163 |
System.out.println("primitive(): " + value); |
50 |
26 Feb 19 |
nicklas |
164 |
return true; |
50 |
26 Feb 19 |
nicklas |
165 |
} |
50 |
26 Feb 19 |
nicklas |
166 |
|
50 |
26 Feb 19 |
nicklas |
167 |
public boolean startArray() throws ParseException { |
50 |
26 Feb 19 |
nicklas |
168 |
System.out.println("startArray()"); |
50 |
26 Feb 19 |
nicklas |
169 |
return true; |
50 |
26 Feb 19 |
nicklas |
170 |
} |
50 |
26 Feb 19 |
nicklas |
171 |
|
50 |
26 Feb 19 |
nicklas |
172 |
public void startJSON() throws ParseException { |
50 |
26 Feb 19 |
nicklas |
173 |
System.out.println("startJSON()"); |
50 |
26 Feb 19 |
nicklas |
174 |
} |
50 |
26 Feb 19 |
nicklas |
175 |
|
50 |
26 Feb 19 |
nicklas |
176 |
public boolean startObject() throws ParseException { |
50 |
26 Feb 19 |
nicklas |
177 |
System.out.println("startObject()"); |
50 |
26 Feb 19 |
nicklas |
178 |
return true; |
50 |
26 Feb 19 |
nicklas |
179 |
} |
50 |
26 Feb 19 |
nicklas |
180 |
|
50 |
26 Feb 19 |
nicklas |
181 |
public boolean startObjectEntry(String key) throws ParseException { |
50 |
26 Feb 19 |
nicklas |
182 |
System.out.println("startObjectEntry(), key:" + key); |
50 |
26 Feb 19 |
nicklas |
183 |
return true; |
50 |
26 Feb 19 |
nicklas |
184 |
} |
50 |
26 Feb 19 |
nicklas |
185 |
|
50 |
26 Feb 19 |
nicklas |
186 |
}; |
50 |
26 Feb 19 |
nicklas |
187 |
try{ |
50 |
26 Feb 19 |
nicklas |
188 |
parser.parse(s, myHandler); |
50 |
26 Feb 19 |
nicklas |
189 |
} |
50 |
26 Feb 19 |
nicklas |
190 |
catch(ParseException pe){ |
50 |
26 Feb 19 |
nicklas |
191 |
pe.printStackTrace(); |
50 |
26 Feb 19 |
nicklas |
192 |
} |
50 |
26 Feb 19 |
nicklas |
193 |
|
50 |
26 Feb 19 |
nicklas |
194 |
class KeyFinder implements ContentHandler{ |
50 |
26 Feb 19 |
nicklas |
195 |
private Object value; |
50 |
26 Feb 19 |
nicklas |
196 |
private boolean found = false; |
50 |
26 Feb 19 |
nicklas |
197 |
private boolean end = false; |
50 |
26 Feb 19 |
nicklas |
198 |
private String key; |
50 |
26 Feb 19 |
nicklas |
199 |
private String matchKey; |
50 |
26 Feb 19 |
nicklas |
200 |
|
50 |
26 Feb 19 |
nicklas |
201 |
public void setMatchKey(String matchKey){ |
50 |
26 Feb 19 |
nicklas |
202 |
this.matchKey = matchKey; |
50 |
26 Feb 19 |
nicklas |
203 |
} |
50 |
26 Feb 19 |
nicklas |
204 |
|
50 |
26 Feb 19 |
nicklas |
205 |
public Object getValue(){ |
50 |
26 Feb 19 |
nicklas |
206 |
return value; |
50 |
26 Feb 19 |
nicklas |
207 |
} |
50 |
26 Feb 19 |
nicklas |
208 |
|
50 |
26 Feb 19 |
nicklas |
209 |
public boolean isEnd(){ |
50 |
26 Feb 19 |
nicklas |
210 |
return end; |
50 |
26 Feb 19 |
nicklas |
211 |
} |
50 |
26 Feb 19 |
nicklas |
212 |
|
50 |
26 Feb 19 |
nicklas |
213 |
public void setFound(boolean found){ |
50 |
26 Feb 19 |
nicklas |
214 |
this.found = found; |
50 |
26 Feb 19 |
nicklas |
215 |
} |
50 |
26 Feb 19 |
nicklas |
216 |
|
50 |
26 Feb 19 |
nicklas |
217 |
public boolean isFound(){ |
50 |
26 Feb 19 |
nicklas |
218 |
return found; |
50 |
26 Feb 19 |
nicklas |
219 |
} |
50 |
26 Feb 19 |
nicklas |
220 |
|
50 |
26 Feb 19 |
nicklas |
221 |
public void startJSON() throws ParseException, IOException { |
50 |
26 Feb 19 |
nicklas |
222 |
found = false; |
50 |
26 Feb 19 |
nicklas |
223 |
end = false; |
50 |
26 Feb 19 |
nicklas |
224 |
} |
50 |
26 Feb 19 |
nicklas |
225 |
|
50 |
26 Feb 19 |
nicklas |
226 |
public void endJSON() throws ParseException, IOException { |
50 |
26 Feb 19 |
nicklas |
227 |
end = true; |
50 |
26 Feb 19 |
nicklas |
228 |
} |
50 |
26 Feb 19 |
nicklas |
229 |
|
50 |
26 Feb 19 |
nicklas |
230 |
public boolean primitive(Object value) throws ParseException, IOException { |
50 |
26 Feb 19 |
nicklas |
231 |
if(key != null){ |
50 |
26 Feb 19 |
nicklas |
232 |
if(key.equals(matchKey)){ |
50 |
26 Feb 19 |
nicklas |
233 |
found = true; |
50 |
26 Feb 19 |
nicklas |
234 |
this.value = value; |
50 |
26 Feb 19 |
nicklas |
235 |
key = null; |
50 |
26 Feb 19 |
nicklas |
236 |
return false; |
50 |
26 Feb 19 |
nicklas |
237 |
} |
50 |
26 Feb 19 |
nicklas |
238 |
} |
50 |
26 Feb 19 |
nicklas |
239 |
return true; |
50 |
26 Feb 19 |
nicklas |
240 |
} |
50 |
26 Feb 19 |
nicklas |
241 |
|
50 |
26 Feb 19 |
nicklas |
242 |
public boolean startArray() throws ParseException, IOException { |
50 |
26 Feb 19 |
nicklas |
243 |
return true; |
50 |
26 Feb 19 |
nicklas |
244 |
} |
50 |
26 Feb 19 |
nicklas |
245 |
|
50 |
26 Feb 19 |
nicklas |
246 |
|
50 |
26 Feb 19 |
nicklas |
247 |
public boolean startObject() throws ParseException, IOException { |
50 |
26 Feb 19 |
nicklas |
248 |
return true; |
50 |
26 Feb 19 |
nicklas |
249 |
} |
50 |
26 Feb 19 |
nicklas |
250 |
|
50 |
26 Feb 19 |
nicklas |
251 |
public boolean startObjectEntry(String key) throws ParseException, IOException { |
50 |
26 Feb 19 |
nicklas |
252 |
this.key = key; |
50 |
26 Feb 19 |
nicklas |
253 |
return true; |
50 |
26 Feb 19 |
nicklas |
254 |
} |
50 |
26 Feb 19 |
nicklas |
255 |
|
50 |
26 Feb 19 |
nicklas |
256 |
public boolean endArray() throws ParseException, IOException { |
50 |
26 Feb 19 |
nicklas |
257 |
return false; |
50 |
26 Feb 19 |
nicklas |
258 |
} |
50 |
26 Feb 19 |
nicklas |
259 |
|
50 |
26 Feb 19 |
nicklas |
260 |
public boolean endObject() throws ParseException, IOException { |
50 |
26 Feb 19 |
nicklas |
261 |
return true; |
50 |
26 Feb 19 |
nicklas |
262 |
} |
50 |
26 Feb 19 |
nicklas |
263 |
|
50 |
26 Feb 19 |
nicklas |
264 |
public boolean endObjectEntry() throws ParseException, IOException { |
50 |
26 Feb 19 |
nicklas |
265 |
return true; |
50 |
26 Feb 19 |
nicklas |
266 |
} |
50 |
26 Feb 19 |
nicklas |
267 |
}; |
50 |
26 Feb 19 |
nicklas |
268 |
|
50 |
26 Feb 19 |
nicklas |
269 |
s = "{\"first\": 123, \"second\": [{\"k1\":{\"id\":\"id1\"}}, 4, 5, 6, {\"id\": 123}], \"third\": 789, \"id\": null}"; |
50 |
26 Feb 19 |
nicklas |
270 |
parser.reset(); |
50 |
26 Feb 19 |
nicklas |
271 |
KeyFinder keyFinder = new KeyFinder(); |
50 |
26 Feb 19 |
nicklas |
272 |
keyFinder.setMatchKey("id"); |
50 |
26 Feb 19 |
nicklas |
273 |
int i = 0; |
50 |
26 Feb 19 |
nicklas |
274 |
try{ |
50 |
26 Feb 19 |
nicklas |
275 |
while(!keyFinder.isEnd()){ |
50 |
26 Feb 19 |
nicklas |
276 |
parser.parse(s, keyFinder, true); |
50 |
26 Feb 19 |
nicklas |
277 |
if(keyFinder.isFound()){ |
50 |
26 Feb 19 |
nicklas |
278 |
i++; |
50 |
26 Feb 19 |
nicklas |
279 |
keyFinder.setFound(false); |
50 |
26 Feb 19 |
nicklas |
280 |
System.out.println("found id:"); |
50 |
26 Feb 19 |
nicklas |
281 |
System.out.println(keyFinder.getValue()); |
50 |
26 Feb 19 |
nicklas |
282 |
if(i == 1) |
50 |
26 Feb 19 |
nicklas |
283 |
assertEquals("id1", keyFinder.getValue()); |
50 |
26 Feb 19 |
nicklas |
284 |
if(i == 2){ |
50 |
26 Feb 19 |
nicklas |
285 |
assertTrue(keyFinder.getValue() instanceof Number); |
50 |
26 Feb 19 |
nicklas |
286 |
assertEquals("123", String.valueOf(keyFinder.getValue())); |
50 |
26 Feb 19 |
nicklas |
287 |
} |
50 |
26 Feb 19 |
nicklas |
288 |
if(i == 3) |
50 |
26 Feb 19 |
nicklas |
289 |
assertTrue(null == keyFinder.getValue()); |
50 |
26 Feb 19 |
nicklas |
290 |
} |
50 |
26 Feb 19 |
nicklas |
291 |
} |
50 |
26 Feb 19 |
nicklas |
292 |
} |
50 |
26 Feb 19 |
nicklas |
293 |
catch(ParseException pe){ |
50 |
26 Feb 19 |
nicklas |
294 |
pe.printStackTrace(); |
50 |
26 Feb 19 |
nicklas |
295 |
} |
50 |
26 Feb 19 |
nicklas |
296 |
} |
50 |
26 Feb 19 |
nicklas |
297 |
|
50 |
26 Feb 19 |
nicklas |
298 |
public void testEncode() throws Exception{ |
50 |
26 Feb 19 |
nicklas |
299 |
System.out.println("=======encode======="); |
50 |
26 Feb 19 |
nicklas |
300 |
|
50 |
26 Feb 19 |
nicklas |
301 |
JSONArray array1=new JSONArray(); |
50 |
26 Feb 19 |
nicklas |
302 |
array1.add("abc\u0010a/"); |
50 |
26 Feb 19 |
nicklas |
303 |
array1.add(new Integer(123)); |
50 |
26 Feb 19 |
nicklas |
304 |
array1.add(new Double(222.123)); |
50 |
26 Feb 19 |
nicklas |
305 |
array1.add(new Boolean(true)); |
50 |
26 Feb 19 |
nicklas |
306 |
System.out.println("======array1=========="); |
50 |
26 Feb 19 |
nicklas |
307 |
System.out.println(array1); |
50 |
26 Feb 19 |
nicklas |
308 |
System.out.println(); |
50 |
26 Feb 19 |
nicklas |
309 |
assertEquals("[\"abc\\u0010a\\/\",123,222.123,true]",array1.toString()); |
50 |
26 Feb 19 |
nicklas |
310 |
|
50 |
26 Feb 19 |
nicklas |
311 |
JSONObject obj1=new JSONObject(); |
50 |
26 Feb 19 |
nicklas |
312 |
obj1.put("name","fang"); |
50 |
26 Feb 19 |
nicklas |
313 |
obj1.put("age",new Integer(27)); |
50 |
26 Feb 19 |
nicklas |
314 |
obj1.put("is_developer",new Boolean(true)); |
50 |
26 Feb 19 |
nicklas |
315 |
obj1.put("weight",new Double(60.21)); |
50 |
26 Feb 19 |
nicklas |
316 |
obj1.put("array1",array1); |
50 |
26 Feb 19 |
nicklas |
317 |
System.out.println("======obj1 with array1==========="); |
50 |
26 Feb 19 |
nicklas |
318 |
System.out.println(obj1); |
50 |
26 Feb 19 |
nicklas |
319 |
System.out.println(); |
50 |
26 Feb 19 |
nicklas |
320 |
assertEquals("{\"array1\":[\"abc\\u0010a\\/\",123,222.123,true],\"weight\":60.21,\"age\":27,\"name\":\"fang\",\"is_developer\":true}",obj1.toString()); |
50 |
26 Feb 19 |
nicklas |
321 |
|
50 |
26 Feb 19 |
nicklas |
322 |
obj1.remove("array1"); |
50 |
26 Feb 19 |
nicklas |
323 |
array1.add(obj1); |
50 |
26 Feb 19 |
nicklas |
324 |
System.out.println("======array1 with obj1========"); |
50 |
26 Feb 19 |
nicklas |
325 |
System.out.println(array1); |
50 |
26 Feb 19 |
nicklas |
326 |
System.out.println(); |
50 |
26 Feb 19 |
nicklas |
327 |
assertEquals("[\"abc\\u0010a\\/\",123,222.123,true,{\"weight\":60.21,\"age\":27,\"name\":\"fang\",\"is_developer\":true}]",array1.toString()); |
50 |
26 Feb 19 |
nicklas |
328 |
|
50 |
26 Feb 19 |
nicklas |
329 |
List list = new ArrayList(); |
50 |
26 Feb 19 |
nicklas |
330 |
list.add("abc\u0010a/"); |
50 |
26 Feb 19 |
nicklas |
331 |
list.add(new Integer(123)); |
50 |
26 Feb 19 |
nicklas |
332 |
list.add(new Double(222.123)); |
50 |
26 Feb 19 |
nicklas |
333 |
list.add(new Boolean(true)); |
50 |
26 Feb 19 |
nicklas |
334 |
list.add(null); |
50 |
26 Feb 19 |
nicklas |
335 |
System.out.println("======list=========="); |
50 |
26 Feb 19 |
nicklas |
336 |
System.out.println(JSONArray.toJSONString(list)); |
50 |
26 Feb 19 |
nicklas |
337 |
System.out.println(); |
50 |
26 Feb 19 |
nicklas |
338 |
assertEquals("[\"abc\\u0010a\\/\",123,222.123,true,null]",JSONArray.toJSONString(list)); |
50 |
26 Feb 19 |
nicklas |
339 |
|
50 |
26 Feb 19 |
nicklas |
340 |
Map map = new HashMap(); |
50 |
26 Feb 19 |
nicklas |
341 |
map.put("name","fang"); |
50 |
26 Feb 19 |
nicklas |
342 |
map.put("age",new Integer(27)); |
50 |
26 Feb 19 |
nicklas |
343 |
map.put("is_developer",new Boolean(true)); |
50 |
26 Feb 19 |
nicklas |
344 |
map.put("weight",new Double(60.21)); |
50 |
26 Feb 19 |
nicklas |
345 |
map.put("array1",list); |
50 |
26 Feb 19 |
nicklas |
346 |
System.out.println("======map with list==========="); |
50 |
26 Feb 19 |
nicklas |
347 |
System.out.println(map); |
50 |
26 Feb 19 |
nicklas |
348 |
System.out.println(); |
50 |
26 Feb 19 |
nicklas |
349 |
assertEquals("{\"array1\":[\"abc\\u0010a\\/\",123,222.123,true,null],\"weight\":60.21,\"age\":27,\"name\":\"fang\",\"is_developer\":true}",JSONObject.toJSONString(map)); |
50 |
26 Feb 19 |
nicklas |
350 |
|
50 |
26 Feb 19 |
nicklas |
351 |
Map m1 = new LinkedHashMap(); |
50 |
26 Feb 19 |
nicklas |
352 |
Map m2 = new HashMap(); |
50 |
26 Feb 19 |
nicklas |
353 |
List l1 = new LinkedList(); |
50 |
26 Feb 19 |
nicklas |
354 |
|
50 |
26 Feb 19 |
nicklas |
355 |
m1.put("k11","v11"); |
50 |
26 Feb 19 |
nicklas |
356 |
m1.put("k12","v12"); |
50 |
26 Feb 19 |
nicklas |
357 |
m1.put("k13", "v13"); |
50 |
26 Feb 19 |
nicklas |
358 |
m2.put("k21","v21"); |
50 |
26 Feb 19 |
nicklas |
359 |
m2.put("k22","v22"); |
50 |
26 Feb 19 |
nicklas |
360 |
m2.put("k23","v23"); |
50 |
26 Feb 19 |
nicklas |
361 |
l1.add(m1); |
50 |
26 Feb 19 |
nicklas |
362 |
l1.add(m2); |
50 |
26 Feb 19 |
nicklas |
363 |
String jsonString = JSONValue.toJSONString(l1); |
50 |
26 Feb 19 |
nicklas |
364 |
System.out.println(jsonString); |
50 |
26 Feb 19 |
nicklas |
365 |
assertEquals("[{\"k11\":\"v11\",\"k12\":\"v12\",\"k13\":\"v13\"},{\"k22\":\"v22\",\"k21\":\"v21\",\"k23\":\"v23\"}]", jsonString); |
50 |
26 Feb 19 |
nicklas |
366 |
|
50 |
26 Feb 19 |
nicklas |
367 |
StringWriter out = new StringWriter(); |
50 |
26 Feb 19 |
nicklas |
368 |
JSONValue.writeJSONString(l1, out); |
50 |
26 Feb 19 |
nicklas |
369 |
jsonString = out.toString(); |
50 |
26 Feb 19 |
nicklas |
370 |
System.out.println(jsonString); |
50 |
26 Feb 19 |
nicklas |
371 |
assertEquals("[{\"k11\":\"v11\",\"k12\":\"v12\",\"k13\":\"v13\"},{\"k22\":\"v22\",\"k21\":\"v21\",\"k23\":\"v23\"}]", jsonString); |
50 |
26 Feb 19 |
nicklas |
372 |
|
50 |
26 Feb 19 |
nicklas |
373 |
List l2 = new LinkedList(); |
50 |
26 Feb 19 |
nicklas |
374 |
Map m3 = new LinkedHashMap(); |
50 |
26 Feb 19 |
nicklas |
375 |
m3.put("k31", "v3"); |
50 |
26 Feb 19 |
nicklas |
376 |
m3.put("k32", new Double(123.45)); |
50 |
26 Feb 19 |
nicklas |
377 |
m3.put("k33", new Boolean(false)); |
50 |
26 Feb 19 |
nicklas |
378 |
m3.put("k34", null); |
50 |
26 Feb 19 |
nicklas |
379 |
l2.add("vvv"); |
50 |
26 Feb 19 |
nicklas |
380 |
l2.add("1.23456789123456789"); |
50 |
26 Feb 19 |
nicklas |
381 |
l2.add(new Boolean(true)); |
50 |
26 Feb 19 |
nicklas |
382 |
l2.add(null); |
50 |
26 Feb 19 |
nicklas |
383 |
m3.put("k35", l2); |
50 |
26 Feb 19 |
nicklas |
384 |
m1.put("k14", m3); |
50 |
26 Feb 19 |
nicklas |
385 |
out = new StringWriter(); |
50 |
26 Feb 19 |
nicklas |
386 |
JSONValue.writeJSONString(l1, out); |
50 |
26 Feb 19 |
nicklas |
387 |
jsonString = out.toString(); |
50 |
26 Feb 19 |
nicklas |
388 |
System.out.println(jsonString); |
50 |
26 Feb 19 |
nicklas |
389 |
assertEquals("[{\"k11\":\"v11\",\"k12\":\"v12\",\"k13\":\"v13\",\"k14\":{\"k31\":\"v3\",\"k32\":123.45,\"k33\":false,\"k34\":null,\"k35\":[\"vvv\",\"1.23456789123456789\",true,null]}},{\"k22\":\"v22\",\"k21\":\"v21\",\"k23\":\"v23\"}]",jsonString); |
50 |
26 Feb 19 |
nicklas |
390 |
} |
50 |
26 Feb 19 |
nicklas |
391 |
} |