From 3e3951f1259fe9ad7f385abd915a9dc9f32cfa71 Mon Sep 17 00:00:00 2001 From: Douglas Crockford Date: Wed, 5 Oct 2011 12:30:20 -0700 Subject: [PATCH] Bad XML --- Test.java | 27 ++++++++++++++++++--------- 1 file changed, 18 insertions(+), 9 deletions(-) diff --git a/Test.java b/Test.java index 2686e9c..bc61e6a 100755 --- a/Test.java +++ b/Test.java @@ -35,12 +35,12 @@ SOFTWARE. /** * Test class. This file is not formally a member of the org.json library. * It is just a test tool. - * + * * Issue: JSONObject does not specify the ordering of keys, so simple-minded * comparisons of .toString to a string literal are likely to fail. * * @author JSON.org - * @version 2011-10-03 + * @version 2011-10-05 */ public class Test extends TestCase { public Test(String name) { @@ -81,7 +81,7 @@ public class Test extends TestCase { } public void testJSON() throws Exception { - double eps = 2.220446049250313e-16; + double eps = 2.220446049250313e-16; Iterator iterator; JSONArray jsonarray; JSONObject jsonobject; @@ -94,7 +94,7 @@ public class Test extends TestCase { string = "[001122334455]"; jsonarray = new JSONArray(string); assertEquals("[1122334455]", jsonarray.toString()); - + string = "[00.10]"; jsonarray = new JSONArray(string); assertEquals("[0.1]", jsonarray.toString()); @@ -158,7 +158,7 @@ public class Test extends TestCase { jsonarray.toString(4)); assertEquals("

JSONML is a transformation betweenJSONandXMLthat preserves ordering of document features.

JSONML can work with JSON arrays or JSON objects.

Three
little
words

", JSONML.toString(jsonarray)); - + string = "{\"xmlns:soap\":\"http://www.w3.org/2003/05/soap-envelope\",\"tagName\":\"soap:Envelope\",\"childNodes\":[{\"tagName\":\"soap:Header\"},{\"tagName\":\"soap:Body\",\"childNodes\":[{\"tagName\":\"ws:listProducts\",\"childNodes\":[{\"tagName\":\"ws:delay\",\"childNodes\":[1]}]}]}],\"xmlns:ws\":\"http://warehouse.acme.com/ws\"}"; jsonobject = new JSONObject(string); assertEquals("1", @@ -291,7 +291,7 @@ public class Test extends TestCase { jsonarray.put(false); jsonarray.put(new JSONArray()); jsonarray.put(new JSONObject()); - jsonobject.put("keys", JSONObject.getNames(jsonobject)); + jsonobject.put("keys", JSONObject.getNames(jsonobject)); assertEquals("{\n \"to\": null,\n \"ten\": 10,\n \"JSONObject\": {},\n \"JSONArray\": [],\n \"op\": \"Good\",\n \"keys\": [\n \"to\",\n \"ten\",\n \"JSONObject\",\n \"JSONArray\",\n \"op\",\n \"int\",\n \"true\",\n \"foo\",\n \"zero\",\n \"double\",\n \"String\",\n \"false\",\n \"bool\",\n \"\\\\u2028\",\n \"\\\\u2029\",\n \"null\"\n ],\n \"int\": 57,\n \"true\": true,\n \"foo\": [\n true,\n false,\n 9876543210,\n 0,\n 1.00000001,\n 1.000000000001,\n 1,\n 1.0E-17,\n 2,\n 0.1,\n 2.0E100,\n -32,\n [],\n {},\n \"string\",\n 666,\n 2001.99,\n \"so \\\"fine\\\".\",\n \"so .\",\n true,\n false,\n [],\n {}\n ],\n \"zero\": -0,\n \"double\": 1.2345678901234568E29,\n \"String\": \"98.6\",\n \"false\": false,\n \"bool\": \"true\",\n \"\\\\u2028\": \"\\u2028\",\n \"\\\\u2029\": \"\\u2029\",\n \"null\": null\n}", jsonobject.toString(4)); assertEquals("null10GoodtotenJSONObjectJSONArrayopinttruefoozerodoubleStringfalsebool\\u2028\\u2029null57truetruefalse98765432100.01.000000011.0000000000011.01.0E-172.00.12.0E100-32string6662001.99so "fine".so <fine>.truefalse-0.01.2345678901234568E2998.6falsetrue<\\u2028>\u2028<\\u2029>\u2029null", @@ -544,7 +544,7 @@ public class Test extends TestCase { jsonobject.toString(1)); jsonarray = new JSONArray("[2147483647, 2147483648, 9223372036854775807, 9223372036854775808]"); - assertEquals("[\n 2147483647,\n 2147483648,\n 9223372036854775807,\n \"9223372036854775808\"\n]", + assertEquals("[\n 2147483647,\n 2147483648,\n 9223372036854775807,\n \"9223372036854775808\"\n]", jsonarray.toString(1)); List expectedKeys = new ArrayList(6); @@ -650,7 +650,7 @@ public class Test extends TestCase { string = "{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; } } "; jsonobject = new JSONObject(string); - assertEquals("{\"AnimalColors\":{\"worm\":\"pink\",\"lamb\":\"black\",\"pig\":\"pink\"},\"plist\":\"Apple\",\"AnimalSounds\":{\"worm\":\"baa\",\"Lisa\":\"Why is the worm talking like a lamb?\",\"lamb\":\"baa\",\"pig\":\"oink\"},\"AnimalSmells\":{\"worm\":\"wormy\",\"lamb\":\"lambish\",\"pig\":\"piggish\"}}", + assertEquals("{\"AnimalColors\":{\"worm\":\"pink\",\"lamb\":\"black\",\"pig\":\"pink\"},\"plist\":\"Apple\",\"AnimalSounds\":{\"worm\":\"baa\",\"Lisa\":\"Why is the worm talking like a lamb?\",\"lamb\":\"baa\",\"pig\":\"oink\"},\"AnimalSmells\":{\"worm\":\"wormy\",\"lamb\":\"lambish\",\"pig\":\"piggish\"}}", jsonobject.toString()); string = " [\"San Francisco\", \"New York\", \"Seoul\", \"London\", \"Seattle\", \"Shanghai\"]"; @@ -660,7 +660,7 @@ public class Test extends TestCase { string = "The content of b and The content of cdoremi"; jsonobject = XML.toJSONObject(string); - assertEquals("{\"a\":{\"f\":\"\",\"content\":\"and\",\"d\":[\"do\",\"re\",\"mi\"],\"ichi\":1,\"e\":\"\",\"b\":\"The content of b\",\"c\":{\"content\":\"The content of c\",\"san\":3},\"ni\":2}}", + assertEquals("{\"a\":{\"f\":\"\",\"content\":\"and\",\"d\":[\"do\",\"re\",\"mi\"],\"ichi\":1,\"e\":\"\",\"b\":\"The content of b\",\"c\":{\"content\":\"The content of c\",\"san\":3},\"ni\":2}}", jsonobject.toString()); assertEquals("anddoremi1The content of bThe content of c32", XML.toString(jsonobject)); @@ -838,6 +838,15 @@ public class Test extends TestCase { assertEquals("Mismatched 'right' and 'wrong' at 15 [character 16 line 1]", jsone.getMessage()); } + try { + string = "This ain't XML."; + jsonarray = JSONML.toJSONArray(string); + System.out.println(jsonarray.toString(4)); + fail("expecting JSONException here."); + } catch (JSONException jsone) { + assertEquals("Bad XML at 17 [character 18 line 1]", jsone.getMessage()); + } + try { string = "{\"koda\": true, \"koda\": true}"; jsonobject = new JSONObject(string);