0
|
1 package org.json;
|
|
2
|
|
3 import java.util.Collection;
|
|
4 import java.util.Iterator;
|
|
5 import java.util.Map;
|
|
6 import java.io.StringWriter;
|
|
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 this.aString = string;
|
|
38 this.aNumber = n;
|
|
39 this.aBoolean = b;
|
|
40 }
|
|
41
|
|
42 public double getNumber() {
|
|
43 return this.aNumber;
|
|
44 }
|
|
45
|
|
46 public String getString() {
|
|
47 return this.aString;
|
|
48 }
|
|
49
|
|
50 public boolean isBoolean() {
|
|
51 return this.aBoolean;
|
|
52 }
|
|
53
|
|
54 public String getBENT() {
|
|
55 return "All uppercase key";
|
|
56 }
|
|
57
|
|
58 public String getX() {
|
|
59 return "x";
|
|
60 }
|
|
61
|
|
62 public String toJSONString() {
|
|
63 return "{" + JSONObject.quote(this.aString) + ":" +
|
|
64 JSONObject.doubleToString(this.aNumber) + "}";
|
|
65 }
|
|
66 public String toString() {
|
|
67 return this.getString() + " " + this.getNumber() + " " +
|
|
68 this.isBoolean() + "." + this.getBENT() + " " + this.getX();
|
|
69 }
|
|
70 }
|
|
71
|
|
72
|
|
73 Obj obj = new Obj("A beany object", 42, true);
|
|
74
|
|
75 try {
|
|
76 j = XML.toJSONObject("<![CDATA[This is a collection of test patterns and examples for org.json.]]> Ignore the stuff past the end. ");
|
|
77 System.out.println(j.toString());
|
|
78
|
|
79 s = "{ \"list of lists\" : [ [1, 2, 3], [4, 5, 6], ] }";
|
|
80 j = new JSONObject(s);
|
|
81 System.out.println(j.toString(4));
|
|
82 System.out.println(XML.toString(j));
|
|
83
|
|
84 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> ";
|
|
85 j = XML.toJSONObject(s);
|
|
86 System.out.println(j.toString(4));
|
|
87 System.out.println();
|
|
88
|
|
89 j = JSONML.toJSONObject(s);
|
|
90 System.out.println(j.toString());
|
|
91 System.out.println(JSONML.toString(j));
|
|
92 System.out.println();
|
|
93
|
|
94 a = JSONML.toJSONArray(s);
|
|
95 System.out.println(a.toString(4));
|
|
96 System.out.println(JSONML.toString(a));
|
|
97 System.out.println();
|
|
98
|
|
99 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>";
|
|
100 j = JSONML.toJSONObject(s);
|
|
101 System.out.println(j.toString(4));
|
|
102 System.out.println(JSONML.toString(j));
|
|
103 System.out.println();
|
|
104
|
|
105 a = JSONML.toJSONArray(s);
|
|
106 System.out.println(a.toString(4));
|
|
107 System.out.println(JSONML.toString(a));
|
|
108 System.out.println();
|
|
109
|
|
110 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>";
|
|
111 j = XML.toJSONObject(s);
|
|
112 System.out.println(j.toString(4));
|
|
113
|
|
114 j = new JSONObject(obj);
|
|
115 System.out.println(j.toString());
|
|
116
|
|
117 s = "{ \"entity\": { \"imageURL\": \"\", \"name\": \"IXXXXXXXXXXXXX\", \"id\": 12336, \"ratingCount\": null, \"averageRating\": null } }";
|
|
118 j = new JSONObject(s);
|
|
119 System.out.println(j.toString(2));
|
|
120
|
|
121 jj = new JSONStringer();
|
|
122 s = jj
|
|
123 .object()
|
|
124 .key("single")
|
|
125 .value("MARIE HAA'S")
|
|
126 .key("Johnny")
|
|
127 .value("MARIE HAA\\'S")
|
|
128 .key("foo")
|
|
129 .value("bar")
|
|
130 .key("baz")
|
|
131 .array()
|
|
132 .object()
|
|
133 .key("quux")
|
|
134 .value("Thanks, Josh!")
|
|
135 .endObject()
|
|
136 .endArray()
|
|
137 .key("obj keys")
|
|
138 .value(JSONObject.getNames(obj))
|
|
139 .endObject()
|
|
140 .toString();
|
|
141 System.out.println(s);
|
|
142
|
|
143 System.out.println(new JSONStringer()
|
|
144 .object()
|
|
145 .key("a")
|
|
146 .array()
|
|
147 .array()
|
|
148 .array()
|
|
149 .value("b")
|
|
150 .endArray()
|
|
151 .endArray()
|
|
152 .endArray()
|
|
153 .endObject()
|
|
154 .toString());
|
|
155
|
|
156 jj = new JSONStringer();
|
|
157 jj.array();
|
|
158 jj.value(1);
|
|
159 jj.array();
|
|
160 jj.value(null);
|
|
161 jj.array();
|
|
162 jj.object();
|
|
163 jj.key("empty-array").array().endArray();
|
|
164 jj.key("answer").value(42);
|
|
165 jj.key("null").value(null);
|
|
166 jj.key("false").value(false);
|
|
167 jj.key("true").value(true);
|
|
168 jj.key("big").value(123456789e+88);
|
|
169 jj.key("small").value(123456789e-88);
|
|
170 jj.key("empty-object").object().endObject();
|
|
171 jj.key("long");
|
|
172 jj.value(9223372036854775807L);
|
|
173 jj.endObject();
|
|
174 jj.value("two");
|
|
175 jj.endArray();
|
|
176 jj.value(true);
|
|
177 jj.endArray();
|
|
178 jj.value(98.6);
|
|
179 jj.value(-100.0);
|
|
180 jj.object();
|
|
181 jj.endObject();
|
|
182 jj.object();
|
|
183 jj.key("one");
|
|
184 jj.value(1.00);
|
|
185 jj.endObject();
|
|
186 jj.value(obj);
|
|
187 jj.endArray();
|
|
188 System.out.println(jj.toString());
|
|
189
|
|
190 System.out.println(new JSONArray(jj.toString()).toString(4));
|
|
191
|
|
192 int ar[] = {1, 2, 3};
|
|
193 JSONArray ja = new JSONArray(ar);
|
|
194 System.out.println(ja.toString());
|
|
195
|
|
196 String sa[] = {"aString", "aNumber", "aBoolean"};
|
|
197 j = new JSONObject(obj, sa);
|
|
198 j.put("Testing JSONString interface", obj);
|
|
199 System.out.println(j.toString(4));
|
|
200
|
|
201 j = new JSONObject("{slashes: '///', closetag: '</script>', backslash:'\\\\', ei: {quotes: '\"\\''},eo: {a: '\"quoted\"', b:\"don't\"}, quotes: [\"'\", '\"']}");
|
|
202 System.out.println(j.toString(2));
|
|
203 System.out.println(XML.toString(j));
|
|
204 System.out.println("");
|
|
205
|
|
206 j = new JSONObject(
|
|
207 "{foo: [true, false,9876543210, 0.0, 1.00000001, 1.000000000001, 1.00000000000000001," +
|
|
208 " .00000000000000001, 2.00, 0.1, 2e100, -32,[],{}, \"string\"], " +
|
|
209 " to : null, op : 'Good'," +
|
|
210 "ten:10} postfix comment");
|
|
211 j.put("String", "98.6");
|
|
212 j.put("JSONObject", new JSONObject());
|
|
213 j.put("JSONArray", new JSONArray());
|
|
214 j.put("int", 57);
|
|
215 j.put("double", 123456789012345678901234567890.);
|
|
216 j.put("true", true);
|
|
217 j.put("false", false);
|
|
218 j.put("null", JSONObject.NULL);
|
|
219 j.put("bool", "true");
|
|
220 j.put("zero", -0.0);
|
|
221 j.put("\\u2028", "\u2028");
|
|
222 j.put("\\u2029", "\u2029");
|
|
223 a = j.getJSONArray("foo");
|
|
224 a.put(666);
|
|
225 a.put(2001.99);
|
|
226 a.put("so \"fine\".");
|
|
227 a.put("so <fine>.");
|
|
228 a.put(true);
|
|
229 a.put(false);
|
|
230 a.put(new JSONArray());
|
|
231 a.put(new JSONObject());
|
|
232 j.put("keys", JSONObject.getNames(j));
|
|
233 System.out.println(j.toString(4));
|
|
234 System.out.println(XML.toString(j));
|
|
235
|
|
236 System.out.println("String: " + j.getDouble("String"));
|
|
237 System.out.println(" bool: " + j.getBoolean("bool"));
|
|
238 System.out.println(" to: " + j.getString("to"));
|
|
239 System.out.println(" true: " + j.getString("true"));
|
|
240 System.out.println(" foo: " + j.getJSONArray("foo"));
|
|
241 System.out.println(" op: " + j.getString("op"));
|
|
242 System.out.println(" ten: " + j.getInt("ten"));
|
|
243 System.out.println(" oops: " + j.optBoolean("oops"));
|
|
244
|
|
245 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>";
|
|
246 j = XML.toJSONObject(s);
|
|
247 System.out.println(j.toString(2));
|
|
248 System.out.println(XML.toString(j));
|
|
249 System.out.println("");
|
|
250
|
|
251 ja = JSONML.toJSONArray(s);
|
|
252 System.out.println(ja.toString(4));
|
|
253 System.out.println(JSONML.toString(ja));
|
|
254 System.out.println("");
|
|
255
|
|
256 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>";
|
|
257 ja = JSONML.toJSONArray(s);
|
|
258 System.out.println(ja.toString(4));
|
|
259 System.out.println(JSONML.toString(ja));
|
|
260 System.out.println("");
|
|
261
|
|
262 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>";
|
|
263 j = XML.toJSONObject(s);
|
|
264
|
|
265 System.out.println(j.toString(2));
|
|
266 System.out.println(XML.toString(j));
|
|
267 System.out.println("");
|
|
268 ja = JSONML.toJSONArray(s);
|
|
269 System.out.println(ja.toString(4));
|
|
270 System.out.println(JSONML.toString(ja));
|
|
271 System.out.println("");
|
|
272
|
|
273 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>");
|
|
274 System.out.println(j.toString(2));
|
|
275 System.out.println(XML.toString(j));
|
|
276 System.out.println("");
|
|
277
|
|
278 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>");
|
|
279 System.out.println(j.toString(2));
|
|
280 System.out.println(XML.toString(j));
|
|
281 System.out.println("");
|
|
282
|
|
283 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>");
|
|
284 System.out.println(j.toString(2));
|
|
285 System.out.println(XML.toString(j));
|
|
286 System.out.println("");
|
|
287
|
|
288 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>");
|
|
289 System.out.println(j.toString());
|
|
290 System.out.println(XML.toString(j));
|
|
291 System.out.println("");
|
|
292
|
|
293 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>");
|
|
294 System.out.println(j.toString(2));
|
|
295 System.out.println(XML.toString(j));
|
|
296 System.out.println("");
|
|
297
|
|
298 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");
|
|
299 System.out.println(j.toString(2));
|
|
300 System.out.println(HTTP.toString(j));
|
|
301 System.out.println("");
|
|
302
|
|
303 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");
|
|
304 System.out.println(j.toString(2));
|
|
305 System.out.println(HTTP.toString(j));
|
|
306 System.out.println("");
|
|
307
|
|
308 j = new JSONObject("{nix: null, nux: false, null: 'null', 'Request-URI': '/', Method: 'GET', 'HTTP-Version': 'HTTP/1.0'}");
|
|
309 System.out.println(j.toString(2));
|
|
310 System.out.println("isNull: " + j.isNull("nix"));
|
|
311 System.out.println(" has: " + j.has("nix"));
|
|
312 System.out.println(XML.toString(j));
|
|
313 System.out.println(HTTP.toString(j));
|
|
314 System.out.println("");
|
|
315
|
|
316 j = XML.toJSONObject("<?xml version='1.0' encoding='UTF-8'?>"+"\n\n"+"<SOAP-ENV:Envelope"+
|
|
317 " xmlns:SOAP-ENV=\"http://schemas.xmlsoap.org/soap/envelope/\""+
|
|
318 " xmlns:xsi=\"http://www.w3.org/1999/XMLSchema-instance\""+
|
|
319 " xmlns:xsd=\"http://www.w3.org/1999/XMLSchema\">"+
|
|
320 "<SOAP-ENV:Body><ns1:doGoogleSearch"+
|
|
321 " xmlns:ns1=\"urn:GoogleSearch\""+
|
|
322 " SOAP-ENV:encodingStyle=\"http://schemas.xmlsoap.org/soap/encoding/\">"+
|
|
323 "<key xsi:type=\"xsd:string\">GOOGLEKEY</key> <q"+
|
|
324 " xsi:type=\"xsd:string\">'+search+'</q> <start"+
|
|
325 " xsi:type=\"xsd:int\">0</start> <maxResults"+
|
|
326 " xsi:type=\"xsd:int\">10</maxResults> <filter"+
|
|
327 " xsi:type=\"xsd:boolean\">true</filter> <restrict"+
|
|
328 " xsi:type=\"xsd:string\"></restrict> <safeSearch"+
|
|
329 " xsi:type=\"xsd:boolean\">false</safeSearch> <lr"+
|
|
330 " xsi:type=\"xsd:string\"></lr> <ie"+
|
|
331 " xsi:type=\"xsd:string\">latin1</ie> <oe"+
|
|
332 " xsi:type=\"xsd:string\">latin1</oe>"+
|
|
333 "</ns1:doGoogleSearch>"+
|
|
334 "</SOAP-ENV:Body></SOAP-ENV:Envelope>");
|
|
335 System.out.println(j.toString(2));
|
|
336 System.out.println(XML.toString(j));
|
|
337 System.out.println("");
|
|
338
|
|
339 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\"}}}}");
|
|
340 System.out.println(j.toString(2));
|
|
341 System.out.println(XML.toString(j));
|
|
342 System.out.println("");
|
|
343
|
|
344 j = CookieList.toJSONObject(" f%oo = b+l=ah ; o;n%40e = t.wo ");
|
|
345 System.out.println(j.toString(2));
|
|
346 System.out.println(CookieList.toString(j));
|
|
347 System.out.println("");
|
|
348
|
|
349 j = Cookie.toJSONObject("f%oo=blah; secure ;expires = April 24, 2002");
|
|
350 System.out.println(j.toString(2));
|
|
351 System.out.println(Cookie.toString(j));
|
|
352 System.out.println("");
|
|
353
|
|
354 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 /'}");
|
|
355 System.out.println(j.toString());
|
|
356 System.out.println("");
|
|
357
|
|
358 JSONTokener jt = new JSONTokener("{op:'test', to:'session', pre:1}{op:'test', to:'session', pre:2}");
|
|
359 j = new JSONObject(jt);
|
|
360 System.out.println(j.toString());
|
|
361 System.out.println("pre: " + j.optInt("pre"));
|
|
362 int i = jt.skipTo('{');
|
|
363 System.out.println(i);
|
|
364 j = new JSONObject(jt);
|
|
365 System.out.println(j.toString());
|
|
366 System.out.println("");
|
|
367
|
|
368 a = CDL.toJSONArray("Comma delimited list test, '\"Strip\"Quotes', 'quote, comma', No quotes, 'Single Quotes', \"Double Quotes\"\n1,'2',\"3\"\n,'It is \"good,\"', \"It works.\"\n\n");
|
|
369
|
|
370 s = CDL.toString(a);
|
|
371 System.out.println(s);
|
|
372 System.out.println("");
|
|
373 System.out.println(a.toString(4));
|
|
374 System.out.println("");
|
|
375 a = CDL.toJSONArray(s);
|
|
376 System.out.println(a.toString(4));
|
|
377 System.out.println("");
|
|
378
|
|
379 a = new JSONArray(" [\"<escape>\", next is an implied null , , ok,] ");
|
|
380 System.out.println(a.toString());
|
|
381 System.out.println("");
|
|
382 System.out.println(XML.toString(a));
|
|
383 System.out.println("");
|
|
384
|
|
385 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}");
|
|
386 System.out.println(j.toString(4));
|
|
387 System.out.println("");
|
|
388 if (j.getBoolean("true") && !j.getBoolean("false")) {
|
|
389 System.out.println("It's all good");
|
|
390 }
|
|
391
|
|
392 System.out.println("");
|
|
393 j = new JSONObject(j, new String[]{"dec", "oct", "hex", "missing"});
|
|
394 System.out.println(j.toString(4));
|
|
395
|
|
396 System.out.println("");
|
|
397 System.out.println(new JSONStringer().array().value(a).value(j).endArray());
|
|
398
|
|
399 j = new JSONObject("{string: \"98.6\", long: 2147483648, int: 2147483647, longer: 9223372036854775807, double: 9223372036854775808}");
|
|
400 System.out.println(j.toString(4));
|
|
401
|
|
402 System.out.println("\ngetInt");
|
|
403 System.out.println("int " + j.getInt("int"));
|
|
404 System.out.println("long " + j.getInt("long"));
|
|
405 System.out.println("longer " + j.getInt("longer"));
|
|
406 System.out.println("double " + j.getInt("double"));
|
|
407 System.out.println("string " + j.getInt("string"));
|
|
408
|
|
409 System.out.println("\ngetLong");
|
|
410 System.out.println("int " + j.getLong("int"));
|
|
411 System.out.println("long " + j.getLong("long"));
|
|
412 System.out.println("longer " + j.getLong("longer"));
|
|
413 System.out.println("double " + j.getLong("double"));
|
|
414 System.out.println("string " + j.getLong("string"));
|
|
415
|
|
416 System.out.println("\ngetDouble");
|
|
417 System.out.println("int " + j.getDouble("int"));
|
|
418 System.out.println("long " + j.getDouble("long"));
|
|
419 System.out.println("longer " + j.getDouble("longer"));
|
|
420 System.out.println("double " + j.getDouble("double"));
|
|
421 System.out.println("string " + j.getDouble("string"));
|
|
422
|
|
423 j.put("good sized", 9223372036854775807L);
|
|
424 System.out.println(j.toString(4));
|
|
425
|
|
426 a = new JSONArray("[2147483647, 2147483648, 9223372036854775807, 9223372036854775808]");
|
|
427 System.out.println(a.toString(4));
|
|
428
|
|
429 System.out.println("\nKeys: ");
|
|
430 it = j.keys();
|
|
431 while (it.hasNext()) {
|
|
432 s = (String)it.next();
|
|
433 System.out.println(s + ": " + j.getString(s));
|
|
434 }
|
|
435
|
|
436
|
|
437 System.out.println("\naccumulate: ");
|
|
438 j = new JSONObject();
|
|
439 j.accumulate("stooge", "Curly");
|
|
440 j.accumulate("stooge", "Larry");
|
|
441 j.accumulate("stooge", "Moe");
|
|
442 a = j.getJSONArray("stooge");
|
|
443 a.put(5, "Shemp");
|
|
444 System.out.println(j.toString(4));
|
|
445
|
|
446 System.out.println("\nwrite:");
|
|
447 System.out.println(j.write(new StringWriter()));
|
|
448
|
|
449 s = "<xml empty><a></a><a>1</a><a>22</a><a>333</a></xml>";
|
|
450 j = XML.toJSONObject(s);
|
|
451 System.out.println(j.toString(4));
|
|
452 System.out.println(XML.toString(j));
|
|
453
|
|
454 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>";
|
|
455 j = XML.toJSONObject(s);
|
|
456 System.out.println(j.toString(4));
|
|
457 System.out.println(XML.toString(j));
|
|
458
|
|
459 a = JSONML.toJSONArray(s);
|
|
460 System.out.println(a.toString(4));
|
|
461 System.out.println(JSONML.toString(a));
|
|
462
|
|
463 Collection c = null;
|
|
464 Map m = null;
|
|
465
|
|
466 j = new JSONObject(m);
|
|
467 a = new JSONArray(c);
|
|
468 j.append("stooge", "Joe DeRita");
|
|
469 j.append("stooge", "Shemp");
|
|
470 j.accumulate("stooges", "Curly");
|
|
471 j.accumulate("stooges", "Larry");
|
|
472 j.accumulate("stooges", "Moe");
|
|
473 j.accumulate("stoogearray", j.get("stooges"));
|
|
474 j.put("map", m);
|
|
475 j.put("collection", c);
|
|
476 j.put("array", a);
|
|
477 a.put(m);
|
|
478 a.put(c);
|
|
479 System.out.println(j.toString(4));
|
|
480
|
|
481 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; } } ";
|
|
482 j = new JSONObject(s);
|
|
483 System.out.println(j.toString(4));
|
|
484
|
|
485 s = " (\"San Francisco\", \"New York\", \"Seoul\", \"London\", \"Seattle\", \"Shanghai\")";
|
|
486 a = new JSONArray(s);
|
|
487 System.out.println(a.toString());
|
|
488
|
|
489 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>";
|
|
490 j = XML.toJSONObject(s);
|
|
491
|
|
492 System.out.println(j.toString(2));
|
|
493 System.out.println(XML.toString(j));
|
|
494 System.out.println("");
|
|
495 ja = JSONML.toJSONArray(s);
|
|
496 System.out.println(ja.toString(4));
|
|
497 System.out.println(JSONML.toString(ja));
|
|
498 System.out.println("");
|
|
499
|
|
500
|
|
501 System.out.println("\nTesting Exceptions: ");
|
|
502
|
|
503 System.out.print("Exception: ");
|
|
504 try {
|
|
505 a = new JSONArray();
|
|
506 a.put(Double.NEGATIVE_INFINITY);
|
|
507 a.put(Double.NaN);
|
|
508 System.out.println(a.toString());
|
|
509 } catch (Exception e) {
|
|
510 System.out.println(e);
|
|
511 }
|
|
512 System.out.print("Exception: ");
|
|
513 try {
|
|
514 System.out.println(j.getDouble("stooge"));
|
|
515 } catch (Exception e) {
|
|
516 System.out.println(e);
|
|
517 }
|
|
518 System.out.print("Exception: ");
|
|
519 try {
|
|
520 System.out.println(j.getDouble("howard"));
|
|
521 } catch (Exception e) {
|
|
522 System.out.println(e);
|
|
523 }
|
|
524 System.out.print("Exception: ");
|
|
525 try {
|
|
526 System.out.println(j.put(null, "howard"));
|
|
527 } catch (Exception e) {
|
|
528 System.out.println(e);
|
|
529 }
|
|
530 System.out.print("Exception: ");
|
|
531 try {
|
|
532 System.out.println(a.getDouble(0));
|
|
533 } catch (Exception e) {
|
|
534 System.out.println(e);
|
|
535 }
|
|
536 System.out.print("Exception: ");
|
|
537 try {
|
|
538 System.out.println(a.get(-1));
|
|
539 } catch (Exception e) {
|
|
540 System.out.println(e);
|
|
541 }
|
|
542 System.out.print("Exception: ");
|
|
543 try {
|
|
544 System.out.println(a.put(Double.NaN));
|
|
545 } catch (Exception e) {
|
|
546 System.out.println(e);
|
|
547 }
|
|
548 System.out.print("Exception: ");
|
|
549 try {
|
|
550 j = XML.toJSONObject("<a><b> ");
|
|
551 } catch (Exception e) {
|
|
552 System.out.println(e);
|
|
553 }
|
|
554 System.out.print("Exception: ");
|
|
555 try {
|
|
556 j = XML.toJSONObject("<a></b> ");
|
|
557 } catch (Exception e) {
|
|
558 System.out.println(e);
|
|
559 }
|
|
560 System.out.print("Exception: ");
|
|
561 try {
|
|
562 j = XML.toJSONObject("<a></a ");
|
|
563 } catch (Exception e) {
|
|
564 System.out.println(e);
|
|
565 }
|
|
566 System.out.print("Exception: ");
|
|
567 try {
|
|
568 ja = new JSONArray(new Object());
|
|
569 System.out.println(ja.toString());
|
|
570 } catch (Exception e) {
|
|
571 System.out.println(e);
|
|
572 }
|
|
573
|
|
574 System.out.print("Exception: ");
|
|
575 try {
|
|
576 s = "[)";
|
|
577 a = new JSONArray(s);
|
|
578 System.out.println(a.toString());
|
|
579 } catch (Exception e) {
|
|
580 System.out.println(e);
|
|
581 }
|
|
582
|
|
583 System.out.print("Exception: ");
|
|
584 try {
|
|
585 s = "<xml";
|
|
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 = "<right></wrong>";
|
|
595 ja = JSONML.toJSONArray(s);
|
|
596 System.out.println(ja.toString(4));
|
|
597 } catch (Exception e) {
|
|
598 System.out.println(e);
|
|
599 }
|
|
600
|
|
601 System.out.print("Exception: ");
|
|
602 try {
|
|
603 s = "{\"koda\": true, \"koda\": true}";
|
|
604 j = new JSONObject(s);
|
|
605 System.out.println(j.toString(4));
|
|
606 } catch (Exception e) {
|
|
607 System.out.println(e);
|
|
608 }
|
|
609
|
|
610 System.out.print("Exception: ");
|
|
611 try {
|
|
612 jj = new JSONStringer();
|
|
613 s = jj
|
|
614 .object()
|
|
615 .key("bosanda")
|
|
616 .value("MARIE HAA'S")
|
|
617 .key("bosanda")
|
|
618 .value("MARIE HAA\\'S")
|
|
619 .endObject()
|
|
620 .toString();
|
|
621 System.out.println(j.toString(4));
|
|
622 } catch (Exception e) {
|
|
623 System.out.println(e);
|
|
624 }
|
|
625 } catch (Exception e) {
|
|
626 System.out.println(e.toString());
|
|
627 }
|
|
628 }
|
|
629 }
|