View Javadoc

1   package atg.taglib.json.util;
2   
3   import java.io.StringWriter;
4   import java.util.Iterator;
5   
6   /**
7    * Test class. This file is not formally a member of the atg.taglib.json.util library.
8    * It is just a casual test tool.
9    */
10  public class Test {
11  
12      /**
13       * Entry point.
14       * @param args
15       */
16      public static void main(String args[]) {
17          Iterator it;
18          JSONArray a;
19          JSONObject j;
20          JSONStringer jj;
21          String s;
22          try {
23              j = XML.toJSONObject("<![CDATA[This is a collection of test patterns and examples for atg.taglib.json.util.]]>");
24              System.out.println(j.toString());
25  
26              jj = new JSONStringer();
27              s = jj.object()
28                  .key("foo")
29                  .value("bar")
30                  .key("baz")
31                  .array()
32                      .object()
33                          .key("quux")
34                          .value("Thanks, Josh!")
35                      .endObject()
36                  .endArray()
37                  .endObject()
38                  .toString();
39              System.out.println(s);
40  
41              System.out.println(new JSONStringer()
42                  .object()
43                  .key("a")
44                  .array()
45                  .array()
46                  .array()
47                  .value("b")
48                  .endArray()
49                  .endArray()
50                  .endArray()
51                  .endObject()
52                  .toString());
53  
54              jj = new JSONStringer();
55              jj.array();
56              jj.value(1);
57              jj.array();
58              jj.value(null);
59              jj.array();
60              jj.object();
61              jj.key("empty-array").array().endArray();
62              jj.key("answer").value(42);
63              jj.key("null").value(null);
64              jj.key("false").value(false);
65              jj.key("true").value(true);
66              jj.key("big").value(123456789e+88);
67              jj.key("small").value(123456789e-88);
68              jj.key("empty-object").object().endObject();
69              jj.key("long");
70              jj.value(9223372036854775807L);
71              jj.endObject();
72              jj.value("two");
73              jj.endArray();
74              jj.value(true);
75              jj.endArray();
76              jj.value(98.6);
77              jj.value(-100.0);
78              jj.object();
79              jj.endObject();
80              jj.object();
81              jj.key("one");
82              jj.value(1.00);
83              jj.endObject();
84              jj.endArray();
85              System.out.println(jj.toString());
86  
87              System.out.println(new JSONArray(jj.toString()).toString(4));
88  
89              j = new JSONObject("{slashes: '///', closetag: '</script>', backslash:'\\\\', ei: {quotes: '\"\\''},eo: {a: '\"quoted\"', b:\"don't\"}, quotes: [\"'\", '\"']}");
90              System.out.println(j.toString(2));
91              System.out.println(XML.toString(j));
92              System.out.println("");
93  
94              j = new JSONObject(
95                  "/*comment*/{foo: [true, false,9876543210,    0.0, 1.00000001,  1.000000000001, 1.00000000000000001," +
96                  " .00000000000000001, 2.00, 0.1, 2e100, -32,[],{}, \"string\"], " +
97                  "  to   : null, op : 'Good'," +
98                  "ten:10} postfix comment");
99              j.put("String", "98.6");
100             j.put("JSONObject", new JSONObject());
101             j.put("JSONArray", new JSONArray());
102             j.put("int", 57);
103             j.put("double", 123456789012345678901234567890.);
104             j.put("true", true);
105             j.put("false", false);
106             j.put("null", JSONObject.NULL);
107             j.put("bool", "true");
108             j.put("zero", -0.0);
109             a = j.getJSONArray("foo");
110             a.put(666);
111             a.put(2001.99);
112             a.put("so \"fine\".");
113             a.put("so <fine>.");
114             a.put(true);
115             a.put(false);
116             a.put(new JSONArray());
117             a.put(new JSONObject());
118             System.out.println(j.toString(4));
119             System.out.println(XML.toString(j));
120 
121             System.out.println("String: " + j.getDouble("String"));
122             System.out.println("  bool: " + j.getBoolean("bool"));
123             System.out.println("    to: " + j.getString("to"));
124             System.out.println("  true: " + j.getString("true"));
125             System.out.println("   foo: " + j.getJSONArray("foo"));
126             System.out.println("    op: " + j.getString("op"));
127             System.out.println("   ten: " + j.getInt("ten"));
128             System.out.println("  oops: " + j.optBoolean("oops"));
129 
130             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>");
131             System.out.println(j.toString(2));
132             System.out.println(XML.toString(j));
133             System.out.println("");
134 
135             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>");
136 
137             System.out.println(j.toString(2));
138             System.out.println(XML.toString(j));
139             System.out.println("");
140 
141             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>");
142             System.out.println(j.toString(2));
143             System.out.println(XML.toString(j));
144             System.out.println("");
145 
146             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>");
147             System.out.println(j.toString(2));
148             System.out.println(XML.toString(j));
149             System.out.println("");
150 
151             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>");
152             System.out.println(j.toString(2));
153             System.out.println(XML.toString(j));
154             System.out.println("");
155 
156             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>");
157             System.out.println(j.toString());
158             System.out.println(XML.toString(j));
159             System.out.println("");
160 
161             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>");
162             System.out.println(j.toString(2));
163             System.out.println(XML.toString(j));
164             System.out.println("");
165 
166             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");
167             System.out.println(j.toString(2));
168             System.out.println(HTTP.toString(j));
169             System.out.println("");
170 
171             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");
172             System.out.println(j.toString(2));
173             System.out.println(HTTP.toString(j));
174             System.out.println("");
175 
176             j = new JSONObject("{nix: null, nux: false, null: 'null', 'Request-URI': '/', Method: 'GET', 'HTTP-Version': 'HTTP/1.0'}");
177             System.out.println(j.toString(2));
178             System.out.println("isNull: " + j.isNull("nix"));
179             System.out.println("   has: " + j.has("nix"));
180             System.out.println(XML.toString(j));
181             System.out.println(HTTP.toString(j));
182             System.out.println("");
183 
184             j = XML.toJSONObject("<?xml version='1.0' encoding='UTF-8'?>"+"\n\n"+"<SOAP-ENV:Envelope"+
185               " xmlns:SOAP-ENV=\"http://schemas.xmlsoap.org/soap/envelope/\""+
186               " xmlns:xsi=\"http://www.w3.org/1999/XMLSchema-instance\""+
187               " xmlns:xsd=\"http://www.w3.org/1999/XMLSchema\">"+
188               "<SOAP-ENV:Body><ns1:doGoogleSearch"+
189               " xmlns:ns1=\"urn:GoogleSearch\""+
190               " SOAP-ENV:encodingStyle=\"http://schemas.xmlsoap.org/soap/encoding/\">"+
191               "<key xsi:type=\"xsd:string\">GOOGLEKEY</key> <q"+
192               " xsi:type=\"xsd:string\">'+search+'</q> <start"+
193               " xsi:type=\"xsd:int\">0</start> <maxResults"+
194               " xsi:type=\"xsd:int\">10</maxResults> <filter"+
195               " xsi:type=\"xsd:boolean\">true</filter> <restrict"+
196               " xsi:type=\"xsd:string\"></restrict> <safeSearch"+
197               " xsi:type=\"xsd:boolean\">false</safeSearch> <lr"+
198               " xsi:type=\"xsd:string\"></lr> <ie"+
199               " xsi:type=\"xsd:string\">latin1</ie> <oe"+
200               " xsi:type=\"xsd:string\">latin1</oe>"+
201               "</ns1:doGoogleSearch>"+
202               "</SOAP-ENV:Body></SOAP-ENV:Envelope>");
203             System.out.println(j.toString(2));
204             System.out.println(XML.toString(j));
205             System.out.println("");
206 
207             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\"}}}}");
208             System.out.println(j.toString(2));
209             System.out.println(XML.toString(j));
210             System.out.println("");
211 
212             j = CookieList.toJSONObject("  f%oo = b+l=ah  ; o;n%40e = t.wo ");
213             System.out.println(j.toString(2));
214             System.out.println(CookieList.toString(j));
215             System.out.println("");
216 
217             j = Cookie.toJSONObject("f%oo=blah; secure ;expires = April 24, 2002");
218             System.out.println(j.toString(2));
219             System.out.println(Cookie.toString(j));
220             System.out.println("");
221 
222             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 /'}");
223             System.out.println(j.toString());
224             System.out.println("");
225 
226             JSONTokener jt = new JSONTokener("{op:'test', to:'session', pre:1}{op:'test', to:'session', pre:2}");
227             j = new JSONObject(jt);
228             System.out.println(j.toString());
229             System.out.println("pre: " + j.optInt("pre"));
230             int i = jt.skipTo('{');
231             System.out.println(i);
232             j = new JSONObject(jt);
233             System.out.println(j.toString());
234             System.out.println("");
235 
236             a = CDL.toJSONArray("No quotes, 'Single Quotes', \"Double Quotes\"\n1,'2',\"3\"\n,'It is \"good,\"', \"It works.\"\n\n");
237 
238             System.out.println(CDL.toString(a));
239             System.out.println("");
240             System.out.println(a.toString(4));
241             System.out.println("");
242 
243             a = new JSONArray(" [\"<escape>\", next is an implied null , , ok,] ");
244             System.out.println(a.toString());
245             System.out.println("");
246             System.out.println(XML.toString(a));
247             System.out.println("");
248 
249             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. ; # comment\r oct=0666; hex=0x666; dec=666; o=0999; noh=0x0x}");
250             System.out.println(j.toString(4));
251             System.out.println("");
252             if (j.getBoolean("true") && !j.getBoolean("false")) {
253                 System.out.println("It's all good");
254             }
255 
256             System.out.println("");
257             j = new JSONObject(j, new String[]{"dec", "oct", "hex", "missing"});
258             System.out.println(j.toString(4));
259 
260             System.out.println("");
261             System.out.println(new JSONStringer().array().value(a).value(j).endArray());
262 
263             j = new JSONObject("{string: \"98.6\", long: 2147483648, int: 2147483647, longer: 9223372036854775807, double: 9223372036854775808}");
264             System.out.println(j.toString(4));
265 
266             System.out.println("\ngetInt");
267             System.out.println("int    " + j.getInt("int"));
268             System.out.println("long   " + j.getInt("long"));
269             System.out.println("longer " + j.getInt("longer"));
270             System.out.println("double " + j.getInt("double"));
271             System.out.println("string " + j.getInt("string"));
272 
273             System.out.println("\ngetLong");
274             System.out.println("int    " + j.getLong("int"));
275             System.out.println("long   " + j.getLong("long"));
276             System.out.println("longer " + j.getLong("longer"));
277             System.out.println("double " + j.getLong("double"));
278             System.out.println("string " + j.getLong("string"));
279 
280             System.out.println("\ngetDouble");
281             System.out.println("int    " + j.getDouble("int"));
282             System.out.println("long   " + j.getDouble("long"));
283             System.out.println("longer " + j.getDouble("longer"));
284             System.out.println("double " + j.getDouble("double"));
285             System.out.println("string " + j.getDouble("string"));
286 
287             j.put("good sized", 9223372036854775807L);
288             System.out.println(j.toString(4));
289 
290             a = new JSONArray("[2147483647, 2147483648, 9223372036854775807, 9223372036854775808]");
291             System.out.println(a.toString(4));
292 
293             System.out.println("\nKeys: ");
294             it = j.keys();
295             while (it.hasNext()) {
296                 s = (String)it.next();
297                 System.out.println(s + ": " + j.getString(s));
298             }
299 
300 
301             System.out.println("\naccumulate: ");
302             j = new JSONObject();
303             j.accumulate("stooge", "Curly");
304             j.accumulate("stooge", "Larry");
305             j.accumulate("stooge", "Moe");
306             a = j.getJSONArray("stooge");
307             a.put(5, "Shemp");
308             System.out.println(j.toString(4));
309 
310             System.out.println("\nwrite:");
311             System.out.println(j.write(new StringWriter()));
312 
313             s = "<xml empty><a></a><a>1</a><a>22</a><a>333</a></xml>";
314             j = XML.toJSONObject(s);
315             System.out.println(j.toString(4));
316             System.out.println(XML.toString(j));
317 
318             System.out.println("\nExceptions: ");
319 
320             System.out.print("Exception: ");
321             try {
322                 System.out.println(j.getDouble("stooge"));
323             } catch (Exception e) {
324                 System.out.println(e);
325             }
326             System.out.print("Exception: ");
327             try {
328                 System.out.println(j.getDouble("howard"));
329             } catch (Exception e) {
330                 System.out.println(e);
331             }
332             System.out.print("Exception: ");
333             try {
334                 System.out.println(j.put(null, "howard"));
335             } catch (Exception e) {
336                 System.out.println(e);
337             }
338             System.out.print("Exception: ");
339             try {
340                 System.out.println(a.getDouble(0));
341             } catch (Exception e) {
342                 System.out.println(e);
343             }
344             System.out.print("Exception: ");
345             try {
346                 System.out.println(a.get(-1));
347             } catch (Exception e) {
348                 System.out.println(e);
349             }
350             System.out.print("Exception: ");
351             try {
352                 System.out.println(a.put(Double.NaN));
353             } catch (Exception e) {
354                 System.out.println(e);
355             }
356         } catch (Exception e) {
357             System.out.println(e.toString());
358         }
359     }
360 }