Statistics
| Revision:

root / org.gvsig.toolbox / trunk / org.gvsig.toolbox / org.gvsig.toolbox.algorithm / src / main / java / org / json / Test.java @ 59

History | View | Annotate | Download (19.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. It is just a casual test tool.
10
 */
11
public class Test {
12

    
13
   /**
14
    * Entry point.
15
    *
16
    * @param args
17
    */
18
   public static void main(final String args[]) {
19
      Iterator it;
20
      JSONArray a;
21
      JSONObject j;
22
      JSONStringer jj;
23
      String s;
24

    
25
      // Obj is a typical class that implements JSONString.
26

    
27
      class Obj
28
               implements
29
                  JSONString {
30
         public String  aString;
31
         public double  aNumber;
32
         public boolean aBoolean;
33

    
34

    
35
         public Obj(final String string,
36
                    final double n,
37
                    final boolean b) {
38
            this.aString = string;
39
            this.aNumber = n;
40
            this.aBoolean = b;
41
         }
42

    
43

    
44
         public String toJSONString() {
45
            return "{" + JSONObject.quote(this.aString) + ":" + JSONObject.doubleToString(this.aNumber) + "}";
46
         }
47

    
48

    
49
         @Override
50
         public String toString() {
51
            return this.aString + " " + this.aNumber + " " + this.aBoolean;
52
         }
53
      }
54

    
55

    
56
      try {
57
         final String sa[] = { "aString", "aNumber", "aBoolean" };
58
         final Obj obj = new Obj("A string, a number, and a boolean", 42, true);
59

    
60
         j = XML.toJSONObject("<![CDATA[This is a collection of test patterns and examples for org.json.]]>  Ignore the stuff past the end.  ");
61
         System.out.println(j.toString());
62

    
63
         jj = new JSONStringer();
64
         s = jj.object().key("foo").value("bar").key("baz").array().object().key("quux").value("Thanks, Josh!").endObject().endArray().endObject().toString();
65
         System.out.println(s);
66

    
67
         System.out.println(new JSONStringer().object().key("a").array().array().array().value("b").endArray().endArray().endArray().endObject().toString());
68

    
69
         jj = new JSONStringer();
70
         jj.array();
71
         jj.value(1);
72
         jj.array();
73
         jj.value(null);
74
         jj.array();
75
         jj.object();
76
         jj.key("empty-array").array().endArray();
77
         jj.key("answer").value(42);
78
         jj.key("null").value(null);
79
         jj.key("false").value(false);
80
         jj.key("true").value(true);
81
         jj.key("big").value(123456789e+88);
82
         jj.key("small").value(123456789e-88);
83
         jj.key("empty-object").object().endObject();
84
         jj.key("long");
85
         jj.value(9223372036854775807L);
86
         jj.endObject();
87
         jj.value("two");
88
         jj.endArray();
89
         jj.value(true);
90
         jj.endArray();
91
         jj.value(98.6);
92
         jj.value(-100.0);
93
         jj.object();
94
         jj.endObject();
95
         jj.object();
96
         jj.key("one");
97
         jj.value(1.00);
98
         jj.endObject();
99
         jj.value(obj);
100
         jj.endArray();
101
         System.out.println(jj.toString());
102

    
103
         System.out.println(new JSONArray(jj.toString()).toString(4));
104

    
105
         j = new JSONObject(obj, sa);
106
         j.put("test", obj);
107
         j.put("comment", "This object contains a test object that implements JSONString");
108
         System.out.println(j.toString(4));
109

    
110
         j = new JSONObject(
111
                  "{slashes: '///', closetag: '</script>', backslash:'\\\\', ei: {quotes: '\"\\''},eo: {a: '\"quoted\"', b:\"don't\"}, quotes: [\"'\", '\"']}");
112
         System.out.println(j.toString(2));
113
         System.out.println(XML.toString(j));
114
         System.out.println("");
115

    
116
         j = new JSONObject("/*comment*/{foo: [true, false,9876543210,    0.0, 1.00000001,  1.000000000001, 1.00000000000000001,"
117
                            + " .00000000000000001, 2.00, 0.1, 2e100, -32,[],{}, \"string\"], " + "  to   : null, op : 'Good',"
118
                            + "ten:10} postfix comment");
119
         j.put("String", "98.6");
120
         j.put("JSONObject", new JSONObject());
121
         j.put("JSONArray", new JSONArray());
122
         j.put("int", 57);
123
         j.put("double", 123456789012345678901234567890.);
124
         j.put("true", true);
125
         j.put("false", false);
126
         j.put("null", JSONObject.NULL);
127
         j.put("bool", "true");
128
         j.put("zero", -0.0);
129
         j.put("\\u2028", "\u2028");
130
         j.put("\\u2029", "\u2029");
131
         a = j.getJSONArray("foo");
132
         a.put(666);
133
         a.put(2001.99);
134
         a.put("so \"fine\".");
135
         a.put("so <fine>.");
136
         a.put(true);
137
         a.put(false);
138
         a.put(new JSONArray());
139
         a.put(new JSONObject());
140
         System.out.println(j.toString(4));
141
         System.out.println(XML.toString(j));
142

    
143
         System.out.println("String: " + j.getDouble("String"));
144
         System.out.println("  bool: " + j.getBoolean("bool"));
145
         System.out.println("    to: " + j.getString("to"));
146
         System.out.println("  true: " + j.getString("true"));
147
         System.out.println("   foo: " + j.getJSONArray("foo"));
148
         System.out.println("    op: " + j.getString("op"));
149
         System.out.println("   ten: " + j.getInt("ten"));
150
         System.out.println("  oops: " + j.optBoolean("oops"));
151

    
152
         j = XML.toJSONObject("<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>");
153
         System.out.println(j.toString(2));
154
         System.out.println(XML.toString(j));
155
         System.out.println("");
156

    
157
         j = XML.toJSONObject("<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>");
158

    
159
         System.out.println(j.toString(2));
160
         System.out.println(XML.toString(j));
161
         System.out.println("");
162

    
163
         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>");
164
         System.out.println(j.toString(2));
165
         System.out.println(XML.toString(j));
166
         System.out.println("");
167

    
168
         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>");
169
         System.out.println(j.toString(2));
170
         System.out.println(XML.toString(j));
171
         System.out.println("");
172

    
173
         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>");
174
         System.out.println(j.toString(2));
175
         System.out.println(XML.toString(j));
176
         System.out.println("");
177

    
178
         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>");
179
         System.out.println(j.toString());
180
         System.out.println(XML.toString(j));
181
         System.out.println("");
182

    
183
         j = XML.toJSONObject("<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>");
184
         System.out.println(j.toString(2));
185
         System.out.println(XML.toString(j));
186
         System.out.println("");
187

    
188
         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");
189
         System.out.println(j.toString(2));
190
         System.out.println(HTTP.toString(j));
191
         System.out.println("");
192

    
193
         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");
194
         System.out.println(j.toString(2));
195
         System.out.println(HTTP.toString(j));
196
         System.out.println("");
197

    
198
         j = new JSONObject(
199
                  "{nix: null, nux: false, null: 'null', 'Request-URI': '/', Method: 'GET', 'HTTP-Version': 'HTTP/1.0'}");
200
         System.out.println(j.toString(2));
201
         System.out.println("isNull: " + j.isNull("nix"));
202
         System.out.println("   has: " + j.has("nix"));
203
         System.out.println(XML.toString(j));
204
         System.out.println(HTTP.toString(j));
205
         System.out.println("");
206

    
207
         j = XML.toJSONObject("<?xml version='1.0' encoding='UTF-8'?>" + "\n\n" + "<SOAP-ENV:Envelope"
208
                              + " xmlns:SOAP-ENV=\"http://schemas.xmlsoap.org/soap/envelope/\""
209
                              + " xmlns:xsi=\"http://www.w3.org/1999/XMLSchema-instance\""
210
                              + " xmlns:xsd=\"http://www.w3.org/1999/XMLSchema\">" + "<SOAP-ENV:Body><ns1:doGoogleSearch"
211
                              + " xmlns:ns1=\"urn:GoogleSearch\""
212
                              + " SOAP-ENV:encodingStyle=\"http://schemas.xmlsoap.org/soap/encoding/\">"
213
                              + "<key xsi:type=\"xsd:string\">GOOGLEKEY</key> <q"
214
                              + " xsi:type=\"xsd:string\">'+search+'</q> <start" + " xsi:type=\"xsd:int\">0</start> <maxResults"
215
                              + " xsi:type=\"xsd:int\">10</maxResults> <filter"
216
                              + " xsi:type=\"xsd:boolean\">true</filter> <restrict"
217
                              + " xsi:type=\"xsd:string\"></restrict> <safeSearch"
218
                              + " xsi:type=\"xsd:boolean\">false</safeSearch> <lr" + " xsi:type=\"xsd:string\"></lr> <ie"
219
                              + " xsi:type=\"xsd:string\">latin1</ie> <oe" + " xsi:type=\"xsd:string\">latin1</oe>"
220
                              + "</ns1:doGoogleSearch>" + "</SOAP-ENV:Body></SOAP-ENV:Envelope>");
221
         System.out.println(j.toString(2));
222
         System.out.println(XML.toString(j));
223
         System.out.println("");
224

    
225
         j = new JSONObject(
226
                  "{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\"}}}}");
227
         System.out.println(j.toString(2));
228
         System.out.println(XML.toString(j));
229
         System.out.println("");
230

    
231
         j = CookieList.toJSONObject("  f%oo = b+l=ah  ; o;n%40e = t.wo ");
232
         System.out.println(j.toString(2));
233
         System.out.println(CookieList.toString(j));
234
         System.out.println("");
235

    
236
         j = Cookie.toJSONObject("f%oo=blah; secure ;expires = April 24, 2002");
237
         System.out.println(j.toString(2));
238
         System.out.println(Cookie.toString(j));
239
         System.out.println("");
240

    
241
         j = new JSONObject(
242
                  "{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 /'}");
243
         System.out.println(j.toString());
244
         System.out.println("");
245

    
246
         final JSONTokener jt = new JSONTokener("{op:'test', to:'session', pre:1}{op:'test', to:'session', pre:2}");
247
         j = new JSONObject(jt);
248
         System.out.println(j.toString());
249
         System.out.println("pre: " + j.optInt("pre"));
250
         final int i = jt.skipTo('{');
251
         System.out.println(i);
252
         j = new JSONObject(jt);
253
         System.out.println(j.toString());
254
         System.out.println("");
255

    
256
         a = CDL.toJSONArray("No quotes, 'Single Quotes', \"Double Quotes\"\n1,'2',\"3\"\n,'It is \"good,\"', \"It works.\"\n\n");
257

    
258
         System.out.println(CDL.toString(a));
259
         System.out.println("");
260
         System.out.println(a.toString(4));
261
         System.out.println("");
262

    
263
         a = new JSONArray(" [\"<escape>\", next is an implied null , , ok,] ");
264
         System.out.println(a.toString());
265
         System.out.println("");
266
         System.out.println(XML.toString(a));
267
         System.out.println("");
268

    
269
         j = new JSONObject(
270
                  "{ 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. ; # comment\r oct=0666; hex=0x666; dec=666; o=0999; noh=0x0x}");
271
         System.out.println(j.toString(4));
272
         System.out.println("");
273
         if (j.getBoolean("true") && !j.getBoolean("false")) {
274
            System.out.println("It's all good");
275
         }
276

    
277
         System.out.println("");
278
         j = new JSONObject(j, new String[] { "dec", "oct", "hex", "missing" });
279
         System.out.println(j.toString(4));
280

    
281
         System.out.println("");
282
         System.out.println(new JSONStringer().array().value(a).value(j).endArray());
283

    
284
         j = new JSONObject(
285
                  "{string: \"98.6\", long: 2147483648, int: 2147483647, longer: 9223372036854775807, double: 9223372036854775808}");
286
         System.out.println(j.toString(4));
287

    
288
         System.out.println("\ngetInt");
289
         System.out.println("int    " + j.getInt("int"));
290
         System.out.println("long   " + j.getInt("long"));
291
         System.out.println("longer " + j.getInt("longer"));
292
         System.out.println("double " + j.getInt("double"));
293
         System.out.println("string " + j.getInt("string"));
294

    
295
         System.out.println("\ngetLong");
296
         System.out.println("int    " + j.getLong("int"));
297
         System.out.println("long   " + j.getLong("long"));
298
         System.out.println("longer " + j.getLong("longer"));
299
         System.out.println("double " + j.getLong("double"));
300
         System.out.println("string " + j.getLong("string"));
301

    
302
         System.out.println("\ngetDouble");
303
         System.out.println("int    " + j.getDouble("int"));
304
         System.out.println("long   " + j.getDouble("long"));
305
         System.out.println("longer " + j.getDouble("longer"));
306
         System.out.println("double " + j.getDouble("double"));
307
         System.out.println("string " + j.getDouble("string"));
308

    
309
         j.put("good sized", 9223372036854775807L);
310
         System.out.println(j.toString(4));
311

    
312
         a = new JSONArray("[2147483647, 2147483648, 9223372036854775807, 9223372036854775808]");
313
         System.out.println(a.toString(4));
314

    
315
         System.out.println("\nKeys: ");
316
         it = j.keys();
317
         while (it.hasNext()) {
318
            s = (String) it.next();
319
            System.out.println(s + ": " + j.getString(s));
320
         }
321

    
322

    
323
         System.out.println("\naccumulate: ");
324
         j = new JSONObject();
325
         j.accumulate("stooge", "Curly");
326
         j.accumulate("stooge", "Larry");
327
         j.accumulate("stooge", "Moe");
328
         a = j.getJSONArray("stooge");
329
         a.put(5, "Shemp");
330
         System.out.println(j.toString(4));
331

    
332
         System.out.println("\nwrite:");
333
         System.out.println(j.write(new StringWriter()));
334

    
335
         s = "<xml empty><a></a><a>1</a><a>22</a><a>333</a></xml>";
336
         j = XML.toJSONObject(s);
337
         System.out.println(j.toString(4));
338
         System.out.println(XML.toString(j));
339

    
340
         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>";
341
         j = XML.toJSONObject(s);
342
         System.out.println(j.toString(4));
343
         System.out.println(XML.toString(j));
344

    
345
         final Collection c = null;
346
         final Map m = null;
347

    
348
         j = new JSONObject(m);
349
         a = new JSONArray(c);
350
         j.append("stooge", "Joe DeRita");
351
         j.append("stooge", "Shemp");
352
         j.accumulate("stooges", "Curly");
353
         j.accumulate("stooges", "Larry");
354
         j.accumulate("stooges", "Moe");
355
         j.accumulate("stoogearray", j.get("stooges"));
356
         j.put("map", m);
357
         j.put("collection", c);
358
         j.put("array", a);
359
         a.put(m);
360
         a.put(c);
361
         System.out.println(j.toString(4));
362

    
363
         System.out.println("\nTesting Exceptions: ");
364

    
365
         System.out.print("Exception: ");
366
         try {
367
            System.out.println(j.getDouble("stooge"));
368
         }
369
         catch (final Exception e) {
370
            System.out.println(e);
371
         }
372
         System.out.print("Exception: ");
373
         try {
374
            System.out.println(j.getDouble("howard"));
375
         }
376
         catch (final Exception e) {
377
            System.out.println(e);
378
         }
379
         System.out.print("Exception: ");
380
         try {
381
            System.out.println(j.put(null, "howard"));
382
         }
383
         catch (final Exception e) {
384
            System.out.println(e);
385
         }
386
         System.out.print("Exception: ");
387
         try {
388
            System.out.println(a.getDouble(0));
389
         }
390
         catch (final Exception e) {
391
            System.out.println(e);
392
         }
393
         System.out.print("Exception: ");
394
         try {
395
            System.out.println(a.get(-1));
396
         }
397
         catch (final Exception e) {
398
            System.out.println(e);
399
         }
400
         System.out.print("Exception: ");
401
         try {
402
            System.out.println(a.put(Double.NaN));
403
         }
404
         catch (final Exception e) {
405
            System.out.println(e);
406
         }
407
         System.out.print("Exception: ");
408
         try {
409
            j = XML.toJSONObject("<a><b>    ");
410
         }
411
         catch (final Exception e) {
412
            System.out.println(e);
413
         }
414
         System.out.print("Exception: ");
415
         try {
416
            j = XML.toJSONObject("<a></b>    ");
417
         }
418
         catch (final Exception e) {
419
            System.out.println(e);
420
         }
421
         System.out.print("Exception: ");
422
         try {
423
            j = XML.toJSONObject("<a></a    ");
424
         }
425
         catch (final Exception e) {
426
            System.out.println(e);
427
         }
428

    
429
      }
430
      catch (final Exception e) {
431
         System.out.println(e.toString());
432
      }
433
   }
434
}