root / src / org / json / Test.java @ 023f6f1e
History | View | Annotate | Download (27.7 kB)
1 |
package org.json; |
---|---|
2 |
|
3 |
import java.io.StringWriter; |
4 |
import java.util.Collection; |
5 |
import java.util.Iterator; |
6 |
import java.util.Map; |
7 |
|
8 |
/**
|
9 |
* Test class. This file is not formally a member of the org.json library.
|
10 |
* It is just a casual test tool.
|
11 |
*/
|
12 |
public class Test { |
13 |
|
14 |
/**
|
15 |
* Entry point.
|
16 |
* @param args
|
17 |
*/
|
18 |
public static void main(String args[]) { |
19 |
Iterator it;
|
20 |
JSONArray a; |
21 |
JSONObject j; |
22 |
JSONStringer jj; |
23 |
String s;
|
24 |
|
25 |
/**
|
26 |
* Obj is a typical class that implements JSONString. It also
|
27 |
* provides some beanie methods that can be used to
|
28 |
* construct a JSONObject. It also demonstrates constructing
|
29 |
* a JSONObject with an array of names.
|
30 |
*/
|
31 |
class Obj implements JSONString { |
32 |
public String aString; |
33 |
public double aNumber; |
34 |
public boolean aBoolean; |
35 |
|
36 |
public Obj(String string, double n, boolean b) { |
37 |
aString = string; |
38 |
aNumber = n; |
39 |
aBoolean = b; |
40 |
} |
41 |
|
42 |
public double getNumber() { |
43 |
return aNumber;
|
44 |
} |
45 |
|
46 |
public String getString() { |
47 |
return aString;
|
48 |
} |
49 |
|
50 |
public boolean isBoolean() { |
51 |
return aBoolean;
|
52 |
} |
53 |
|
54 |
public String getBENT() { |
55 |
return "All uppercase key"; |
56 |
} |
57 |
|
58 |
public String getX() { |
59 |
return "x"; |
60 |
} |
61 |
|
62 |
@Override
|
63 |
public String toJSONString() { |
64 |
return "{" + JSONObject.quote(aString) + ":" + |
65 |
JSONObject.doubleToString(aNumber) + "}";
|
66 |
} |
67 |
@Override
|
68 |
public String toString() { |
69 |
return getString() + " " + getNumber() + " " + |
70 |
isBoolean() + "." + getBENT() + " " + getX(); |
71 |
} |
72 |
} |
73 |
|
74 |
Obj obj = new Obj("A beany object", 42, true); |
75 |
|
76 |
try {
|
77 |
j = XML.toJSONObject("<![CDATA[This is a collection of test patterns and examples for org.json.]]> Ignore the stuff past the end. ");
|
78 |
System.out.println(j.toString());
|
79 |
|
80 |
s = "<recipe name=\"bread\" prep_time=\"5 mins\" cook_time=\"3 hours\"> <title>Basic bread</title> <ingredient amount=\"8\" unit=\"dL\">Flour</ingredient> <ingredient amount=\"10\" unit=\"grams\">Yeast</ingredient> <ingredient amount=\"4\" unit=\"dL\" state=\"warm\">Water</ingredient> <ingredient amount=\"1\" unit=\"teaspoon\">Salt</ingredient> <instructions> <step>Mix all ingredients together.</step> <step>Knead thoroughly.</step> <step>Cover with a cloth, and leave for one hour in warm room.</step> <step>Knead again.</step> <step>Place in a bread baking tin.</step> <step>Cover with a cloth, and leave for one hour in warm room.</step> <step>Bake in the oven at 180(degrees)C for 30 minutes.</step> </instructions> </recipe> ";
|
81 |
j = XML.toJSONObject(s); |
82 |
System.out.println(j.toString(4)); |
83 |
System.out.println();
|
84 |
|
85 |
j = JSONML.toJSONObject(s); |
86 |
System.out.println(j.toString());
|
87 |
System.out.println(JSONML.toString(j));
|
88 |
System.out.println();
|
89 |
|
90 |
a = JSONML.toJSONArray(s); |
91 |
System.out.println(a.toString(4)); |
92 |
System.out.println(JSONML.toString(a));
|
93 |
System.out.println();
|
94 |
|
95 |
s = "<div id=\"demo\" class=\"JSONML\"><p>JSONML is a transformation between <b>JSON</b> and <b>XML</b> that preserves ordering of document features.</p><p>JSONML can work with JSON arrays or JSON objects.</p><p>Three<br/>little<br/>words</p></div>";
|
96 |
j = JSONML.toJSONObject(s); |
97 |
System.out.println(j.toString(4)); |
98 |
System.out.println(JSONML.toString(j));
|
99 |
System.out.println();
|
100 |
|
101 |
a = JSONML.toJSONArray(s); |
102 |
System.out.println(a.toString(4)); |
103 |
System.out.println(JSONML.toString(a));
|
104 |
System.out.println();
|
105 |
|
106 |
s = "<person created=\"2006-11-11T19:23\" modified=\"2006-12-31T23:59\">\n <firstName>Robert</firstName>\n <lastName>Smith</lastName>\n <address type=\"home\">\n <street>12345 Sixth Ave</street>\n <city>Anytown</city>\n <state>CA</state>\n <postalCode>98765-4321</postalCode>\n </address>\n </person>";
|
107 |
j = XML.toJSONObject(s); |
108 |
System.out.println(j.toString(4)); |
109 |
|
110 |
j = new JSONObject(obj);
|
111 |
System.out.println(j.toString());
|
112 |
|
113 |
s = "{ \"entity\": { \"imageURL\": \"\", \"name\": \"IXXXXXXXXXXXXX\", \"id\": 12336, \"ratingCount\": null, \"averageRating\": null } }";
|
114 |
j = new JSONObject(s);
|
115 |
System.out.println(j.toString(2)); |
116 |
|
117 |
jj = new JSONStringer();
|
118 |
s = jj |
119 |
.object() |
120 |
.key("single")
|
121 |
.value("MARIE HAA'S")
|
122 |
.key("Johnny")
|
123 |
.value("MARIE HAA\\'S")
|
124 |
.key("foo")
|
125 |
.value("bar")
|
126 |
.key("baz")
|
127 |
.array() |
128 |
.object() |
129 |
.key("quux")
|
130 |
.value("Thanks, Josh!")
|
131 |
.endObject() |
132 |
.endArray() |
133 |
.key("obj keys")
|
134 |
.value(JSONObject.getNames(obj)) |
135 |
.endObject() |
136 |
.toString(); |
137 |
System.out.println(s);
|
138 |
|
139 |
System.out.println(new JSONStringer() |
140 |
.object() |
141 |
.key("a")
|
142 |
.array() |
143 |
.array() |
144 |
.array() |
145 |
.value("b")
|
146 |
.endArray() |
147 |
.endArray() |
148 |
.endArray() |
149 |
.endObject() |
150 |
.toString()); |
151 |
|
152 |
jj = new JSONStringer();
|
153 |
jj.array(); |
154 |
jj.value(1);
|
155 |
jj.array(); |
156 |
jj.value(null);
|
157 |
jj.array(); |
158 |
jj.object(); |
159 |
jj.key("empty-array").array().endArray();
|
160 |
jj.key("answer").value(42); |
161 |
jj.key("null").value(null); |
162 |
jj.key("false").value(false); |
163 |
jj.key("true").value(true); |
164 |
jj.key("big").value(123456789e+88); |
165 |
jj.key("small").value(123456789e-88); |
166 |
jj.key("empty-object").object().endObject();
|
167 |
jj.key("long");
|
168 |
jj.value(9223372036854775807L);
|
169 |
jj.endObject(); |
170 |
jj.value("two");
|
171 |
jj.endArray(); |
172 |
jj.value(true);
|
173 |
jj.endArray(); |
174 |
jj.value(98.6);
|
175 |
jj.value(-100.0);
|
176 |
jj.object(); |
177 |
jj.endObject(); |
178 |
jj.object(); |
179 |
jj.key("one");
|
180 |
jj.value(1.00);
|
181 |
jj.endObject(); |
182 |
jj.value(obj); |
183 |
jj.endArray(); |
184 |
System.out.println(jj.toString());
|
185 |
|
186 |
System.out.println(new JSONArray(jj.toString()).toString(4)); |
187 |
|
188 |
int ar[] = {1, 2, 3}; |
189 |
JSONArray ja = new JSONArray(ar);
|
190 |
System.out.println(ja.toString());
|
191 |
|
192 |
String sa[] = {"aString", "aNumber", "aBoolean"}; |
193 |
j = new JSONObject(obj, sa);
|
194 |
j.put("Testing JSONString interface", obj);
|
195 |
System.out.println(j.toString(4)); |
196 |
|
197 |
j = new JSONObject("{slashes: '///', closetag: '</script>', backslash:'\\\\', ei: {quotes: '\"\\''},eo: {a: '\"quoted\"', b:\"don't\"}, quotes: [\"'\", '\"']}"); |
198 |
System.out.println(j.toString(2)); |
199 |
System.out.println(XML.toString(j));
|
200 |
System.out.println(""); |
201 |
|
202 |
j = new JSONObject(
|
203 |
"{foo: [true, false,9876543210, 0.0, 1.00000001, 1.000000000001, 1.00000000000000001," +
|
204 |
" .00000000000000001, 2.00, 0.1, 2e100, -32,[],{}, \"string\"], " +
|
205 |
" to : null, op : 'Good'," +
|
206 |
"ten:10} postfix comment");
|
207 |
j.put("String", "98.6"); |
208 |
j.put("JSONObject", new JSONObject()); |
209 |
j.put("JSONArray", new JSONArray()); |
210 |
j.put("int", 57); |
211 |
j.put("double", 123456789012345678901234567890.); |
212 |
j.put("true", true); |
213 |
j.put("false", false); |
214 |
j.put("null", JSONObject.NULL);
|
215 |
j.put("bool", "true"); |
216 |
j.put("zero", -0.0); |
217 |
j.put("\\u2028", "\u2028"); |
218 |
j.put("\\u2029", "\u2029"); |
219 |
a = j.getJSONArray("foo");
|
220 |
a.put(666);
|
221 |
a.put(2001.99);
|
222 |
a.put("so \"fine\".");
|
223 |
a.put("so <fine>.");
|
224 |
a.put(true);
|
225 |
a.put(false);
|
226 |
a.put(new JSONArray());
|
227 |
a.put(new JSONObject());
|
228 |
j.put("keys", JSONObject.getNames(j));
|
229 |
System.out.println(j.toString(4)); |
230 |
System.out.println(XML.toString(j));
|
231 |
|
232 |
System.out.println("String: " + j.getDouble("String")); |
233 |
System.out.println(" bool: " + j.getBoolean("bool")); |
234 |
System.out.println(" to: " + j.getString("to")); |
235 |
System.out.println(" true: " + j.getString("true")); |
236 |
System.out.println(" foo: " + j.getJSONArray("foo")); |
237 |
System.out.println(" op: " + j.getString("op")); |
238 |
System.out.println(" ten: " + j.getInt("ten")); |
239 |
System.out.println(" oops: " + j.optBoolean("oops")); |
240 |
|
241 |
s = "<xml one = 1 two=' \"2\" '><five></five>First \u0009<content><five></five> This is \"content\". <three> 3 </three>JSON does not preserve the sequencing of elements and contents.<three> III </three> <three> T H R E E</three><four/>Content text is an implied structure in XML. <six content=\"6\"/>JSON does not have implied structure:<seven>7</seven>everything is explicit.<![CDATA[CDATA blocks<are><supported>!]]></xml>";
|
242 |
j = XML.toJSONObject(s); |
243 |
System.out.println(j.toString(2)); |
244 |
System.out.println(XML.toString(j));
|
245 |
System.out.println(""); |
246 |
|
247 |
ja = JSONML.toJSONArray(s); |
248 |
System.out.println(ja.toString(4)); |
249 |
System.out.println(JSONML.toString(ja));
|
250 |
System.out.println(""); |
251 |
|
252 |
s = "<xml do='0'>uno<a re='1' mi='2'>dos<b fa='3'/>tres<c>true</c>quatro</a>cinqo<d>seis<e/></d></xml>";
|
253 |
ja = JSONML.toJSONArray(s); |
254 |
System.out.println(ja.toString(4)); |
255 |
System.out.println(JSONML.toString(ja));
|
256 |
System.out.println(""); |
257 |
|
258 |
s = "<mapping><empty/> <class name = \"Customer\"> <field name = \"ID\" type = \"string\"> <bind-xml name=\"ID\" node=\"attribute\"/> </field> <field name = \"FirstName\" type = \"FirstName\"/> <field name = \"MI\" type = \"MI\"/> <field name = \"LastName\" type = \"LastName\"/> </class> <class name = \"FirstName\"> <field name = \"text\"> <bind-xml name = \"text\" node = \"text\"/> </field> </class> <class name = \"MI\"> <field name = \"text\"> <bind-xml name = \"text\" node = \"text\"/> </field> </class> <class name = \"LastName\"> <field name = \"text\"> <bind-xml name = \"text\" node = \"text\"/> </field> </class></mapping>";
|
259 |
j = XML.toJSONObject(s); |
260 |
|
261 |
System.out.println(j.toString(2)); |
262 |
System.out.println(XML.toString(j));
|
263 |
System.out.println(""); |
264 |
ja = JSONML.toJSONArray(s); |
265 |
System.out.println(ja.toString(4)); |
266 |
System.out.println(JSONML.toString(ja));
|
267 |
System.out.println(""); |
268 |
|
269 |
j = XML.toJSONObject("<?xml version=\"1.0\" ?><Book Author=\"Anonymous\"><Title>Sample Book</Title><Chapter id=\"1\">This is chapter 1. It is not very long or interesting.</Chapter><Chapter id=\"2\">This is chapter 2. Although it is longer than chapter 1, it is not any more interesting.</Chapter></Book>");
|
270 |
System.out.println(j.toString(2)); |
271 |
System.out.println(XML.toString(j));
|
272 |
System.out.println(""); |
273 |
|
274 |
j = XML.toJSONObject("<!DOCTYPE bCard 'http://www.cs.caltech.edu/~adam/schemas/bCard'><bCard><?xml default bCard firstname = '' lastname = '' company = '' email = '' homepage = ''?><bCard firstname = 'Rohit' lastname = 'Khare' company = 'MCI' email = 'khare@mci.net' homepage = 'http://pest.w3.org/'/><bCard firstname = 'Adam' lastname = 'Rifkin' company = 'Caltech Infospheres Project' email = 'adam@cs.caltech.edu' homepage = 'http://www.cs.caltech.edu/~adam/'/></bCard>");
|
275 |
System.out.println(j.toString(2)); |
276 |
System.out.println(XML.toString(j));
|
277 |
System.out.println(""); |
278 |
|
279 |
j = XML.toJSONObject("<?xml version=\"1.0\"?><customer> <firstName> <text>Fred</text> </firstName> <ID>fbs0001</ID> <lastName> <text>Scerbo</text> </lastName> <MI> <text>B</text> </MI></customer>");
|
280 |
System.out.println(j.toString(2)); |
281 |
System.out.println(XML.toString(j));
|
282 |
System.out.println(""); |
283 |
|
284 |
j = XML.toJSONObject("<!ENTITY tp-address PUBLIC '-//ABC University::Special Collections Library//TEXT (titlepage: name and address)//EN' 'tpspcoll.sgm'><list type='simple'><head>Repository Address </head><item>Special Collections Library</item><item>ABC University</item><item>Main Library, 40 Circle Drive</item><item>Ourtown, Pennsylvania</item><item>17654 USA</item></list>");
|
285 |
System.out.println(j.toString());
|
286 |
System.out.println(XML.toString(j));
|
287 |
System.out.println(""); |
288 |
|
289 |
j = XML.toJSONObject("<test intertag status=ok><empty/>deluxe<blip sweet=true>&"toot"&toot;A</blip><x>eks</x><w>bonus</w><w>bonus2</w></test>");
|
290 |
System.out.println(j.toString(2)); |
291 |
System.out.println(XML.toString(j));
|
292 |
System.out.println(""); |
293 |
|
294 |
j = HTTP.toJSONObject("GET / HTTP/1.0\nAccept: image/gif, image/x-xbitmap, image/jpeg, image/pjpeg, application/vnd.ms-powerpoint, application/vnd.ms-excel, application/msword, */*\nAccept-Language: en-us\nUser-Agent: Mozilla/4.0 (compatible; MSIE 5.5; Windows 98; Win 9x 4.90; T312461; Q312461)\nHost: www.nokko.com\nConnection: keep-alive\nAccept-encoding: gzip, deflate\n");
|
295 |
System.out.println(j.toString(2)); |
296 |
System.out.println(HTTP.toString(j));
|
297 |
System.out.println(""); |
298 |
|
299 |
j = HTTP.toJSONObject("HTTP/1.1 200 Oki Doki\nDate: Sun, 26 May 2002 17:38:52 GMT\nServer: Apache/1.3.23 (Unix) mod_perl/1.26\nKeep-Alive: timeout=15, max=100\nConnection: Keep-Alive\nTransfer-Encoding: chunked\nContent-Type: text/html\n");
|
300 |
System.out.println(j.toString(2)); |
301 |
System.out.println(HTTP.toString(j));
|
302 |
System.out.println(""); |
303 |
|
304 |
j = new JSONObject("{nix: null, nux: false, null: 'null', 'Request-URI': '/', Method: 'GET', 'HTTP-Version': 'HTTP/1.0'}"); |
305 |
System.out.println(j.toString(2)); |
306 |
System.out.println("isNull: " + j.isNull("nix")); |
307 |
System.out.println(" has: " + j.has("nix")); |
308 |
System.out.println(XML.toString(j));
|
309 |
System.out.println(HTTP.toString(j));
|
310 |
System.out.println(""); |
311 |
|
312 |
j = XML.toJSONObject("<?xml version='1.0' encoding='UTF-8'?>"+"\n\n"+"<SOAP-ENV:Envelope"+ |
313 |
" xmlns:SOAP-ENV=\"http://schemas.xmlsoap.org/soap/envelope/\""+
|
314 |
" xmlns:xsi=\"http://www.w3.org/1999/XMLSchema-instance\""+
|
315 |
" xmlns:xsd=\"http://www.w3.org/1999/XMLSchema\">"+
|
316 |
"<SOAP-ENV:Body><ns1:doGoogleSearch"+
|
317 |
" xmlns:ns1=\"urn:GoogleSearch\""+
|
318 |
" SOAP-ENV:encodingStyle=\"http://schemas.xmlsoap.org/soap/encoding/\">"+
|
319 |
"<key xsi:type=\"xsd:string\">GOOGLEKEY</key> <q"+
|
320 |
" xsi:type=\"xsd:string\">'+search+'</q> <start"+
|
321 |
" xsi:type=\"xsd:int\">0</start> <maxResults"+
|
322 |
" xsi:type=\"xsd:int\">10</maxResults> <filter"+
|
323 |
" xsi:type=\"xsd:boolean\">true</filter> <restrict"+
|
324 |
" xsi:type=\"xsd:string\"></restrict> <safeSearch"+
|
325 |
" xsi:type=\"xsd:boolean\">false</safeSearch> <lr"+
|
326 |
" xsi:type=\"xsd:string\"></lr> <ie"+
|
327 |
" xsi:type=\"xsd:string\">latin1</ie> <oe"+
|
328 |
" xsi:type=\"xsd:string\">latin1</oe>"+
|
329 |
"</ns1:doGoogleSearch>"+
|
330 |
"</SOAP-ENV:Body></SOAP-ENV:Envelope>");
|
331 |
System.out.println(j.toString(2)); |
332 |
System.out.println(XML.toString(j));
|
333 |
System.out.println(""); |
334 |
|
335 |
j = new JSONObject("{Envelope: {Body: {\"ns1:doGoogleSearch\": {oe: \"latin1\", filter: true, q: \"'+search+'\", key: \"GOOGLEKEY\", maxResults: 10, \"SOAP-ENV:encodingStyle\": \"http://schemas.xmlsoap.org/soap/encoding/\", start: 0, ie: \"latin1\", safeSearch:false, \"xmlns:ns1\": \"urn:GoogleSearch\"}}}}"); |
336 |
System.out.println(j.toString(2)); |
337 |
System.out.println(XML.toString(j));
|
338 |
System.out.println(""); |
339 |
|
340 |
j = CookieList.toJSONObject(" f%oo = b+l=ah ; o;n%40e = t.wo ");
|
341 |
System.out.println(j.toString(2)); |
342 |
System.out.println(CookieList.toString(j));
|
343 |
System.out.println(""); |
344 |
|
345 |
j = Cookie.toJSONObject("f%oo=blah; secure ;expires = April 24, 2002");
|
346 |
System.out.println(j.toString(2)); |
347 |
System.out.println(Cookie.toString(j));
|
348 |
System.out.println(""); |
349 |
|
350 |
j = new JSONObject("{script: 'It is not allowed in HTML to send a close script tag in a string<script>because it confuses browsers</script>so we insert a backslash before the /'}"); |
351 |
System.out.println(j.toString());
|
352 |
System.out.println(""); |
353 |
|
354 |
JSONTokener jt = new JSONTokener("{op:'test', to:'session', pre:1}{op:'test', to:'session', pre:2}"); |
355 |
j = new JSONObject(jt);
|
356 |
System.out.println(j.toString());
|
357 |
System.out.println("pre: " + j.optInt("pre")); |
358 |
int i = jt.skipTo('{'); |
359 |
System.out.println(i);
|
360 |
j = new JSONObject(jt);
|
361 |
System.out.println(j.toString());
|
362 |
System.out.println(""); |
363 |
|
364 |
a = CDL.toJSONArray("No quotes, 'Single Quotes', \"Double Quotes\"\n1,'2',\"3\"\n,'It is \"good,\"', \"It works.\"\n\n");
|
365 |
|
366 |
System.out.println(CDL.toString(a));
|
367 |
System.out.println(""); |
368 |
System.out.println(a.toString(4)); |
369 |
System.out.println(""); |
370 |
|
371 |
a = new JSONArray(" [\"<escape>\", next is an implied null , , ok,] "); |
372 |
System.out.println(a.toString());
|
373 |
System.out.println(""); |
374 |
System.out.println(XML.toString(a));
|
375 |
System.out.println(""); |
376 |
|
377 |
j = new JSONObject("{ fun => with non-standard forms ; forgiving => This package can be used to parse formats that are similar to but not stricting conforming to JSON; why=To make it easier to migrate existing data to JSON,one = [[1.00]]; uno=[[{1=>1}]];'+':+6e66 ;pluses=+++;empty = '' , 'double':0.666,true: TRUE, false: FALSE, null=NULL;[true] = [[!,@;*]]; string=> o. k. ; \r oct=0666; hex=0x666; dec=666; o=0999; noh=0x0x}"); |
378 |
System.out.println(j.toString(4)); |
379 |
System.out.println(""); |
380 |
if (j.getBoolean("true") && !j.getBoolean("false")) |
381 |
System.out.println("It's all good"); |
382 |
|
383 |
System.out.println(""); |
384 |
j = new JSONObject(j, new String[]{"dec", "oct", "hex", "missing"}); |
385 |
System.out.println(j.toString(4)); |
386 |
|
387 |
System.out.println(""); |
388 |
System.out.println(new JSONStringer().array().value(a).value(j).endArray()); |
389 |
|
390 |
j = new JSONObject("{string: \"98.6\", long: 2147483648, int: 2147483647, longer: 9223372036854775807, double: 9223372036854775808}"); |
391 |
System.out.println(j.toString(4)); |
392 |
|
393 |
System.out.println("\ngetInt"); |
394 |
System.out.println("int " + j.getInt("int")); |
395 |
System.out.println("long " + j.getInt("long")); |
396 |
System.out.println("longer " + j.getInt("longer")); |
397 |
System.out.println("double " + j.getInt("double")); |
398 |
System.out.println("string " + j.getInt("string")); |
399 |
|
400 |
System.out.println("\ngetLong"); |
401 |
System.out.println("int " + j.getLong("int")); |
402 |
System.out.println("long " + j.getLong("long")); |
403 |
System.out.println("longer " + j.getLong("longer")); |
404 |
System.out.println("double " + j.getLong("double")); |
405 |
System.out.println("string " + j.getLong("string")); |
406 |
|
407 |
System.out.println("\ngetDouble"); |
408 |
System.out.println("int " + j.getDouble("int")); |
409 |
System.out.println("long " + j.getDouble("long")); |
410 |
System.out.println("longer " + j.getDouble("longer")); |
411 |
System.out.println("double " + j.getDouble("double")); |
412 |
System.out.println("string " + j.getDouble("string")); |
413 |
|
414 |
j.put("good sized", 9223372036854775807L); |
415 |
System.out.println(j.toString(4)); |
416 |
|
417 |
a = new JSONArray("[2147483647, 2147483648, 9223372036854775807, 9223372036854775808]"); |
418 |
System.out.println(a.toString(4)); |
419 |
|
420 |
System.out.println("\nKeys: "); |
421 |
it = j.keys(); |
422 |
while (it.hasNext()) {
|
423 |
s = (String)it.next();
|
424 |
System.out.println(s + ": " + j.getString(s)); |
425 |
} |
426 |
|
427 |
|
428 |
System.out.println("\naccumulate: "); |
429 |
j = new JSONObject();
|
430 |
j.accumulate("stooge", "Curly"); |
431 |
j.accumulate("stooge", "Larry"); |
432 |
j.accumulate("stooge", "Moe"); |
433 |
a = j.getJSONArray("stooge");
|
434 |
a.put(5, "Shemp"); |
435 |
System.out.println(j.toString(4)); |
436 |
|
437 |
System.out.println("\nwrite:"); |
438 |
System.out.println(j.write(new StringWriter())); |
439 |
|
440 |
s = "<xml empty><a></a><a>1</a><a>22</a><a>333</a></xml>";
|
441 |
j = XML.toJSONObject(s); |
442 |
System.out.println(j.toString(4)); |
443 |
System.out.println(XML.toString(j));
|
444 |
|
445 |
s = "<book><chapter>Content of the first chapter</chapter><chapter>Content of the second chapter <chapter>Content of the first subchapter</chapter> <chapter>Content of the second subchapter</chapter></chapter><chapter>Third Chapter</chapter></book>";
|
446 |
j = XML.toJSONObject(s); |
447 |
System.out.println(j.toString(4)); |
448 |
System.out.println(XML.toString(j));
|
449 |
|
450 |
a = JSONML.toJSONArray(s); |
451 |
System.out.println(a.toString(4)); |
452 |
System.out.println(JSONML.toString(a));
|
453 |
|
454 |
Collection c = null; |
455 |
Map m = null; |
456 |
|
457 |
j = new JSONObject(m);
|
458 |
a = new JSONArray(c);
|
459 |
j.append("stooge", "Joe DeRita"); |
460 |
j.append("stooge", "Shemp"); |
461 |
j.accumulate("stooges", "Curly"); |
462 |
j.accumulate("stooges", "Larry"); |
463 |
j.accumulate("stooges", "Moe"); |
464 |
j.accumulate("stoogearray", j.get("stooges")); |
465 |
j.put("map", m);
|
466 |
j.put("collection", c);
|
467 |
j.put("array", a);
|
468 |
a.put(m); |
469 |
a.put(c); |
470 |
System.out.println(j.toString(4)); |
471 |
|
472 |
s = "{plist=Apple; AnimalSmells = { pig = piggish; lamb = lambish; worm = wormy; }; AnimalSounds = { pig = oink; lamb = baa; worm = baa; Lisa = \"Why is the worm talking like a lamb?\" } ; AnimalColors = { pig = pink; lamb = black; worm = pink; } } ";
|
473 |
j = new JSONObject(s);
|
474 |
System.out.println(j.toString(4)); |
475 |
|
476 |
s = " (\"San Francisco\", \"New York\", \"Seoul\", \"London\", \"Seattle\", \"Shanghai\")";
|
477 |
a = new JSONArray(s);
|
478 |
System.out.println(a.toString());
|
479 |
|
480 |
s = "<a ichi='1' ni='2'><b>The content of b</b> and <c san='3'>The content of c</c><d>do</d><e></e><d>re</d><f/><d>mi</d></a>";
|
481 |
j = XML.toJSONObject(s); |
482 |
|
483 |
System.out.println(j.toString(2)); |
484 |
System.out.println(XML.toString(j));
|
485 |
System.out.println(""); |
486 |
ja = JSONML.toJSONArray(s); |
487 |
System.out.println(ja.toString(4)); |
488 |
System.out.println(JSONML.toString(ja));
|
489 |
System.out.println(""); |
490 |
|
491 |
|
492 |
System.out.println("\nTesting Exceptions: "); |
493 |
|
494 |
System.out.print("Exception: "); |
495 |
try {
|
496 |
a = new JSONArray();
|
497 |
a.put(Double.NEGATIVE_INFINITY);
|
498 |
a.put(Double.NaN);
|
499 |
System.out.println(a.toString());
|
500 |
} catch (Exception e) { |
501 |
System.out.println(e);
|
502 |
} |
503 |
System.out.print("Exception: "); |
504 |
try {
|
505 |
System.out.println(j.getDouble("stooge")); |
506 |
} catch (Exception e) { |
507 |
System.out.println(e);
|
508 |
} |
509 |
System.out.print("Exception: "); |
510 |
try {
|
511 |
System.out.println(j.getDouble("howard")); |
512 |
} catch (Exception e) { |
513 |
System.out.println(e);
|
514 |
} |
515 |
System.out.print("Exception: "); |
516 |
try {
|
517 |
System.out.println(j.put(null, "howard")); |
518 |
} catch (Exception e) { |
519 |
System.out.println(e);
|
520 |
} |
521 |
System.out.print("Exception: "); |
522 |
try {
|
523 |
System.out.println(a.getDouble(0)); |
524 |
} catch (Exception e) { |
525 |
System.out.println(e);
|
526 |
} |
527 |
System.out.print("Exception: "); |
528 |
try {
|
529 |
System.out.println(a.get(-1)); |
530 |
} catch (Exception e) { |
531 |
System.out.println(e);
|
532 |
} |
533 |
System.out.print("Exception: "); |
534 |
try {
|
535 |
System.out.println(a.put(Double.NaN)); |
536 |
} catch (Exception e) { |
537 |
System.out.println(e);
|
538 |
} |
539 |
System.out.print("Exception: "); |
540 |
try {
|
541 |
j = XML.toJSONObject("<a><b> ");
|
542 |
} catch (Exception e) { |
543 |
System.out.println(e);
|
544 |
} |
545 |
System.out.print("Exception: "); |
546 |
try {
|
547 |
j = XML.toJSONObject("<a></b> ");
|
548 |
} catch (Exception e) { |
549 |
System.out.println(e);
|
550 |
} |
551 |
System.out.print("Exception: "); |
552 |
try {
|
553 |
j = XML.toJSONObject("<a></a ");
|
554 |
} catch (Exception e) { |
555 |
System.out.println(e);
|
556 |
} |
557 |
System.out.print("Exception: "); |
558 |
try {
|
559 |
ja = new JSONArray(new Object()); |
560 |
System.out.println(ja.toString());
|
561 |
} catch (Exception e) { |
562 |
System.out.println(e);
|
563 |
} |
564 |
|
565 |
System.out.print("Exception: "); |
566 |
try {
|
567 |
s = "[)";
|
568 |
a = new JSONArray(s);
|
569 |
System.out.println(a.toString());
|
570 |
} catch (Exception e) { |
571 |
System.out.println(e);
|
572 |
} |
573 |
|
574 |
System.out.print("Exception: "); |
575 |
try {
|
576 |
s = "<xml";
|
577 |
ja = JSONML.toJSONArray(s); |
578 |
System.out.println(ja.toString(4)); |
579 |
} catch (Exception e) { |
580 |
System.out.println(e);
|
581 |
} |
582 |
|
583 |
System.out.print("Exception: "); |
584 |
try {
|
585 |
s = "<right></wrong>";
|
586 |
ja = JSONML.toJSONArray(s); |
587 |
System.out.println(ja.toString(4)); |
588 |
} catch (Exception e) { |
589 |
System.out.println(e);
|
590 |
} |
591 |
|
592 |
System.out.print("Exception: "); |
593 |
try {
|
594 |
s = "{\"koda\": true, \"koda\": true}";
|
595 |
j = new JSONObject(s);
|
596 |
System.out.println(j.toString(4)); |
597 |
} catch (Exception e) { |
598 |
System.out.println(e);
|
599 |
} |
600 |
|
601 |
System.out.print("Exception: "); |
602 |
try {
|
603 |
jj = new JSONStringer();
|
604 |
s = jj |
605 |
.object() |
606 |
.key("bosanda")
|
607 |
.value("MARIE HAA'S")
|
608 |
.key("bosanda")
|
609 |
.value("MARIE HAA\\'S")
|
610 |
.endObject() |
611 |
.toString(); |
612 |
System.out.println(j.toString(4)); |
613 |
} catch (Exception e) { |
614 |
System.out.println(e);
|
615 |
} |
616 |
} catch (Exception e) { |
617 |
System.out.println(e.toString());
|
618 |
} |
619 |
} |
620 |
} |