001/*
002Below is the copyright agreement for the Ptolemy II system.
003
004Copyright (c) 2010-2011 The Regents of the University of California.
005All rights reserved.
006
007Permission is hereby granted, without written agreement and without
008license or royalty fees, to use, copy, modify, and distribute this
009software and its documentation for any purpose, provided that the above
010copyright notice and the following two paragraphs appear in all copies
011of this software.
012
013IN NO EVENT SHALL THE UNIVERSITY OF CALIFORNIA BE LIABLE TO ANY PARTY
014FOR DIRECT, INDIRECT, SPECIAL, INCIDENTAL, OR CONSEQUENTIAL DAMAGES
015ARISING OUT OF THE USE OF THIS SOFTWARE AND ITS DOCUMENTATION, EVEN IF
016THE UNIVERSITY OF CALIFORNIA HAS BEEN ADVISED OF THE POSSIBILITY OF
017SUCH DAMAGE.
018
019THE UNIVERSITY OF CALIFORNIA SPECIFICALLY DISCLAIMS ANY WARRANTIES,
020INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
021MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. THE SOFTWARE
022PROVIDED HEREUNDER IS ON AN "AS IS" BASIS, AND THE UNIVERSITY OF
023CALIFORNIA HAS NO OBLIGATION TO PROVIDE MAINTENANCE, SUPPORT, UPDATES,
024ENHANCEMENTS, OR MODIFICATIONS.
025
026Ptolemy II includes the work of others, to see those copyrights, follow
027the copyright link on the splash page or see copyright.htm.
028 */
029package org.json;
030
031import java.io.StringWriter;
032import java.util.Collection;
033import java.util.Iterator;
034import java.util.Map;
035
036/**
037 * Test class. This file is not formally a member of the org.json library.
038 * It is just a casual test tool.
039 */
040public class Test {
041
042    /**
043     * Entry point.
044     * @param args
045     */
046    public static void main(String args[]) {
047        Iterator it;
048        JSONArray a;
049        JSONObject j;
050        JSONStringer jj;
051        Object o;
052        String s;
053
054        /**
055         *  Obj is a typical class that implements JSONString. It also
056         *  provides some beanie methods that can be used to
057         *  construct a JSONObject. It also demonstrates constructing
058         *  a JSONObject with an array of names.
059         */
060        class Obj implements JSONString {
061            public String aString;
062            public double aNumber;
063            public boolean aBoolean;
064
065            public Obj(String string, double n, boolean b) {
066                this.aString = string;
067                this.aNumber = n;
068                this.aBoolean = b;
069            }
070
071            public double getNumber() {
072                return this.aNumber;
073            }
074
075            public String getString() {
076                return this.aString;
077            }
078
079            public boolean isBoolean() {
080                return this.aBoolean;
081            }
082
083            public String getBENT() {
084                return "All uppercase key";
085            }
086
087            public String getX() {
088                return "x";
089            }
090
091            @Override
092            public String toJSONString() {
093                return "{" + JSONObject.quote(this.aString) + ":"
094                        + JSONObject.doubleToString(this.aNumber) + "}";
095            }
096
097            @Override
098            public String toString() {
099                return this.getString() + " " + this.getNumber() + " "
100                        + this.isBoolean() + "." + this.getBENT() + " "
101                        + this.getX();
102            }
103        }
104
105        Obj obj = new Obj("A beany object", 42, true);
106
107        try {
108            s = "[0.1]";
109            a = new JSONArray(s);
110            System.out.println(a.toString());
111            System.out.println("");
112
113            j = XML.toJSONObject(
114                    "<![CDATA[This is a collection of test patterns and examples for org.json.]]>  Ignore the stuff past the end.  ");
115            System.out.println(j.toString());
116            System.out.println("");
117
118            j = new JSONObject();
119            o = null;
120            j.put("booga", o);
121            j.put("wooga", JSONObject.NULL);
122            System.out.println(j.toString());
123            System.out.println("");
124
125            j = new JSONObject();
126            j.increment("two");
127            j.increment("two");
128            System.out.println(j.toString());
129            System.out.println("");
130
131            s = "<test><blank></blank><empty/></test>";
132            j = XML.toJSONObject(s);
133            System.out.println(j.toString(2));
134            System.out.println(XML.toString(j));
135            System.out.println("");
136
137            s = "{     \"list of lists\" : [         [1, 2, 3],         [4, 5, 6],     ] }";
138            j = new JSONObject(s);
139            System.out.println(j.toString(4));
140            System.out.println(XML.toString(j));
141
142            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> ";
143            j = XML.toJSONObject(s);
144            System.out.println(j.toString(4));
145            System.out.println();
146
147            j = JSONML.toJSONObject(s);
148            System.out.println(j.toString());
149            System.out.println(JSONML.toString(j));
150            System.out.println();
151
152            a = JSONML.toJSONArray(s);
153            System.out.println(a.toString(4));
154            System.out.println(JSONML.toString(a));
155            System.out.println();
156
157            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>";
158            j = JSONML.toJSONObject(s);
159            System.out.println(j.toString(4));
160            System.out.println(JSONML.toString(j));
161            System.out.println();
162
163            a = JSONML.toJSONArray(s);
164            System.out.println(a.toString(4));
165            System.out.println(JSONML.toString(a));
166            System.out.println();
167
168            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>";
169            j = XML.toJSONObject(s);
170            System.out.println(j.toString(4));
171
172            j = new JSONObject(obj);
173            System.out.println(j.toString());
174
175            s = "{ \"entity\": { \"imageURL\": \"\", \"name\": \"IXXXXXXXXXXXXX\", \"id\": 12336, \"ratingCount\": null, \"averageRating\": null } }";
176            j = new JSONObject(s);
177            System.out.println(j.toString(2));
178
179            jj = new JSONStringer();
180            s = jj.object().key("single").value("MARIE HAA'S").key("Johnny")
181                    .value("MARIE HAA\\'S").key("foo").value("bar").key("baz")
182                    .array().object().key("quux").value("Thanks, Josh!")
183                    .endObject().endArray().key("obj keys")
184                    .value(JSONObject.getNames(obj)).endObject().toString();
185            System.out.println(s);
186
187            System.out.println(new JSONStringer().object().key("a").array()
188                    .array().array().value("b").endArray().endArray().endArray()
189                    .endObject().toString());
190
191            jj = new JSONStringer();
192            jj.array();
193            jj.value(1);
194            jj.array();
195            jj.value(null);
196            jj.array();
197            jj.object();
198            jj.key("empty-array").array().endArray();
199            jj.key("answer").value(42);
200            jj.key("null").value(null);
201            jj.key("false").value(false);
202            jj.key("true").value(true);
203            jj.key("big").value(123456789e+88);
204            jj.key("small").value(123456789e-88);
205            jj.key("empty-object").object().endObject();
206            jj.key("long");
207            jj.value(9223372036854775807L);
208            jj.endObject();
209            jj.value("two");
210            jj.endArray();
211            jj.value(true);
212            jj.endArray();
213            jj.value(98.6);
214            jj.value(-100.0);
215            jj.object();
216            jj.endObject();
217            jj.object();
218            jj.key("one");
219            jj.value(1.00);
220            jj.endObject();
221            jj.value(obj);
222            jj.endArray();
223            System.out.println(jj.toString());
224
225            System.out.println(new JSONArray(jj.toString()).toString(4));
226
227            int ar[] = { 1, 2, 3 };
228            JSONArray ja = new JSONArray(ar);
229            System.out.println(ja.toString());
230
231            String sa[] = { "aString", "aNumber", "aBoolean" };
232            j = new JSONObject(obj, sa);
233            j.put("Testing JSONString interface", obj);
234            System.out.println(j.toString(4));
235
236            j = new JSONObject(
237                    "{slashes: '///', closetag: '</script>', backslash:'\\\\', ei: {quotes: '\"\\''},eo: {a: '\"quoted\"', b:\"don't\"}, quotes: [\"'\", '\"']}");
238            System.out.println(j.toString(2));
239            System.out.println(XML.toString(j));
240            System.out.println("");
241
242            j = new JSONObject(
243                    "{foo: [true, false,9876543210,    0.0, 1.00000001,  1.000000000001, 1.00000000000000001,"
244                            + " .00000000000000001, 2.00, 0.1, 2e100, -32,[],{}, \"string\"], "
245                            + "  to   : null, op : 'Good',"
246                            + "ten:10} postfix comment");
247            j.put("String", "98.6");
248            j.put("JSONObject", new JSONObject());
249            j.put("JSONArray", new JSONArray());
250            j.put("int", 57);
251            j.put("double", 123456789012345678901234567890.);
252            j.put("true", true);
253            j.put("false", false);
254            j.put("null", JSONObject.NULL);
255            j.put("bool", "true");
256            j.put("zero", -0.0);
257            j.put("\\u2028", "\u2028");
258            j.put("\\u2029", "\u2029");
259            a = j.getJSONArray("foo");
260            a.put(666);
261            a.put(2001.99);
262            a.put("so \"fine\".");
263            a.put("so <fine>.");
264            a.put(true);
265            a.put(false);
266            a.put(new JSONArray());
267            a.put(new JSONObject());
268            j.put("keys", JSONObject.getNames(j));
269            System.out.println(j.toString(4));
270            System.out.println(XML.toString(j));
271
272            System.out.println("String: " + j.getDouble("String"));
273            System.out.println("  bool: " + j.getBoolean("bool"));
274            System.out.println("    to: " + j.getString("to"));
275            System.out.println("  true: " + j.getString("true"));
276            System.out.println("   foo: " + j.getJSONArray("foo"));
277            System.out.println("    op: " + j.getString("op"));
278            System.out.println("   ten: " + j.getInt("ten"));
279            System.out.println("  oops: " + j.optBoolean("oops"));
280
281            s = "<xml one = 1 two=' \"2\" '><five></five>First \u0009&lt;content&gt;<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>";
282            j = XML.toJSONObject(s);
283            System.out.println(j.toString(2));
284            System.out.println(XML.toString(j));
285            System.out.println("");
286
287            ja = JSONML.toJSONArray(s);
288            System.out.println(ja.toString(4));
289            System.out.println(JSONML.toString(ja));
290            System.out.println("");
291
292            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>";
293            ja = JSONML.toJSONArray(s);
294            System.out.println(ja.toString(4));
295            System.out.println(JSONML.toString(ja));
296            System.out.println("");
297
298            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>";
299            j = XML.toJSONObject(s);
300
301            System.out.println(j.toString(2));
302            System.out.println(XML.toString(j));
303            System.out.println("");
304            ja = JSONML.toJSONArray(s);
305            System.out.println(ja.toString(4));
306            System.out.println(JSONML.toString(ja));
307            System.out.println("");
308
309            j = XML.toJSONObject(
310                    "<?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>");
311            System.out.println(j.toString(2));
312            System.out.println(XML.toString(j));
313            System.out.println("");
314
315            j = XML.toJSONObject(
316                    "<!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>");
317            System.out.println(j.toString(2));
318            System.out.println(XML.toString(j));
319            System.out.println("");
320
321            j = XML.toJSONObject(
322                    "<?xml version=\"1.0\"?><customer>    <firstName>        <text>Fred</text>    </firstName>    <ID>fbs0001</ID>    <lastName> <text>Scerbo</text>    </lastName>    <MI>        <text>B</text>    </MI></customer>");
323            System.out.println(j.toString(2));
324            System.out.println(XML.toString(j));
325            System.out.println("");
326
327            j = XML.toJSONObject(
328                    "<!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>");
329            System.out.println(j.toString());
330            System.out.println(XML.toString(j));
331            System.out.println("");
332
333            j = XML.toJSONObject(
334                    "<test intertag status=ok><empty/>deluxe<blip sweet=true>&amp;&quot;toot&quot;&toot;&#x41;</blip><x>eks</x><w>bonus</w><w>bonus2</w></test>");
335            System.out.println(j.toString(2));
336            System.out.println(XML.toString(j));
337            System.out.println("");
338
339            j = HTTP.toJSONObject(
340                    "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");
341            System.out.println(j.toString(2));
342            System.out.println(HTTP.toString(j));
343            System.out.println("");
344
345            j = HTTP.toJSONObject(
346                    "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");
347            System.out.println(j.toString(2));
348            System.out.println(HTTP.toString(j));
349            System.out.println("");
350
351            j = new JSONObject(
352                    "{nix: null, nux: false, null: 'null', 'Request-URI': '/', Method: 'GET', 'HTTP-Version': 'HTTP/1.0'}");
353            System.out.println(j.toString(2));
354            System.out.println("isNull: " + j.isNull("nix"));
355            System.out.println("   has: " + j.has("nix"));
356            System.out.println(XML.toString(j));
357            System.out.println(HTTP.toString(j));
358            System.out.println("");
359
360            j = XML.toJSONObject("<?xml version='1.0' encoding='UTF-8'?>"
361                    + "\n\n" + "<SOAP-ENV:Envelope"
362                    + " xmlns:SOAP-ENV=\"http://schemas.xmlsoap.org/soap/envelope/\""
363                    + " xmlns:xsi=\"http://www.w3.org/1999/XMLSchema-instance\""
364                    + " xmlns:xsd=\"http://www.w3.org/1999/XMLSchema\">"
365                    + "<SOAP-ENV:Body><ns1:doGoogleSearch"
366                    + " xmlns:ns1=\"urn:GoogleSearch\""
367                    + " SOAP-ENV:encodingStyle=\"http://schemas.xmlsoap.org/soap/encoding/\">"
368                    + "<key xsi:type=\"xsd:string\">GOOGLEKEY</key> <q"
369                    + " xsi:type=\"xsd:string\">'+search+'</q> <start"
370                    + " xsi:type=\"xsd:int\">0</start> <maxResults"
371                    + " xsi:type=\"xsd:int\">10</maxResults> <filter"
372                    + " xsi:type=\"xsd:boolean\">true</filter> <restrict"
373                    + " xsi:type=\"xsd:string\"></restrict> <safeSearch"
374                    + " xsi:type=\"xsd:boolean\">false</safeSearch> <lr"
375                    + " xsi:type=\"xsd:string\"></lr> <ie"
376                    + " xsi:type=\"xsd:string\">latin1</ie> <oe"
377                    + " xsi:type=\"xsd:string\">latin1</oe>"
378                    + "</ns1:doGoogleSearch>"
379                    + "</SOAP-ENV:Body></SOAP-ENV:Envelope>");
380            System.out.println(j.toString(2));
381            System.out.println(XML.toString(j));
382            System.out.println("");
383
384            j = new JSONObject(
385                    "{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\"}}}}");
386            System.out.println(j.toString(2));
387            System.out.println(XML.toString(j));
388            System.out.println("");
389
390            j = CookieList.toJSONObject("  f%oo = b+l=ah  ; o;n%40e = t.wo ");
391            System.out.println(j.toString(2));
392            System.out.println(CookieList.toString(j));
393            System.out.println("");
394
395            j = Cookie.toJSONObject(
396                    "f%oo=blah; secure ;expires = April 24, 2002");
397            System.out.println(j.toString(2));
398            System.out.println(Cookie.toString(j));
399            System.out.println("");
400
401            j = new JSONObject(
402                    "{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 /'}");
403            System.out.println(j.toString());
404            System.out.println("");
405
406            JSONTokener jt = new JSONTokener(
407                    "{op:'test', to:'session', pre:1}{op:'test', to:'session', pre:2}");
408            j = new JSONObject(jt);
409            System.out.println(j.toString());
410            System.out.println("pre: " + j.optInt("pre"));
411            int i = jt.skipTo('{');
412            System.out.println(i);
413            j = new JSONObject(jt);
414            System.out.println(j.toString());
415            System.out.println("");
416
417            a = CDL.toJSONArray(
418                    "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");
419
420            s = CDL.toString(a);
421            System.out.println(s);
422            System.out.println("");
423            System.out.println(a.toString(4));
424            System.out.println("");
425            a = CDL.toJSONArray(s);
426            System.out.println(a.toString(4));
427            System.out.println("");
428
429            a = new JSONArray(
430                    " [\"<escape>\", next is an implied null , , ok,] ");
431            System.out.println(a.toString());
432            System.out.println("");
433            System.out.println(XML.toString(a));
434            System.out.println("");
435
436            j = new JSONObject(
437                    "{ 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}");
438            System.out.println(j.toString(4));
439            System.out.println("");
440            if (j.getBoolean("true") && !j.getBoolean("false")) {
441                System.out.println("It's all good");
442            }
443
444            System.out.println("");
445            j = new JSONObject(j,
446                    new String[] { "dec", "oct", "hex", "missing" });
447            System.out.println(j.toString(4));
448
449            System.out.println("");
450            System.out.println(
451                    new JSONStringer().array().value(a).value(j).endArray());
452
453            j = new JSONObject(
454                    "{string: \"98.6\", long: 2147483648, int: 2147483647, longer: 9223372036854775807, double: 9223372036854775808}");
455            System.out.println(j.toString(4));
456
457            System.out.println("\ngetInt");
458            System.out.println("int    " + j.getInt("int"));
459            System.out.println("long   " + j.getInt("long"));
460            System.out.println("longer " + j.getInt("longer"));
461            //System.out.println("double " + j.getInt("double"));
462            //System.out.println("string " + j.getInt("string"));
463
464            System.out.println("\ngetLong");
465            System.out.println("int    " + j.getLong("int"));
466            System.out.println("long   " + j.getLong("long"));
467            System.out.println("longer " + j.getLong("longer"));
468            //System.out.println("double " + j.getLong("double"));
469            //System.out.println("string " + j.getLong("string"));
470
471            System.out.println("\ngetDouble");
472            System.out.println("int    " + j.getDouble("int"));
473            System.out.println("long   " + j.getDouble("long"));
474            System.out.println("longer " + j.getDouble("longer"));
475            System.out.println("double " + j.getDouble("double"));
476            System.out.println("string " + j.getDouble("string"));
477
478            j.put("good sized", 9223372036854775807L);
479            System.out.println(j.toString(4));
480
481            a = new JSONArray(
482                    "[2147483647, 2147483648, 9223372036854775807, 9223372036854775808]");
483            System.out.println(a.toString(4));
484
485            System.out.println("\nKeys: ");
486            it = j.keys();
487            while (it.hasNext()) {
488                s = (String) it.next();
489                System.out.println(s + ": " + j.getString(s));
490            }
491
492            System.out.println("\naccumulate: ");
493            j = new JSONObject();
494            j.accumulate("stooge", "Curly");
495            j.accumulate("stooge", "Larry");
496            j.accumulate("stooge", "Moe");
497            a = j.getJSONArray("stooge");
498            a.put(5, "Shemp");
499            System.out.println(j.toString(4));
500
501            System.out.println("\nwrite:");
502            System.out.println(j.write(new StringWriter()));
503
504            s = "<xml empty><a></a><a>1</a><a>22</a><a>333</a></xml>";
505            j = XML.toJSONObject(s);
506            System.out.println(j.toString(4));
507            System.out.println(XML.toString(j));
508
509            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>";
510            j = XML.toJSONObject(s);
511            System.out.println(j.toString(4));
512            System.out.println(XML.toString(j));
513
514            a = JSONML.toJSONArray(s);
515            System.out.println(a.toString(4));
516            System.out.println(JSONML.toString(a));
517
518            Collection c = null;
519            Map m = null;
520
521            j = new JSONObject(m);
522            a = new JSONArray(c);
523            j.append("stooge", "Joe DeRita");
524            j.append("stooge", "Shemp");
525            j.accumulate("stooges", "Curly");
526            j.accumulate("stooges", "Larry");
527            j.accumulate("stooges", "Moe");
528            j.accumulate("stoogearray", j.get("stooges"));
529            j.put("map", m);
530            j.put("collection", c);
531            j.put("array", a);
532            a.put(m);
533            a.put(c);
534            System.out.println(j.toString(4));
535
536            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; } } ";
537            j = new JSONObject(s);
538            System.out.println(j.toString(4));
539
540            s = " (\"San Francisco\", \"New York\", \"Seoul\", \"London\", \"Seattle\", \"Shanghai\")";
541            a = new JSONArray(s);
542            System.out.println(a.toString());
543
544            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>";
545            j = XML.toJSONObject(s);
546
547            System.out.println(j.toString(2));
548            System.out.println(XML.toString(j));
549            System.out.println("");
550            ja = JSONML.toJSONArray(s);
551            System.out.println(ja.toString(4));
552            System.out.println(JSONML.toString(ja));
553            System.out.println("");
554
555            s = "<Root><MsgType type=\"node\"><BatchType type=\"string\">111111111111111</BatchType></MsgType></Root>";
556            j = JSONML.toJSONObject(s);
557            System.out.println(j);
558            ja = JSONML.toJSONArray(s);
559            System.out.println(ja);
560
561            System.out.println("\nTesting Exceptions: ");
562
563            System.out.print("Exception: ");
564            try {
565                a = new JSONArray("[\n\r\n\r}");
566                System.out.println(a.toString());
567            } catch (Exception e) {
568                System.out.println(e);
569            }
570
571            System.out.print("Exception: ");
572            try {
573                a = new JSONArray("<\n\r\n\r      ");
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                a = new JSONArray();
582                a.put(Double.NEGATIVE_INFINITY);
583                a.put(Double.NaN);
584                System.out.println(a.toString());
585            } catch (Exception e) {
586                System.out.println(e);
587            }
588            System.out.print("Exception: ");
589            try {
590                System.out.println(j.getDouble("stooge"));
591            } catch (Exception e) {
592                System.out.println(e);
593            }
594            System.out.print("Exception: ");
595            try {
596                System.out.println(j.getDouble("howard"));
597            } catch (Exception e) {
598                System.out.println(e);
599            }
600            System.out.print("Exception: ");
601            try {
602                System.out.println(j.put(null, "howard"));
603            } catch (Exception e) {
604                System.out.println(e);
605            }
606            System.out.print("Exception: ");
607            try {
608                System.out.println(a.getDouble(0));
609            } catch (Exception e) {
610                System.out.println(e);
611            }
612            System.out.print("Exception: ");
613            try {
614                System.out.println(a.get(-1));
615            } catch (Exception e) {
616                System.out.println(e);
617            }
618            System.out.print("Exception: ");
619            try {
620                System.out.println(a.put(Double.NaN));
621            } catch (Exception e) {
622                System.out.println(e);
623            }
624            System.out.print("Exception: ");
625            try {
626                j = XML.toJSONObject("<a><b>    ");
627            } catch (Exception e) {
628                System.out.println(e);
629            }
630            System.out.print("Exception: ");
631            try {
632                j = XML.toJSONObject("<a></b>    ");
633            } catch (Exception e) {
634                System.out.println(e);
635            }
636            System.out.print("Exception: ");
637            try {
638                j = XML.toJSONObject("<a></a    ");
639            } catch (Exception e) {
640                System.out.println(e);
641            }
642            System.out.print("Exception: ");
643            try {
644                ja = new JSONArray(new Object());
645                System.out.println(ja.toString());
646            } catch (Exception e) {
647                System.out.println(e);
648            }
649
650            System.out.print("Exception: ");
651            try {
652                s = "[)";
653                a = new JSONArray(s);
654                System.out.println(a.toString());
655            } catch (Exception e) {
656                System.out.println(e);
657            }
658
659            System.out.print("Exception: ");
660            try {
661                s = "<xml";
662                ja = JSONML.toJSONArray(s);
663                System.out.println(ja.toString(4));
664            } catch (Exception e) {
665                System.out.println(e);
666            }
667
668            System.out.print("Exception: ");
669            try {
670                s = "<right></wrong>";
671                ja = JSONML.toJSONArray(s);
672                System.out.println(ja.toString(4));
673            } catch (Exception e) {
674                System.out.println(e);
675            }
676
677            System.out.print("Exception: ");
678            try {
679                s = "{\"koda\": true, \"koda\": true}";
680                j = new JSONObject(s);
681                System.out.println(j.toString(4));
682            } catch (Exception e) {
683                System.out.println(e);
684            }
685
686            System.out.print("Exception: ");
687            try {
688                jj = new JSONStringer();
689                s = jj.object().key("bosanda").value("MARIE HAA'S")
690                        .key("bosanda").value("MARIE HAA\\'S").endObject()
691                        .toString();
692                System.out.println(j.toString(4));
693            } catch (Exception e) {
694                System.out.println(e);
695            }
696        } catch (Exception e) {
697            System.out.println(e.toString());
698        }
699    }
700}