Java IRC bot
Vous ne pouvez pas sélectionner plus de 25 sujets Les noms de sujets doivent commencer par une lettre ou un nombre, peuvent contenir des tirets ('-') et peuvent comporter jusqu'à 35 caractères.

Test.java 29KB

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