1
0
Fork 0
mirror of https://github.com/ethauvin/JSON-java.git synced 2025-06-17 07:50:52 -07:00
This commit is contained in:
Douglas Crockford 2011-10-05 12:30:20 -07:00
parent 90e62b0e1b
commit 3e3951f125

View file

@ -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("<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>",
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("<soap:Envelope xmlns:soap=\"http://www.w3.org/2003/05/soap-envelope\" xmlns:ws=\"http://warehouse.acme.com/ws\"><soap:Header/><soap:Body><ws:listProducts><ws:delay>1</ws:delay></ws:listProducts></soap:Body></soap:Envelope>",
@ -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 <fine>.\",\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("<to>null</to><ten>10</ten><JSONObject></JSONObject><op>Good</op><keys>to</keys><keys>ten</keys><keys>JSONObject</keys><keys>JSONArray</keys><keys>op</keys><keys>int</keys><keys>true</keys><keys>foo</keys><keys>zero</keys><keys>double</keys><keys>String</keys><keys>false</keys><keys>bool</keys><keys>\\u2028</keys><keys>\\u2029</keys><keys>null</keys><int>57</int><true>true</true><foo>true</foo><foo>false</foo><foo>9876543210</foo><foo>0.0</foo><foo>1.00000001</foo><foo>1.000000000001</foo><foo>1.0</foo><foo>1.0E-17</foo><foo>2.0</foo><foo>0.1</foo><foo>2.0E100</foo><foo>-32</foo><foo></foo><foo></foo><foo>string</foo><foo>666</foo><foo>2001.99</foo><foo>so &quot;fine&quot;.</foo><foo>so &lt;fine&gt;.</foo><foo>true</foo><foo>false</foo><foo></foo><foo></foo><zero>-0.0</zero><double>1.2345678901234568E29</double><String>98.6</String><false>false</false><bool>true</bool><\\u2028>\u2028</\\u2028><\\u2029>\u2029</\\u2029><null>null</null>",
@ -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 = "<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>";
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("<a><f/>and<d>do</d><d>re</d><d>mi</d><ichi>1</ichi><e/><b>The content of b</b><c>The content of c<san>3</san></c><ni>2</ni></a>",
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);