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("No quotes, 'Single Quotes', \"Double Quotes\"\n1,'2',\"3\"\n,'It is \"good,\"', \"It works.\"\n\n"); |
---|
369 | |
---|
370 | System.out.println(CDL.toString(a)); |
---|
371 | System.out.println(""); |
---|
372 | System.out.println(a.toString(4)); |
---|
373 | System.out.println(""); |
---|
374 | |
---|
375 | a = new JSONArray(" [\"<escape>\", next is an implied null , , ok,] "); |
---|
376 | System.out.println(a.toString()); |
---|
377 | System.out.println(""); |
---|
378 | System.out.println(XML.toString(a)); |
---|
379 | System.out.println(""); |
---|
380 | |
---|
381 | 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}"); |
---|
382 | System.out.println(j.toString(4)); |
---|
383 | System.out.println(""); |
---|
384 | if (j.getBoolean("true") && !j.getBoolean("false")) { |
---|
385 | System.out.println("It's all good"); |
---|
386 | } |
---|
387 | |
---|
388 | System.out.println(""); |
---|
389 | j = new JSONObject(j, new String[]{"dec", "oct", "hex", "missing"}); |
---|
390 | System.out.println(j.toString(4)); |
---|
391 | |
---|
392 | System.out.println(""); |
---|
393 | System.out.println(new JSONStringer().array().value(a).value(j).endArray()); |
---|
394 | |
---|
395 | j = new JSONObject("{string: \"98.6\", long: 2147483648, int: 2147483647, longer: 9223372036854775807, double: 9223372036854775808}"); |
---|
396 | System.out.println(j.toString(4)); |
---|
397 | |
---|
398 | System.out.println("\ngetInt"); |
---|
399 | System.out.println("int " + j.getInt("int")); |
---|
400 | System.out.println("long " + j.getInt("long")); |
---|
401 | System.out.println("longer " + j.getInt("longer")); |
---|
402 | System.out.println("double " + j.getInt("double")); |
---|
403 | System.out.println("string " + j.getInt("string")); |
---|
404 | |
---|
405 | System.out.println("\ngetLong"); |
---|
406 | System.out.println("int " + j.getLong("int")); |
---|
407 | System.out.println("long " + j.getLong("long")); |
---|
408 | System.out.println("longer " + j.getLong("longer")); |
---|
409 | System.out.println("double " + j.getLong("double")); |
---|
410 | System.out.println("string " + j.getLong("string")); |
---|
411 | |
---|
412 | System.out.println("\ngetDouble"); |
---|
413 | System.out.println("int " + j.getDouble("int")); |
---|
414 | System.out.println("long " + j.getDouble("long")); |
---|
415 | System.out.println("longer " + j.getDouble("longer")); |
---|
416 | System.out.println("double " + j.getDouble("double")); |
---|
417 | System.out.println("string " + j.getDouble("string")); |
---|
418 | |
---|
419 | j.put("good sized", 9223372036854775807L); |
---|
420 | System.out.println(j.toString(4)); |
---|
421 | |
---|
422 | a = new JSONArray("[2147483647, 2147483648, 9223372036854775807, 9223372036854775808]"); |
---|
423 | System.out.println(a.toString(4)); |
---|
424 | |
---|
425 | System.out.println("\nKeys: "); |
---|
426 | it = j.keys(); |
---|
427 | while (it.hasNext()) { |
---|
428 | s = (String)it.next(); |
---|
429 | System.out.println(s + ": " + j.getString(s)); |
---|
430 | } |
---|
431 | |
---|
432 | |
---|
433 | System.out.println("\naccumulate: "); |
---|
434 | j = new JSONObject(); |
---|
435 | j.accumulate("stooge", "Curly"); |
---|
436 | j.accumulate("stooge", "Larry"); |
---|
437 | j.accumulate("stooge", "Moe"); |
---|
438 | a = j.getJSONArray("stooge"); |
---|
439 | a.put(5, "Shemp"); |
---|
440 | System.out.println(j.toString(4)); |
---|
441 | |
---|
442 | System.out.println("\nwrite:"); |
---|
443 | System.out.println(j.write(new StringWriter())); |
---|
444 | |
---|
445 | s = "<xml empty><a></a><a>1</a><a>22</a><a>333</a></xml>"; |
---|
446 | j = XML.toJSONObject(s); |
---|
447 | System.out.println(j.toString(4)); |
---|
448 | System.out.println(XML.toString(j)); |
---|
449 | |
---|
450 | 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>"; |
---|
451 | j = XML.toJSONObject(s); |
---|
452 | System.out.println(j.toString(4)); |
---|
453 | System.out.println(XML.toString(j)); |
---|
454 | |
---|
455 | a = JSONML.toJSONArray(s); |
---|
456 | System.out.println(a.toString(4)); |
---|
457 | System.out.println(JSONML.toString(a)); |
---|
458 | |
---|
459 | Collection c = null; |
---|
460 | Map m = null; |
---|
461 | |
---|
462 | j = new JSONObject(m); |
---|
463 | a = new JSONArray(c); |
---|
464 | j.append("stooge", "Joe DeRita"); |
---|
465 | j.append("stooge", "Shemp"); |
---|
466 | j.accumulate("stooges", "Curly"); |
---|
467 | j.accumulate("stooges", "Larry"); |
---|
468 | j.accumulate("stooges", "Moe"); |
---|
469 | j.accumulate("stoogearray", j.get("stooges")); |
---|
470 | j.put("map", m); |
---|
471 | j.put("collection", c); |
---|
472 | j.put("array", a); |
---|
473 | a.put(m); |
---|
474 | a.put(c); |
---|
475 | System.out.println(j.toString(4)); |
---|
476 | |
---|
477 | 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; } } "; |
---|
478 | j = new JSONObject(s); |
---|
479 | System.out.println(j.toString(4)); |
---|
480 | |
---|
481 | s = " (\"San Francisco\", \"New York\", \"Seoul\", \"London\", \"Seattle\", \"Shanghai\")"; |
---|
482 | a = new JSONArray(s); |
---|
483 | System.out.println(a.toString()); |
---|
484 | |
---|
485 | 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>"; |
---|
486 | j = XML.toJSONObject(s); |
---|
487 | |
---|
488 | System.out.println(j.toString(2)); |
---|
489 | System.out.println(XML.toString(j)); |
---|
490 | System.out.println(""); |
---|
491 | ja = JSONML.toJSONArray(s); |
---|
492 | System.out.println(ja.toString(4)); |
---|
493 | System.out.println(JSONML.toString(ja)); |
---|
494 | System.out.println(""); |
---|
495 | |
---|
496 | |
---|
497 | System.out.println("\nTesting Exceptions: "); |
---|
498 | |
---|
499 | System.out.print("Exception: "); |
---|
500 | try { |
---|
501 | a = new JSONArray(); |
---|
502 | a.put(Double.NEGATIVE_INFINITY); |
---|
503 | a.put(Double.NaN); |
---|
504 | System.out.println(a.toString()); |
---|
505 | } catch (Exception e) { |
---|
506 | System.out.println(e); |
---|
507 | } |
---|
508 | System.out.print("Exception: "); |
---|
509 | try { |
---|
510 | System.out.println(j.getDouble("stooge")); |
---|
511 | } catch (Exception e) { |
---|
512 | System.out.println(e); |
---|
513 | } |
---|
514 | System.out.print("Exception: "); |
---|
515 | try { |
---|
516 | System.out.println(j.getDouble("howard")); |
---|
517 | } catch (Exception e) { |
---|
518 | System.out.println(e); |
---|
519 | } |
---|
520 | System.out.print("Exception: "); |
---|
521 | try { |
---|
522 | System.out.println(j.put(null, "howard")); |
---|
523 | } catch (Exception e) { |
---|
524 | System.out.println(e); |
---|
525 | } |
---|
526 | System.out.print("Exception: "); |
---|
527 | try { |
---|
528 | System.out.println(a.getDouble(0)); |
---|
529 | } catch (Exception e) { |
---|
530 | System.out.println(e); |
---|
531 | } |
---|
532 | System.out.print("Exception: "); |
---|
533 | try { |
---|
534 | System.out.println(a.get(-1)); |
---|
535 | } catch (Exception e) { |
---|
536 | System.out.println(e); |
---|
537 | } |
---|
538 | System.out.print("Exception: "); |
---|
539 | try { |
---|
540 | System.out.println(a.put(Double.NaN)); |
---|
541 | } catch (Exception e) { |
---|
542 | System.out.println(e); |
---|
543 | } |
---|
544 | System.out.print("Exception: "); |
---|
545 | try { |
---|
546 | j = XML.toJSONObject("<a><b> "); |
---|
547 | } catch (Exception e) { |
---|
548 | System.out.println(e); |
---|
549 | } |
---|
550 | System.out.print("Exception: "); |
---|
551 | try { |
---|
552 | j = XML.toJSONObject("<a></b> "); |
---|
553 | } catch (Exception e) { |
---|
554 | System.out.println(e); |
---|
555 | } |
---|
556 | System.out.print("Exception: "); |
---|
557 | try { |
---|
558 | j = XML.toJSONObject("<a></a "); |
---|
559 | } catch (Exception e) { |
---|
560 | System.out.println(e); |
---|
561 | } |
---|
562 | System.out.print("Exception: "); |
---|
563 | try { |
---|
564 | ja = new JSONArray(new Object()); |
---|
565 | System.out.println(ja.toString()); |
---|
566 | } catch (Exception e) { |
---|
567 | System.out.println(e); |
---|
568 | } |
---|
569 | |
---|
570 | System.out.print("Exception: "); |
---|
571 | try { |
---|
572 | s = "[)"; |
---|
573 | a = new JSONArray(s); |
---|
574 | System.out.println(a.toString()); |
---|
575 | } catch (Exception e) { |
---|
576 | System.out.println(e); |
---|
577 | } |
---|
578 | |
---|
579 | System.out.print("Exception: "); |
---|
580 | try { |
---|
581 | s = "<xml"; |
---|
582 | ja = JSONML.toJSONArray(s); |
---|
583 | System.out.println(ja.toString(4)); |
---|
584 | } catch (Exception e) { |
---|
585 | System.out.println(e); |
---|
586 | } |
---|
587 | |
---|
588 | System.out.print("Exception: "); |
---|
589 | try { |
---|
590 | s = "<right></wrong>"; |
---|
591 | ja = JSONML.toJSONArray(s); |
---|
592 | System.out.println(ja.toString(4)); |
---|
593 | } catch (Exception e) { |
---|
594 | System.out.println(e); |
---|
595 | } |
---|
596 | |
---|
597 | System.out.print("Exception: "); |
---|
598 | try { |
---|
599 | s = "{\"koda\": true, \"koda\": true}"; |
---|
600 | j = new JSONObject(s); |
---|
601 | System.out.println(j.toString(4)); |
---|
602 | } catch (Exception e) { |
---|
603 | System.out.println(e); |
---|
604 | } |
---|
605 | |
---|
606 | System.out.print("Exception: "); |
---|
607 | try { |
---|
608 | jj = new JSONStringer(); |
---|
609 | s = jj |
---|
610 | .object() |
---|
611 | .key("bosanda") |
---|
612 | .value("MARIE HAA'S") |
---|
613 | .key("bosanda") |
---|
614 | .value("MARIE HAA\\'S") |
---|
615 | .endObject() |
---|
616 | .toString(); |
---|
617 | System.out.println(j.toString(4)); |
---|
618 | } catch (Exception e) { |
---|
619 | System.out.println(e); |
---|
620 | } |
---|
621 | } catch (Exception e) { |
---|
622 | System.out.println(e.toString()); |
---|
623 | } |
---|
624 | } |
---|
625 | } |
---|