diff --git a/.classpath b/.classpath new file mode 100644 index 000000000..0afc1a166 --- /dev/null +++ b/.classpath @@ -0,0 +1,8 @@ + + + + + + + + diff --git a/.gitignore b/.gitignore new file mode 100644 index 000000000..ac314b53d --- /dev/null +++ b/.gitignore @@ -0,0 +1,4 @@ +bin/ +build/ +dist/ +.DS_Store diff --git a/.project b/.project new file mode 100644 index 000000000..c539d8f59 --- /dev/null +++ b/.project @@ -0,0 +1,17 @@ + + + JSON-java + + + + + + org.eclipse.jdt.core.javabuilder + + + + + + org.eclipse.jdt.core.javanature + + diff --git a/build.properties b/build.properties new file mode 100644 index 000000000..34d0c2680 --- /dev/null +++ b/build.properties @@ -0,0 +1,2 @@ +javac.source=1.6 +javac.debug=true diff --git a/build.xml b/build.xml new file mode 100644 index 000000000..f423d1342 --- /dev/null +++ b/build.xml @@ -0,0 +1,120 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + diff --git a/lib/junit-4.4.jar b/lib/junit-4.4.jar new file mode 100644 index 000000000..649b0b327 Binary files /dev/null and b/lib/junit-4.4.jar differ diff --git a/CDL.java b/src/java/org/json/CDL.java old mode 100755 new mode 100644 similarity index 100% rename from CDL.java rename to src/java/org/json/CDL.java diff --git a/Cookie.java b/src/java/org/json/Cookie.java old mode 100755 new mode 100644 similarity index 100% rename from Cookie.java rename to src/java/org/json/Cookie.java diff --git a/CookieList.java b/src/java/org/json/CookieList.java old mode 100755 new mode 100644 similarity index 100% rename from CookieList.java rename to src/java/org/json/CookieList.java diff --git a/HTTP.java b/src/java/org/json/HTTP.java old mode 100755 new mode 100644 similarity index 100% rename from HTTP.java rename to src/java/org/json/HTTP.java diff --git a/HTTPTokener.java b/src/java/org/json/HTTPTokener.java old mode 100755 new mode 100644 similarity index 100% rename from HTTPTokener.java rename to src/java/org/json/HTTPTokener.java diff --git a/src/java/org/json/JSON.java b/src/java/org/json/JSON.java new file mode 100644 index 000000000..f9128932a --- /dev/null +++ b/src/java/org/json/JSON.java @@ -0,0 +1,106 @@ +/* + Copyright 2010, Strategic Gains, Inc. + + Licensed under the Apache License, Version 2.0 (the "License"); + you may not use this file except in compliance with the License. + You may obtain a copy of the License at + + http://www.apache.org/licenses/LICENSE-2.0 + + Unless required by applicable law or agreed to in writing, software + distributed under the License is distributed on an "AS IS" BASIS, + WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + See the License for the specific language governing permissions and + limitations under the License. +*/ +package org.json; + + +/** + * An uninstantiable class offering foreign convenience methods to convert between JSON and XML strings. + * + * @author toddf + * @since Jun 7, 2010 + */ +public final class JSON +{ + // SECTION: CONSTANTS + + private static final String ARRAY_ITEM_ELEMENT_NAME = "item"; + private static final String ARRAY_ELEMENT_NAME = "list"; + private static final String UNNAMED_ROOT_NAME = "root"; + + + // SECTION: CONSTRUCTOR - PRIVATE + + private JSON() + { + // simply to prevent instantiation. + } + + + // SECTION: CONVENIENCE METHODS + + public static JSONElement fromJson(String jsonString) + throws JSONException + { + if (jsonString == null || jsonString.trim().length() == 0) + { + return null; + } + + String json = jsonString.trim(); + + if (json.startsWith("[")) + { + return new JSONObject().put(ARRAY_ITEM_ELEMENT_NAME, new JSONArray(json)); + } + + return new JSONObject(json); + } + + public static String toJson(JSONElement jsonElement) + throws JSONException + { + return null; + } + + public static JSONElement fromXml(String xml) + throws JSONException + { + return null; + } + + public static String toXml(String json) + throws JSONException + { + return toXml(fromJson(json)); + } + + public static String toXml(JSONElement jsonElement) + throws JSONException + { + String wrappingTag = null; + + if (isJsonArray(jsonElement.first())) + { + wrappingTag = ARRAY_ELEMENT_NAME; + } + else if (jsonElement.length() > 1) + { + wrappingTag = UNNAMED_ROOT_NAME; + } + + return XML.toString(jsonElement, wrappingTag); + } + + + /** + * @param object + * @return + */ + private static boolean isJsonArray(Object object) + { + return (object instanceof JSONArray); + } +} diff --git a/JSONArray.java b/src/java/org/json/JSONArray.java similarity index 99% rename from JSONArray.java rename to src/java/org/json/JSONArray.java index b5a2dd4bf..74500bc5f 100644 --- a/JSONArray.java +++ b/src/java/org/json/JSONArray.java @@ -79,7 +79,8 @@ of this software and associated documentation files (the "Software"), to deal * @author JSON.org * @version 2012-11-13 */ -public class JSONArray { +public class JSONArray +extends JSONElement { /** @@ -366,6 +367,7 @@ public String join(String separator) throws JSONException { * * @return The length (or size). */ + @Override public int length() { return this.myArrayList.size(); } diff --git a/src/java/org/json/JSONElement.java b/src/java/org/json/JSONElement.java new file mode 100644 index 000000000..603b1892d --- /dev/null +++ b/src/java/org/json/JSONElement.java @@ -0,0 +1,81 @@ +/* + Copyright 2010, Strategic Gains, Inc. + + Licensed under the Apache License, Version 2.0 (the "License"); + you may not use this file except in compliance with the License. + You may obtain a copy of the License at + + http://www.apache.org/licenses/LICENSE-2.0 + + Unless required by applicable law or agreed to in writing, software + distributed under the License is distributed on an "AS IS" BASIS, + WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + See the License for the specific language governing permissions and + limitations under the License. +*/ +package org.json; + +/** + * @author toddf + * @since Jun 7, 2010 + */ +public abstract class JSONElement +{ + public boolean isJsonArray() + { + return (this instanceof JSONArray); + } + + public boolean isJsonObject() + { + return (this instanceof JSONObject); + } + + public Object first() + throws JSONException + { + if (!isJsonObject()) + { + throw new JSONException("first() is not supported."); + } + + JSONObject object = (JSONObject) this; + return object.get((String) object.keys().next()); + + } + + public abstract int length(); + + + // UTILITY - PUBLIC + + public static JSONElement from(JSONTokener x) + throws JSONException + { + JSONElement result = null; + + try + { + result = new JSONObject(x); + } + catch(JSONException e) + { + try + { + result = new JSONArray(x); + } + catch (JSONException ex) + { + throw new JSONException(e.getMessage() + " or " + ex.getMessage()); + } + } + + return result; + } + + public static JSONElement from(String jsonString) + throws JSONException + { + return from(new JSONTokener(jsonString)); + } +} diff --git a/JSONException.java b/src/java/org/json/JSONException.java old mode 100755 new mode 100644 similarity index 100% rename from JSONException.java rename to src/java/org/json/JSONException.java diff --git a/JSONML.java b/src/java/org/json/JSONML.java old mode 100755 new mode 100644 similarity index 100% rename from JSONML.java rename to src/java/org/json/JSONML.java diff --git a/JSONObject.java b/src/java/org/json/JSONObject.java old mode 100755 new mode 100644 similarity index 99% rename from JSONObject.java rename to src/java/org/json/JSONObject.java index 399f093c1..4da2dfb4f --- a/JSONObject.java +++ b/src/java/org/json/JSONObject.java @@ -93,7 +93,8 @@ of this software and associated documentation files (the "Software"), to deal * @author JSON.org * @version 2012-12-01 */ -public class JSONObject { +public class JSONObject +extends JSONElement { /** * The maximum number of keys in the key pool. */ @@ -734,6 +735,7 @@ public Set keySet() { * * @return The number of keys in the JSONObject. */ + @Override public int length() { return this.map.size(); } diff --git a/JSONString.java b/src/java/org/json/JSONString.java old mode 100755 new mode 100644 similarity index 100% rename from JSONString.java rename to src/java/org/json/JSONString.java diff --git a/JSONStringer.java b/src/java/org/json/JSONStringer.java old mode 100755 new mode 100644 similarity index 100% rename from JSONStringer.java rename to src/java/org/json/JSONStringer.java diff --git a/JSONTokener.java b/src/java/org/json/JSONTokener.java similarity index 100% rename from JSONTokener.java rename to src/java/org/json/JSONTokener.java diff --git a/JSONWriter.java b/src/java/org/json/JSONWriter.java old mode 100755 new mode 100644 similarity index 100% rename from JSONWriter.java rename to src/java/org/json/JSONWriter.java diff --git a/XML.java b/src/java/org/json/XML.java old mode 100755 new mode 100644 similarity index 100% rename from XML.java rename to src/java/org/json/XML.java diff --git a/XMLTokener.java b/src/java/org/json/XMLTokener.java old mode 100755 new mode 100644 similarity index 100% rename from XMLTokener.java rename to src/java/org/json/XMLTokener.java diff --git a/test/java/org/json/JSONTest.java b/test/java/org/json/JSONTest.java new file mode 100644 index 000000000..58e978327 --- /dev/null +++ b/test/java/org/json/JSONTest.java @@ -0,0 +1,91 @@ +/* + Copyright 2010, Strategic Gains, Inc. + + Licensed under the Apache License, Version 2.0 (the "License"); + you may not use this file except in compliance with the License. + You may obtain a copy of the License at + + http://www.apache.org/licenses/LICENSE-2.0 + + Unless required by applicable law or agreed to in writing, software + distributed under the License is distributed on an "AS IS" BASIS, + WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + See the License for the specific language governing permissions and + limitations under the License. + */ +package org.json; + +import static org.junit.Assert.assertEquals; + +import org.junit.Test; + +/** + * @author toddf + * @since Jun 7, 2010 + */ +public class JSONTest +{ + + @Test + public void shouldConvertJsonNamedListToXmlElements() + throws JSONException + { + String json = "{\"stuff\":[{\"id\":1005,\"subject\":\"foo\"},{\"id\":1006,\"subject\":\"bar\"}]}"; + String xml = JSON.toXml(json); + assertEquals( + "1005foo1006bar", + xml); + } + + @Test + public void shouldConvertJsonNamedRootToXmlElements() throws JSONException + { + String json = "{ \"resource\" : { \"name\" : \"test post\", \"data\" : \"some data\" } }"; + String xml = JSON.toXml(json); + assertEquals("test postsome data", xml); + } + + @Test + public void shouldConvertJsonUnnamedArrayToXmlElements() + throws JSONException + { + String json = "[{\"id\":1005,\"subject\":\"foo\"},{\"id\":1006,\"subject\":\"bar\"}]"; + String xml = JSON.toXml(json); + assertEquals("1005foo1006bar", xml); + } + + @Test + public void shouldConvertJsonUnnamedRootToXmlElements() + throws JSONException + { + String json = "{\"access_token\":\"mauth|79889m9rwet|2114798|2010-06-07T09%3a51%3a03|66cb32d9e0cf9ea2dad1f999946af951\",\"expires\":3600}"; + String xml = JSON.toXml(json); + assertEquals("3600mauth|79889m9rwet|2114798|2010-06-07T09%3a51%3a03|66cb32d9e0cf9ea2dad1f999946af951", xml); + } + + @Test + public void shouldConvertJsonNamedValueToXmlElements() throws JSONException + { + String json = "{\"access_token\":{\"name\":\"value\"}}"; + String xml = JSON.toXml(json); + assertEquals("value", xml); + } + + @Test + public void shouldConvertJsonValueToXmlElements() throws JSONException + { + String json = "{\"access_token\":\"value\"}"; + String xml = JSON.toXml(json); + assertEquals("value", xml); + } + + @Test + public void shouldConvertHeinousJsonUnnamedListToXmlElements() + throws JSONException + { + String json = "[{\"named_list\":[{\"a\":\"a_value\"}, {\"b\":\"b_value\"}, {\"c\":\"c_value\"}]},{\"named_root\":{\"foo\":\"bar\"}},{\"a\":\"b\",\"c\":\"d\"},[{\"humpty\":1}, {\"dumpty\":2}]]"; + String xml = JSON.toXml(json); + assertEquals("a_valueb_valuec_valuebardb12", xml); + } + +} diff --git a/test/java/org/json/SampleResourceBundle_en_US.java b/test/java/org/json/SampleResourceBundle_en_US.java new file mode 100644 index 000000000..58113c4e7 --- /dev/null +++ b/test/java/org/json/SampleResourceBundle_en_US.java @@ -0,0 +1,29 @@ +/* + * File: SampleResourceBundle_en_US.java + * Author: JSON.org + */ +package org.json; + +import java.util.*; + +/** + * The Class SampleResourceBundle_en_US. + */ +public class SampleResourceBundle_en_US extends ListResourceBundle { + + /* (non-Javadoc) + * @see java.util.ListResourceBundle#getContents() + */ + @Override + public Object[][] getContents() { + return contents; + } + + /** The contents. */ + private Object[][] contents = { + { "ASCII", "American Standard Code for Information Interchange" }, + { "JAVA.desc", "Just Another Vague Acronym" }, + { "JAVA.data", "Sweet language" }, + { "JSON", "JavaScript Object Notation" }, + }; +} \ No newline at end of file diff --git a/test/java/org/json/SampleResourceBundle_fr.java b/test/java/org/json/SampleResourceBundle_fr.java new file mode 100644 index 000000000..54fae4bca --- /dev/null +++ b/test/java/org/json/SampleResourceBundle_fr.java @@ -0,0 +1,28 @@ +/* + * File: SampleResourceBundle_fr.java + * Author: JSON.org + */ +package org.json; + +import java.util.*; + +/** + * The Class SampleResourceBundle_fr. + */ +public class SampleResourceBundle_fr extends ListResourceBundle { + + /* (non-Javadoc) + * @see java.util.ListResourceBundle#getContents() + */ + @Override + public Object[][] getContents() { + return contents; + } + + /** The contents. */ + private Object[][] contents = { + { "ASCII", "Number that represent chraracters" }, + { "JAVA", "The language you are running to see this" }, + { "JSON", "What are we testing?" }, + }; +} \ No newline at end of file diff --git a/test/java/org/json/Test.java b/test/java/org/json/Test.java new file mode 100644 index 000000000..523734cdb --- /dev/null +++ b/test/java/org/json/Test.java @@ -0,0 +1,931 @@ +package org.json; + +import java.util.ArrayList; +import java.util.Collection; +import java.util.Iterator; +import java.util.List; +import java.util.Map; +import java.io.StringWriter; +import junit.framework.TestCase; + +/* +Copyright (c) 2002 JSON.org + +Permission is hereby granted, free of charge, to any person obtaining a copy +of this software and associated documentation files (the "Software"), to deal +in the Software without restriction, including without limitation the rights +to use, copy, modify, merge, publish, distribute, sublicense, and/or sell +copies of the Software, and to permit persons to whom the Software is +furnished to do so, subject to the following conditions: + +The above copyright notice and this permission notice shall be included in all +copies or substantial portions of the Software. + +The Software shall be used for Good, not Evil. + +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE +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-25 + */ +public class Test extends TestCase { + public Test(String name) { + super(name); + } + + protected void setUp() throws Exception { + super.setUp(); + } + + protected void tearDown() throws Exception { + super.tearDown(); + } + + public void testXML() throws Exception { + JSONObject jsonobject; + String string; + + jsonobject = XML.toJSONObject(" Ignore the stuff past the end. "); + assertEquals("{\"content\":\"This is a collection of test patterns and examples for org.json.\"}", jsonobject.toString()); + assertEquals("This is a collection of test patterns and examples for org.json.", jsonobject.getString("content")); + + string = ""; + jsonobject = XML.toJSONObject(string); + assertEquals("{\"test\": {\n \"blank\": \"\",\n \"empty\": \"\"\n}}", jsonobject.toString(2)); + assertEquals("", XML.toString(jsonobject)); + + string = ""; + jsonobject = XML.toJSONObject(string); + assertEquals("{\"subsonic-response\":{\"playlists\":{\"playlist\":[{\"id\":\"476c65652e6d3375\",\"int\":\"12345678901234567890123456789012345678901234567890213991133777039355058536718668104339937\"},{\"id\":\"50617274792e78737066\"}]}}}", jsonobject.toString()); + } + + public void testNull() throws Exception { + JSONObject jsonobject; + + jsonobject = new JSONObject("{\"message\":\"null\"}"); + assertFalse(jsonobject.isNull("message")); + assertEquals("null", jsonobject.getString("message")); + + jsonobject = new JSONObject("{\"message\":null}"); + assertTrue(jsonobject.isNull("message")); + } + + public void testJSON() throws Exception { + double eps = 2.220446049250313e-16; + Iterator iterator; + JSONArray jsonarray; + JSONObject jsonobject; + JSONStringer jsonstringer; + Object object; + String string; + + Beany beanie = new Beany("A beany object", 42, true); + + string = "[001122334455]"; + jsonarray = new JSONArray(string); + assertEquals("[1122334455]", jsonarray.toString()); + + string = "[666e666]"; + jsonarray = new JSONArray(string); + assertEquals("[\"666e666\"]", jsonarray.toString()); + + string = "[00.10]"; + jsonarray = new JSONArray(string); + assertEquals("[0.1]", jsonarray.toString()); + + jsonobject = new JSONObject(); + object = null; + jsonobject.put("booga", object); + jsonobject.put("wooga", JSONObject.NULL); + assertEquals("{\"wooga\":null}", jsonobject.toString()); + assertTrue(jsonobject.isNull("booga")); + + jsonobject = new JSONObject(); + jsonobject.increment("two"); + jsonobject.increment("two"); + assertEquals("{\"two\":2}", jsonobject.toString()); + assertEquals(2, jsonobject.getInt("two")); + + string = "{ \"list of lists\" : [ [1, 2, 3], [4, 5, 6], ] }"; + jsonobject = new JSONObject(string); + assertEquals("{\"list of lists\": [\n" + + " [\n" + + " 1,\n" + + " 2,\n" + + " 3\n" + + " ],\n" + + " [\n" + + " 4,\n" + + " 5,\n" + + " 6\n" + + " ]\n" + + "]}", jsonobject.toString(4)); + assertEquals("123456", + XML.toString(jsonobject)); + + string = " Basic bread Flour Yeast Water Salt Mix all ingredients together. Knead thoroughly. Cover with a cloth, and leave for one hour in warm room. Knead again. Place in a bread baking tin. Cover with a cloth, and leave for one hour in warm room. Bake in the oven at 180(degrees)C for 30 minutes. "; + jsonobject = XML.toJSONObject(string); + assertEquals("{\"recipe\": {\n \"title\": \"Basic bread\",\n \"cook_time\": \"3 hours\",\n \"instructions\": {\"step\": [\n \"Mix all ingredients together.\",\n \"Knead thoroughly.\",\n \"Cover with a cloth, and leave for one hour in warm room.\",\n \"Knead again.\",\n \"Place in a bread baking tin.\",\n \"Cover with a cloth, and leave for one hour in warm room.\",\n \"Bake in the oven at 180(degrees)C for 30 minutes.\"\n ]},\n \"name\": \"bread\",\n \"ingredient\": [\n {\n \"content\": \"Flour\",\n \"amount\": 8,\n \"unit\": \"dL\"\n },\n {\n \"content\": \"Yeast\",\n \"amount\": 10,\n \"unit\": \"grams\"\n },\n {\n \"content\": \"Water\",\n \"amount\": 4,\n \"unit\": \"dL\",\n \"state\": \"warm\"\n },\n {\n \"content\": \"Salt\",\n \"amount\": 1,\n \"unit\": \"teaspoon\"\n }\n ],\n \"prep_time\": \"5 mins\"\n}}", + jsonobject.toString(4)); + + jsonobject = JSONML.toJSONObject(string); + assertEquals("{\"cook_time\":\"3 hours\",\"name\":\"bread\",\"tagName\":\"recipe\",\"childNodes\":[{\"tagName\":\"title\",\"childNodes\":[\"Basic bread\"]},{\"amount\":8,\"unit\":\"dL\",\"tagName\":\"ingredient\",\"childNodes\":[\"Flour\"]},{\"amount\":10,\"unit\":\"grams\",\"tagName\":\"ingredient\",\"childNodes\":[\"Yeast\"]},{\"amount\":4,\"unit\":\"dL\",\"tagName\":\"ingredient\",\"state\":\"warm\",\"childNodes\":[\"Water\"]},{\"amount\":1,\"unit\":\"teaspoon\",\"tagName\":\"ingredient\",\"childNodes\":[\"Salt\"]},{\"tagName\":\"instructions\",\"childNodes\":[{\"tagName\":\"step\",\"childNodes\":[\"Mix all ingredients together.\"]},{\"tagName\":\"step\",\"childNodes\":[\"Knead thoroughly.\"]},{\"tagName\":\"step\",\"childNodes\":[\"Cover with a cloth, and leave for one hour in warm room.\"]},{\"tagName\":\"step\",\"childNodes\":[\"Knead again.\"]},{\"tagName\":\"step\",\"childNodes\":[\"Place in a bread baking tin.\"]},{\"tagName\":\"step\",\"childNodes\":[\"Cover with a cloth, and leave for one hour in warm room.\"]},{\"tagName\":\"step\",\"childNodes\":[\"Bake in the oven at 180(degrees)C for 30 minutes.\"]}]}],\"prep_time\":\"5 mins\"}", + jsonobject.toString()); + assertEquals("Basic breadFlourYeastWaterSaltMix all ingredients together.Knead thoroughly.Cover with a cloth, and leave for one hour in warm room.Knead again.Place in a bread baking tin.Cover with a cloth, and leave for one hour in warm room.Bake in the oven at 180(degrees)C for 30 minutes.", + JSONML.toString(jsonobject)); + + jsonarray = JSONML.toJSONArray(string); + assertEquals("[\n \"recipe\",\n {\n \"cook_time\": \"3 hours\",\n \"name\": \"bread\",\n \"prep_time\": \"5 mins\"\n },\n [\n \"title\",\n \"Basic bread\"\n ],\n [\n \"ingredient\",\n {\n \"amount\": 8,\n \"unit\": \"dL\"\n },\n \"Flour\"\n ],\n [\n \"ingredient\",\n {\n \"amount\": 10,\n \"unit\": \"grams\"\n },\n \"Yeast\"\n ],\n [\n \"ingredient\",\n {\n \"amount\": 4,\n \"unit\": \"dL\",\n \"state\": \"warm\"\n },\n \"Water\"\n ],\n [\n \"ingredient\",\n {\n \"amount\": 1,\n \"unit\": \"teaspoon\"\n },\n \"Salt\"\n ],\n [\n \"instructions\",\n [\n \"step\",\n \"Mix all ingredients together.\"\n ],\n [\n \"step\",\n \"Knead thoroughly.\"\n ],\n [\n \"step\",\n \"Cover with a cloth, and leave for one hour in warm room.\"\n ],\n [\n \"step\",\n \"Knead again.\"\n ],\n [\n \"step\",\n \"Place in a bread baking tin.\"\n ],\n [\n \"step\",\n \"Cover with a cloth, and leave for one hour in warm room.\"\n ],\n [\n \"step\",\n \"Bake in the oven at 180(degrees)C for 30 minutes.\"\n ]\n ]\n]", + jsonarray.toString(4)); + assertEquals("Basic breadFlourYeastWaterSaltMix all ingredients together.Knead thoroughly.Cover with a cloth, and leave for one hour in warm room.Knead again.Place in a bread baking tin.Cover with a cloth, and leave for one hour in warm room.Bake in the oven at 180(degrees)C for 30 minutes.", + JSONML.toString(jsonarray)); + + string = "

JSONML is a transformation between JSON and XML that preserves ordering of document features.

JSONML can work with JSON arrays or JSON objects.

Three
little
words

"; + jsonobject = JSONML.toJSONObject(string); + assertEquals("{\n \"id\": \"demo\",\n \"tagName\": \"div\",\n \"class\": \"JSONML\",\n \"childNodes\": [\n {\n \"tagName\": \"p\",\n \"childNodes\": [\n \"JSONML is a transformation between\",\n {\n \"tagName\": \"b\",\n \"childNodes\": [\"JSON\"]\n },\n \"and\",\n {\n \"tagName\": \"b\",\n \"childNodes\": [\"XML\"]\n },\n \"that preserves ordering of document features.\"\n ]\n },\n {\n \"tagName\": \"p\",\n \"childNodes\": [\"JSONML can work with JSON arrays or JSON objects.\"]\n },\n {\n \"tagName\": \"p\",\n \"childNodes\": [\n \"Three\",\n {\"tagName\": \"br\"},\n \"little\",\n {\"tagName\": \"br\"},\n \"words\"\n ]\n }\n ]\n}", + jsonobject.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(jsonobject)); + + jsonarray = JSONML.toJSONArray(string); + assertEquals("[\n \"div\",\n {\n \"id\": \"demo\",\n \"class\": \"JSONML\"\n },\n [\n \"p\",\n \"JSONML is a transformation between\",\n [\n \"b\",\n \"JSON\"\n ],\n \"and\",\n [\n \"b\",\n \"XML\"\n ],\n \"that preserves ordering of document features.\"\n ],\n [\n \"p\",\n \"JSONML can work with JSON arrays or JSON objects.\"\n ],\n [\n \"p\",\n \"Three\",\n [\"br\"],\n \"little\",\n [\"br\"],\n \"words\"\n ]\n]", + 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", + JSONML.toString(jsonobject)); + + string = "\n Robert\n Smith\n
\n 12345 Sixth Ave\n Anytown\n CA\n 98765-4321\n
\n
"; + jsonobject = XML.toJSONObject(string); + assertEquals("{\"person\": {\n \"lastName\": \"Smith\",\n \"address\": {\n \"postalCode\": \"98765-4321\",\n \"street\": \"12345 Sixth Ave\",\n \"state\": \"CA\",\n \"type\": \"home\",\n \"city\": \"Anytown\"\n },\n \"created\": \"2006-11-11T19:23\",\n \"firstName\": \"Robert\",\n \"modified\": \"2006-12-31T23:59\"\n}}", + jsonobject.toString(4)); + + string = "{ \"entity\": { \"imageURL\": \"\", \"name\": \"IXXXXXXXXXXXXX\", \"id\": 12336, \"ratingCount\": null, \"averageRating\": null } }"; + jsonobject = new JSONObject(string); + assertEquals("{\"entity\": {\n \"id\": 12336,\n \"averageRating\": null,\n \"ratingCount\": null,\n \"name\": \"IXXXXXXXXXXXXX\",\n \"imageURL\": \"\"\n}}", + jsonobject.toString(2)); + + jsonstringer = new JSONStringer(); + string = jsonstringer + .object() + .key("single") + .value("MARIE HAA'S") + .key("Johnny") + .value("MARIE HAA\\'S") + .key("foo") + .value("bar") + .key("baz") + .array() + .object() + .key("quux") + .value("Thanks, Josh!") + .endObject() + .endArray() + .key("obj keys") + .value(JSONObject.getNames(beanie)) + .endObject() + .toString(); + assertEquals("{\"single\":\"MARIE HAA'S\",\"Johnny\":\"MARIE HAA\\\\'S\",\"foo\":\"bar\",\"baz\":[{\"quux\":\"Thanks, Josh!\"}],\"obj keys\":[\"aString\",\"aNumber\",\"aBoolean\"]}" + , string); + + assertEquals("{\"a\":[[[\"b\"]]]}" + , new JSONStringer() + .object() + .key("a") + .array() + .array() + .array() + .value("b") + .endArray() + .endArray() + .endArray() + .endObject() + .toString()); + + jsonstringer = new JSONStringer(); + jsonstringer.array(); + jsonstringer.value(1); + jsonstringer.array(); + jsonstringer.value(null); + jsonstringer.array(); + jsonstringer.object(); + jsonstringer.key("empty-array").array().endArray(); + jsonstringer.key("answer").value(42); + jsonstringer.key("null").value(null); + jsonstringer.key("false").value(false); + jsonstringer.key("true").value(true); + jsonstringer.key("big").value(123456789e+88); + jsonstringer.key("small").value(123456789e-88); + jsonstringer.key("empty-object").object().endObject(); + jsonstringer.key("long"); + jsonstringer.value(9223372036854775807L); + jsonstringer.endObject(); + jsonstringer.value("two"); + jsonstringer.endArray(); + jsonstringer.value(true); + jsonstringer.endArray(); + jsonstringer.value(98.6); + jsonstringer.value(-100.0); + jsonstringer.object(); + jsonstringer.endObject(); + jsonstringer.object(); + jsonstringer.key("one"); + jsonstringer.value(1.00); + jsonstringer.endObject(); + jsonstringer.value(beanie); + jsonstringer.endArray(); + assertEquals("[1,[null,[{\"empty-array\":[],\"answer\":42,\"null\":null,\"false\":false,\"true\":true,\"big\":1.23456789E96,\"small\":1.23456789E-80,\"empty-object\":{},\"long\":9223372036854775807},\"two\"],true],98.6,-100,{},{\"one\":1},{\"A beany object\":42}]", + jsonstringer.toString()); + assertEquals("[\n 1,\n [\n null,\n [\n {\n \"empty-array\": [],\n \"empty-object\": {},\n \"answer\": 42,\n \"true\": true,\n \"false\": false,\n \"long\": 9223372036854775807,\n \"big\": 1.23456789E96,\n \"small\": 1.23456789E-80,\n \"null\": null\n },\n \"two\"\n ],\n true\n ],\n 98.6,\n -100,\n {},\n {\"one\": 1},\n {\"A beany object\": 42}\n]", + new JSONArray(jsonstringer.toString()).toString(4)); + + int ar[] = {1, 2, 3}; + JSONArray ja = new JSONArray(ar); + assertEquals("[1,2,3]", ja.toString()); + assertEquals("123", XML.toString(ar)); + + String sa[] = {"aString", "aNumber", "aBoolean"}; + jsonobject = new JSONObject(beanie, sa); + jsonobject.put("Testing JSONString interface", beanie); + assertEquals("{\n \"aBoolean\": true,\n \"aNumber\": 42,\n \"aString\": \"A beany object\",\n \"Testing JSONString interface\": {\"A beany object\":42}\n}", + jsonobject.toString(4)); + + jsonobject = new JSONObject("{slashes: '///', closetag: '', backslash:'\\\\', ei: {quotes: '\"\\''},eo: {a: '\"quoted\"', b:\"don't\"}, quotes: [\"'\", '\"']}"); + assertEquals("{\n \"quotes\": [\n \"'\",\n \"\\\"\"\n ],\n \"slashes\": \"///\",\n \"ei\": {\"quotes\": \"\\\"'\"},\n \"eo\": {\n \"b\": \"don't\",\n \"a\": \"\\\"quoted\\\"\"\n },\n \"closetag\": \"<\\/script>\",\n \"backslash\": \"\\\\\"\n}", + jsonobject.toString(2)); + assertEquals("'"///"'don't"quoted"</script>\\", + XML.toString(jsonobject)); + + jsonobject = new JSONObject( + "{foo: [true, false,9876543210, 0.0, 1.00000001, 1.000000000001, 1.00000000000000001," + + " .00000000000000001, 2.00, 0.1, 2e100, -32,[],{}, \"string\"], " + + " to : null, op : 'Good'," + + "ten:10} postfix comment"); + jsonobject.put("String", "98.6"); + jsonobject.put("JSONObject", new JSONObject()); + jsonobject.put("JSONArray", new JSONArray()); + jsonobject.put("int", 57); + jsonobject.put("double", 123456789012345678901234567890.); + jsonobject.put("true", true); + jsonobject.put("false", false); + jsonobject.put("null", JSONObject.NULL); + jsonobject.put("bool", "true"); + jsonobject.put("zero", -0.0); + jsonobject.put("\\u2028", "\u2028"); + jsonobject.put("\\u2029", "\u2029"); + jsonarray = jsonobject.getJSONArray("foo"); + jsonarray.put(666); + jsonarray.put(2001.99); + jsonarray.put("so \"fine\"."); + jsonarray.put("so ."); + jsonarray.put(true); + jsonarray.put(false); + jsonarray.put(new JSONArray()); + jsonarray.put(new 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", + XML.toString(jsonobject)); + assertEquals(98.6d, jsonobject.getDouble("String"), eps); + assertTrue(jsonobject.getBoolean("bool")); + assertEquals("[true,false,9876543210,0,1.00000001,1.000000000001,1,1.0E-17,2,0.1,2.0E100,-32,[],{},\"string\",666,2001.99,\"so \\\"fine\\\".\",\"so .\",true,false,[],{}]", + jsonobject.getJSONArray("foo").toString()); + assertEquals("Good", jsonobject.getString("op")); + assertEquals(10, jsonobject.getInt("ten")); + assertFalse(jsonobject.optBoolean("oops")); + + string = "First \u0009<content> This is \"content\". 3 JSON does not preserve the sequencing of elements and contents. III T H R E EContent text is an implied structure in XML. JSON does not have implied structure:7everything is explicit.!]]>"; + jsonobject = XML.toJSONObject(string); + assertEquals("{\"xml\": {\n \"content\": [\n \"First \\t\",\n \"This is \\\"content\\\".\",\n \"JSON does not preserve the sequencing of elements and contents.\",\n \"Content text is an implied structure in XML.\",\n \"JSON does not have implied structure:\",\n \"everything is explicit.\",\n \"CDATA blocks!\"\n ],\n \"two\": \" \\\"2\\\" \",\n \"seven\": 7,\n \"five\": [\n \"\",\n \"\"\n ],\n \"one\": 1,\n \"three\": [\n 3,\n \"III\",\n \"T H R E E\"\n ],\n \"four\": \"\",\n \"six\": {\"content\": 6}\n}}", + jsonobject.toString(2)); + assertEquals("First \t<content>\n" + + "This is "content".\n" + + "JSON does not preserve the sequencing of elements and contents.\n" + + "Content text is an implied structure in XML.\n" + + "JSON does not have implied structure:\n" + + "everything is explicit.\n" + + "CDATA blocks<are><supported>! "2" 713IIIT H R E E6", + XML.toString(jsonobject)); + + ja = JSONML.toJSONArray(string); + assertEquals("[\n \"xml\",\n {\n \"two\": \" \\\"2\\\" \",\n \"one\": 1\n },\n [\"five\"],\n \"First \\t\",\n [\"five\"],\n \"This is \\\"content\\\".\",\n [\n \"three\",\n 3\n ],\n \"JSON does not preserve the sequencing of elements and contents.\",\n [\n \"three\",\n \"III\"\n ],\n [\n \"three\",\n \"T H R E E\"\n ],\n [\"four\"],\n \"Content text is an implied structure in XML.\",\n [\n \"six\",\n {\"content\": 6}\n ],\n \"JSON does not have implied structure:\",\n [\n \"seven\",\n 7\n ],\n \"everything is explicit.\",\n \"CDATA blocks!\"\n]", + ja.toString(4)); + assertEquals("First \t<content>This is "content".JSON does not preserve the sequencing of elements and contents.IIIT H R E EContent text is an implied structure in XML.JSON does not have implied structure:everything is explicit.CDATA blocks<are><supported>!", + JSONML.toString(ja)); + + string = "unodostrestruequatrocinqoseis"; + ja = JSONML.toJSONArray(string); + assertEquals("[\n \"xml\",\n {\"do\": 0},\n \"uno\",\n [\n \"a\",\n {\n \"re\": 1,\n \"mi\": 2\n },\n \"dos\",\n [\n \"b\",\n {\"fa\": 3}\n ],\n \"tres\",\n [\n \"c\",\n true\n ],\n \"quatro\"\n ],\n \"cinqo\",\n [\n \"d\",\n \"seis\",\n [\"e\"]\n ]\n]", + ja.toString(4)); + assertEquals("unodostresquatrocinqoseis", + JSONML.toString(ja)); + + string = " "; + jsonobject = XML.toJSONObject(string); + + assertEquals("{\"mapping\": {\n \"empty\": \"\",\n \"class\": [\n {\n \"field\": [\n {\n \"bind-xml\": {\n \"node\": \"attribute\",\n \"name\": \"ID\"\n },\n \"name\": \"ID\",\n \"type\": \"string\"\n },\n {\n \"name\": \"FirstName\",\n \"type\": \"FirstName\"\n },\n {\n \"name\": \"MI\",\n \"type\": \"MI\"\n },\n {\n \"name\": \"LastName\",\n \"type\": \"LastName\"\n }\n ],\n \"name\": \"Customer\"\n },\n {\n \"field\": {\n \"bind-xml\": {\n \"node\": \"text\",\n \"name\": \"text\"\n },\n \"name\": \"text\"\n },\n \"name\": \"FirstName\"\n },\n {\n \"field\": {\n \"bind-xml\": {\n \"node\": \"text\",\n \"name\": \"text\"\n },\n \"name\": \"text\"\n },\n \"name\": \"MI\"\n },\n {\n \"field\": {\n \"bind-xml\": {\n \"node\": \"text\",\n \"name\": \"text\"\n },\n \"name\": \"text\"\n },\n \"name\": \"LastName\"\n }\n ]\n}}", + jsonobject.toString(2)); + assertEquals("attributeIDIDstringFirstNameFirstNameMIMILastNameLastNameCustomertexttexttextFirstNametexttexttextMItexttexttextLastName", + XML.toString(jsonobject)); + ja = JSONML.toJSONArray(string); + assertEquals("[\n \"mapping\",\n [\"empty\"],\n [\n \"class\",\n {\"name\": \"Customer\"},\n [\n \"field\",\n {\n \"name\": \"ID\",\n \"type\": \"string\"\n },\n [\n \"bind-xml\",\n {\n \"node\": \"attribute\",\n \"name\": \"ID\"\n }\n ]\n ],\n [\n \"field\",\n {\n \"name\": \"FirstName\",\n \"type\": \"FirstName\"\n }\n ],\n [\n \"field\",\n {\n \"name\": \"MI\",\n \"type\": \"MI\"\n }\n ],\n [\n \"field\",\n {\n \"name\": \"LastName\",\n \"type\": \"LastName\"\n }\n ]\n ],\n [\n \"class\",\n {\"name\": \"FirstName\"},\n [\n \"field\",\n {\"name\": \"text\"},\n [\n \"bind-xml\",\n {\n \"node\": \"text\",\n \"name\": \"text\"\n }\n ]\n ]\n ],\n [\n \"class\",\n {\"name\": \"MI\"},\n [\n \"field\",\n {\"name\": \"text\"},\n [\n \"bind-xml\",\n {\n \"node\": \"text\",\n \"name\": \"text\"\n }\n ]\n ]\n ],\n [\n \"class\",\n {\"name\": \"LastName\"},\n [\n \"field\",\n {\"name\": \"text\"},\n [\n \"bind-xml\",\n {\n \"node\": \"text\",\n \"name\": \"text\"\n }\n ]\n ]\n ]\n]", + ja.toString(4)); + assertEquals("", + JSONML.toString(ja)); + + jsonobject = XML.toJSONObject("Sample BookThis is chapter 1. It is not very long or interesting.This is chapter 2. Although it is longer than chapter 1, it is not any more interesting."); + assertEquals("{\"Book\": {\n \"Chapter\": [\n {\n \"content\": \"This is chapter 1. It is not very long or interesting.\",\n \"id\": 1\n },\n {\n \"content\": \"This is chapter 2. Although it is longer than chapter 1, it is not any more interesting.\",\n \"id\": 2\n }\n ],\n \"Author\": \"Anonymous\",\n \"Title\": \"Sample Book\"\n}}", + jsonobject.toString(2)); + assertEquals("This is chapter 1. It is not very long or interesting.1This is chapter 2. Although it is longer than chapter 1, it is not any more interesting.2AnonymousSample Book", + XML.toString(jsonobject)); + + jsonobject = XML.toJSONObject(""); + assertEquals("{\"bCard\": {\"bCard\": [\n {\n \"email\": \"khare@mci.net\",\n \"company\": \"MCI\",\n \"lastname\": \"Khare\",\n \"firstname\": \"Rohit\",\n \"homepage\": \"http://pest.w3.org/\"\n },\n {\n \"email\": \"adam@cs.caltech.edu\",\n \"company\": \"Caltech Infospheres Project\",\n \"lastname\": \"Rifkin\",\n \"firstname\": \"Adam\",\n \"homepage\": \"http://www.cs.caltech.edu/~adam/\"\n }\n]}}", + jsonobject.toString(2)); + assertEquals("khare@mci.netMCIKhareRohithttp://pest.w3.org/adam@cs.caltech.eduCaltech Infospheres ProjectRifkinAdamhttp://www.cs.caltech.edu/~adam/", + XML.toString(jsonobject)); + + jsonobject = XML.toJSONObject(" Fred fbs0001 Scerbo B "); + assertEquals("{\"customer\": {\n \"lastName\": {\"text\": \"Scerbo\"},\n \"MI\": {\"text\": \"B\"},\n \"ID\": \"fbs0001\",\n \"firstName\": {\"text\": \"Fred\"}\n}}", + jsonobject.toString(2)); + assertEquals("ScerboBfbs0001Fred", + XML.toString(jsonobject)); + + jsonobject = XML.toJSONObject("Repository Address Special Collections LibraryABC UniversityMain Library, 40 Circle DriveOurtown, Pennsylvania17654 USA"); + assertEquals("{\"list\":{\"item\":[\"Special Collections Library\",\"ABC University\",\"Main Library, 40 Circle Drive\",\"Ourtown, Pennsylvania\",\"17654 USA\"],\"head\":\"Repository Address\",\"type\":\"simple\"}}", + jsonobject.toString()); + assertEquals("Special Collections LibraryABC UniversityMain Library, 40 Circle DriveOurtown, Pennsylvania17654 USARepository Addresssimple", + XML.toString(jsonobject)); + + jsonobject = XML.toJSONObject("deluxe&"toot"&toot;Aeksbonusbonus2"); + assertEquals("{\"test\": {\n \"w\": [\n \"bonus\",\n \"bonus2\"\n ],\n \"content\": \"deluxe\",\n \"intertag\": \"\",\n \"status\": \"ok\",\n \"blip\": {\n \"content\": \"&\\\"toot\\\"&toot;A\",\n \"sweet\": true\n },\n \"empty\": \"\",\n \"zero\": 0,\n \"x\": \"eks\"\n}}", + jsonobject.toString(2)); + assertEquals("bonusbonus2deluxeok&"toot"&toot;&#x41;true0eks", + XML.toString(jsonobject)); + + jsonobject = 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"); + assertEquals("{\n \"Accept-Language\": \"en-us\",\n \"Request-URI\": \"/\",\n \"Host\": \"www.nokko.com\",\n \"Method\": \"GET\",\n \"Accept-encoding\": \"gzip, deflate\",\n \"User-Agent\": \"Mozilla/4.0 (compatible; MSIE 5.5; Windows 98; Win 9x 4.90; T312461; Q312461)\",\n \"HTTP-Version\": \"HTTP/1.0\",\n \"Connection\": \"keep-alive\",\n \"Accept\": \"image/gif, image/x-xbitmap, image/jpeg, image/pjpeg, application/vnd.ms-powerpoint, application/vnd.ms-excel, application/msword, */*\"\n}", + jsonobject.toString(2)); + assertEquals("GET \"/\" HTTP/1.0\r\n" + + "Accept-Language: en-us\r\n" + + "Host: www.nokko.com\r\n" + + "Accept-encoding: gzip, deflate\r\n" + + "User-Agent: Mozilla/4.0 (compatible; MSIE 5.5; Windows 98; Win 9x 4.90; T312461; Q312461)\r\n" + + "Connection: keep-alive\r\n" + + "Accept: image/gif, image/x-xbitmap, image/jpeg, image/pjpeg, application/vnd.ms-powerpoint, application/vnd.ms-excel, application/msword, */*\r\n\r\n", + HTTP.toString(jsonobject)); + + jsonobject = 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"); + assertEquals("{\n \"Reason-Phrase\": \"Oki Doki\",\n \"Status-Code\": \"200\",\n \"Transfer-Encoding\": \"chunked\",\n \"Date\": \"Sun, 26 May 2002 17:38:52 GMT\",\n \"Keep-Alive\": \"timeout=15, max=100\",\n \"HTTP-Version\": \"HTTP/1.1\",\n \"Content-Type\": \"text/html\",\n \"Connection\": \"Keep-Alive\",\n \"Server\": \"Apache/1.3.23 (Unix) mod_perl/1.26\"\n}", + jsonobject.toString(2)); + assertEquals("HTTP/1.1 200 Oki Doki\r\n" + + "Transfer-Encoding: chunked\r\n" + + "Date: Sun, 26 May 2002 17:38:52 GMT\r\n" + + "Keep-Alive: timeout=15, max=100\r\n" + + "Content-Type: text/html\r\n" + + "Connection: Keep-Alive\r\n" + + "Server: Apache/1.3.23 (Unix) mod_perl/1.26\r\n\r\n", + HTTP.toString(jsonobject)); + + jsonobject = new JSONObject("{nix: null, nux: false, null: 'null', 'Request-URI': '/', Method: 'GET', 'HTTP-Version': 'HTTP/1.0'}"); + assertEquals("{\n \"Request-URI\": \"/\",\n \"nix\": null,\n \"nux\": false,\n \"Method\": \"GET\",\n \"HTTP-Version\": \"HTTP/1.0\",\n \"null\": \"null\"\n}", + jsonobject.toString(2)); + assertTrue(jsonobject.isNull("nix")); + assertTrue(jsonobject.has("nix")); + assertEquals("/nullfalseGETHTTP/1.0null", + XML.toString(jsonobject)); + + jsonobject = XML.toJSONObject("" + "\n\n" + "" + + "" + + "GOOGLEKEY '+search+' 0 10 true false latin1 latin1" + + "" + + ""); + + assertEquals("{\"SOAP-ENV:Envelope\": {\n \"SOAP-ENV:Body\": {\"ns1:doGoogleSearch\": {\n \"oe\": {\n \"content\": \"latin1\",\n \"xsi:type\": \"xsd:string\"\n },\n \"SOAP-ENV:encodingStyle\": \"http://schemas.xmlsoap.org/soap/encoding/\",\n \"lr\": {\"xsi:type\": \"xsd:string\"},\n \"start\": {\n \"content\": 0,\n \"xsi:type\": \"xsd:int\"\n },\n \"q\": {\n \"content\": \"'+search+'\",\n \"xsi:type\": \"xsd:string\"\n },\n \"ie\": {\n \"content\": \"latin1\",\n \"xsi:type\": \"xsd:string\"\n },\n \"safeSearch\": {\n \"content\": false,\n \"xsi:type\": \"xsd:boolean\"\n },\n \"xmlns:ns1\": \"urn:GoogleSearch\",\n \"restrict\": {\"xsi:type\": \"xsd:string\"},\n \"filter\": {\n \"content\": true,\n \"xsi:type\": \"xsd:boolean\"\n },\n \"maxResults\": {\n \"content\": 10,\n \"xsi:type\": \"xsd:int\"\n },\n \"key\": {\n \"content\": \"GOOGLEKEY\",\n \"xsi:type\": \"xsd:string\"\n }\n }},\n \"xmlns:xsd\": \"http://www.w3.org/1999/XMLSchema\",\n \"xmlns:xsi\": \"http://www.w3.org/1999/XMLSchema-instance\",\n \"xmlns:SOAP-ENV\": \"http://schemas.xmlsoap.org/soap/envelope/\"\n}}", + jsonobject.toString(2)); + + assertEquals("latin1xsd:stringhttp://schemas.xmlsoap.org/soap/encoding/xsd:string0xsd:int'+search+'xsd:stringlatin1xsd:stringfalsexsd:booleanurn:GoogleSearchxsd:stringtruexsd:boolean10xsd:intGOOGLEKEYxsd:stringhttp://www.w3.org/1999/XMLSchemahttp://www.w3.org/1999/XMLSchema-instancehttp://schemas.xmlsoap.org/soap/envelope/", + XML.toString(jsonobject)); + + jsonobject = 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\"}}}}"); + assertEquals("{\"Envelope\": {\"Body\": {\"ns1:doGoogleSearch\": {\n \"oe\": \"latin1\",\n \"SOAP-ENV:encodingStyle\": \"http://schemas.xmlsoap.org/soap/encoding/\",\n \"start\": 0,\n \"q\": \"'+search+'\",\n \"ie\": \"latin1\",\n \"safeSearch\": false,\n \"xmlns:ns1\": \"urn:GoogleSearch\",\n \"maxResults\": 10,\n \"key\": \"GOOGLEKEY\",\n \"filter\": true\n}}}}", + jsonobject.toString(2)); + assertEquals("latin1http://schemas.xmlsoap.org/soap/encoding/0'+search+'latin1falseurn:GoogleSearch10GOOGLEKEYtrue", + XML.toString(jsonobject)); + + jsonobject = CookieList.toJSONObject(" f%oo = b+l=ah ; o;n%40e = t.wo "); + assertEquals("{\n \"o;n@e\": \"t.wo\",\n \"f%oo\": \"b l=ah\"\n}", + jsonobject.toString(2)); + assertEquals("o%3bn@e=t.wo;f%25oo=b l%3dah", + CookieList.toString(jsonobject)); + + jsonobject = Cookie.toJSONObject("f%oo=blah; secure ;expires = April 24, 2002"); + assertEquals("{\n" + + " \"expires\": \"April 24, 2002\",\n" + + " \"name\": \"f%oo\",\n" + + " \"secure\": true,\n" + + " \"value\": \"blah\"\n" + + "}", jsonobject.toString(2)); + assertEquals("f%25oo=blah;expires=April 24, 2002;secure", + Cookie.toString(jsonobject)); + + jsonobject = new JSONObject("{script: 'It is not allowed in HTML to send a close script tag in a stringso we insert a backslash before the /'}"); + assertEquals("{\"script\":\"It is not allowed in HTML to send a close script tag in a stringso we insert a backslash before the /'}"); + assertEquals( + "{\"script\":\"It is not allowed in HTML to send a close script tag in a string', backslash:'\\\\', ei: {quotes: '\"\\''},eo: {a: '\"quoted\"', b:\"don't\"}, quotes: [\"'\", '\"']}"); + assertEquals( + "{\n \"quotes\": [\n \"'\",\n \"\\\"\"\n ],\n \"slashes\": \"///\",\n \"ei\": {\"quotes\": \"\\\"'\"},\n \"eo\": {\n \"b\": \"don't\",\n \"a\": \"\\\"quoted\\\"\"\n },\n \"closetag\": \"<\\/script>\",\n \"backslash\": \"\\\\\"\n}", + jsonobject.toString(2)); + assertEquals( + "'"///"'don't"quoted"</script>\\", + XML.toString(jsonobject)); + } catch (JSONException e) + { + fail(e.getMessage()); + } + } + + /** + * Tests the toJsonObject method using json information. + */ + public void testToJsonObject_JsonInformation() + { + try + { + string = "First \u0009<content> This is \"content\". 3 JSON does not preserve the sequencing of elements and contents. III T H R E EContent text is an implied structure in XML. JSON does not have implied structure:7everything is explicit.!]]>"; + jsonobject = XML.toJSONObject(string); + assertEquals( + "{\"xml\": {\n \"content\": [\n \"First \\t\",\n \"This is \\\"content\\\".\",\n \"JSON does not preserve the sequencing of elements and contents.\",\n \"Content text is an implied structure in XML.\",\n \"JSON does not have implied structure:\",\n \"everything is explicit.\",\n \"CDATA blocks!\"\n ],\n \"two\": \" \\\"2\\\" \",\n \"seven\": 7,\n \"five\": [\n \"\",\n \"\"\n ],\n \"one\": 1,\n \"three\": [\n 3,\n \"III\",\n \"T H R E E\"\n ],\n \"four\": \"\",\n \"six\": {\"content\": 6}\n}}", + jsonobject.toString(2)); + assertEquals( + "First \t<content>\n" + + "This is "content".\n" + + "JSON does not preserve the sequencing of elements and contents.\n" + + "Content text is an implied structure in XML.\n" + + "JSON does not have implied structure:\n" + + "everything is explicit.\n" + + "CDATA blocks<are><supported>! "2" 713IIIT H R E E6", + XML.toString(jsonobject)); + } catch (JSONException e) + { + fail(e.getMessage()); + } + } + + /** + * Tests the toString method using json array of int array. + */ + public void testToString_JsonArrayOfIntArray() + { + try + { + int ar[] = + { 1, 2, 3 }; + jsonarray = new JSONArray(ar); + assertEquals("[1,2,3]", jsonarray.toString()); + assertEquals("123", + XML.toString(ar)); + } catch (JSONException e) + { + fail(e.getMessage()); + } + } + + /** + * Tests the toString method using table mapping. + */ + public void testToString_TableMapping() + { + try + { + string = " "; + jsonobject = XML.toJSONObject(string); + + assertEquals( + "{\"mapping\": {\n \"empty\": \"\",\n \"class\": [\n {\n \"field\": [\n {\n \"bind-xml\": {\n \"node\": \"attribute\",\n \"name\": \"ID\"\n },\n \"name\": \"ID\",\n \"type\": \"string\"\n },\n {\n \"name\": \"FirstName\",\n \"type\": \"FirstName\"\n },\n {\n \"name\": \"MI\",\n \"type\": \"MI\"\n },\n {\n \"name\": \"LastName\",\n \"type\": \"LastName\"\n }\n ],\n \"name\": \"Customer\"\n },\n {\n \"field\": {\n \"bind-xml\": {\n \"node\": \"text\",\n \"name\": \"text\"\n },\n \"name\": \"text\"\n },\n \"name\": \"FirstName\"\n },\n {\n \"field\": {\n \"bind-xml\": {\n \"node\": \"text\",\n \"name\": \"text\"\n },\n \"name\": \"text\"\n },\n \"name\": \"MI\"\n },\n {\n \"field\": {\n \"bind-xml\": {\n \"node\": \"text\",\n \"name\": \"text\"\n },\n \"name\": \"text\"\n },\n \"name\": \"LastName\"\n }\n ]\n}}", + jsonobject.toString(2)); + assertEquals( + "attributeIDIDstringFirstNameFirstNameMIMILastNameLastNameCustomertexttexttextFirstNametexttexttextMItexttexttextLastName", + XML.toString(jsonobject)); + } catch (JSONException e) + { + fail(e.getMessage()); + } + } + + /** + * Tests the toString method using book info. + */ + public void testToString_BookInfo() + { + try + { + jsonobject = XML + .toJSONObject("Sample BookThis is chapter 1. It is not very long or interesting.This is chapter 2. Although it is longer than chapter 1, it is not any more interesting."); + assertEquals( + "{\"Book\": {\n \"Chapter\": [\n {\n \"content\": \"This is chapter 1. It is not very long or interesting.\",\n \"id\": 1\n },\n {\n \"content\": \"This is chapter 2. Although it is longer than chapter 1, it is not any more interesting.\",\n \"id\": 2\n }\n ],\n \"Author\": \"Anonymous\",\n \"Title\": \"Sample Book\"\n}}", + jsonobject.toString(2)); + assertEquals( + "This is chapter 1. It is not very long or interesting.1This is chapter 2. Although it is longer than chapter 1, it is not any more interesting.2AnonymousSample Book", + XML.toString(jsonobject)); + } catch (JSONException e) + { + fail(e.getMessage()); + } + } + + /** + * Tests the toJsonObject method using table of contents. + */ + public void testToJsonObject_TableOfContents() + { + try + { + string = "Content of the first chapterContent of the second chapter Content of the first subchapter Content of the second subchapterThird Chapter"; + jsonobject = XML.toJSONObject(string); + assertEquals( + "{\"book\": {\"chapter\": [\n \"Content of the first chapter\",\n {\n \"content\": \"Content of the second chapter\",\n \"chapter\": [\n \"Content of the first subchapter\",\n \"Content of the second subchapter\"\n ]\n },\n \"Third Chapter\"\n]}}", + jsonobject.toString(1)); + assertEquals( + "Content of the first chapterContent of the second chapterContent of the first subchapterContent of the second subchapterThird Chapter", + XML.toString(jsonobject)); + } catch (JSONException e) + { + fail(e.getMessage()); + } + } + + /** + * Tests the toJsonObject method using simple xml. + */ + public void testToJsonObject_SimpleXml() + { + try + { + string = "122333"; + jsonobject = XML.toJSONObject(string); + assertEquals("{\"xml\": {\n" + " \"a\": [\n" + " \"\",\n" + + " 1,\n" + " 22,\n" + " 333\n" + + " ],\n" + " \"empty\": \"\"\n" + "}}", + jsonobject.toString(4)); + assertEquals("122333", + XML.toString(jsonobject)); + } catch (JSONException e) + { + fail(e.getMessage()); + } + } + + /** + * Tests the toJsonObject method using html escapes. + */ + public void testToJsonObject_HtmlEscapes() + { + try + { + jsonobject = XML + .toJSONObject("deluxe&"toot"&toot;Aeksbonusbonus2"); + assertEquals( + "{\"test\": {\n \"w\": [\n \"bonus\",\n \"bonus2\"\n ],\n \"content\": \"deluxe\",\n \"intertag\": \"\",\n \"status\": \"ok\",\n \"blip\": {\n \"content\": \"&\\\"toot\\\"&toot;A\",\n \"sweet\": true\n },\n \"empty\": \"\",\n \"zero\": 0,\n \"x\": \"eks\"\n}}", + jsonobject.toString(2)); + assertEquals( + "bonusbonus2deluxeok&"toot"&toot;&#x41;true0eks", + XML.toString(jsonobject)); + } catch (JSONException e) + { + fail(e.getMessage()); + } + } + + /** + * Tests the toJsonObject method using phone book. + */ + public void testToJsonObject_PhoneBook() + { + try + { + jsonobject = XML + .toJSONObject(""); + assertEquals( + "{\"bCard\": {\"bCard\": [\n {\n \"email\": \"khare@mci.net\",\n \"company\": \"MCI\",\n \"lastname\": \"Khare\",\n \"firstname\": \"Rohit\",\n \"homepage\": \"http://pest.w3.org/\"\n },\n {\n \"email\": \"adam@cs.caltech.edu\",\n \"company\": \"Caltech Infospheres Project\",\n \"lastname\": \"Rifkin\",\n \"firstname\": \"Adam\",\n \"homepage\": \"http://www.cs.caltech.edu/~adam/\"\n }\n]}}", + jsonobject.toString(2)); + assertEquals( + "khare@mci.netMCIKhareRohithttp://pest.w3.org/adam@cs.caltech.eduCaltech Infospheres ProjectRifkinAdamhttp://www.cs.caltech.edu/~adam/", + XML.toString(jsonobject)); + } catch (JSONException e) + { + fail(e.getMessage()); + } + } + + /** + * Tests the toJsonObject method using customer info. + */ + public void testToJsonObject_CustomerInfo() + { + try + { + jsonobject = XML + .toJSONObject(" Fred fbs0001 Scerbo B "); + assertEquals( + "{\"customer\": {\n \"lastName\": {\"text\": \"Scerbo\"},\n \"MI\": {\"text\": \"B\"},\n \"ID\": \"fbs0001\",\n \"firstName\": {\"text\": \"Fred\"}\n}}", + jsonobject.toString(2)); + assertEquals( + "ScerboBfbs0001Fred", + XML.toString(jsonobject)); + } catch (JSONException e) + { + fail(e.getMessage()); + } + } + + /** + * Tests the toJsonObject method using library catalog. + */ + public void testToJsonObject_LibraryCatalog() + { + try + { + jsonobject = XML + .toJSONObject("Repository Address Special Collections LibraryABC UniversityMain Library, 40 Circle DriveOurtown, Pennsylvania17654 USA"); + assertEquals( + "{\"list\":{\"item\":[\"Special Collections Library\",\"ABC University\",\"Main Library, 40 Circle Drive\",\"Ourtown, Pennsylvania\",\"17654 USA\"],\"head\":\"Repository Address\",\"type\":\"simple\"}}", + jsonobject.toString()); + assertEquals( + "Special Collections LibraryABC UniversityMain Library, 40 Circle DriveOurtown, Pennsylvania17654 USARepository Addresssimple", + XML.toString(jsonobject)); + } catch (JSONException e) + { + fail(e.getMessage()); + } + } + + /** + * Tests the toString method using xml within xml. + */ + public void testToString_XmlWithinXml() + { + try{ + jsonarray = new JSONArray( + " [\"\", next is an implied null , , ok,] "); + assertEquals("[\"\",\"next is an implied null\",null,\"ok\"]", + jsonarray.toString()); + assertEquals( + "<escape>next is an implied nullnullok", + XML.toString(jsonarray)); + } catch (JSONException e) + { + fail(e.getMessage()); + } + } + + /** + * Tests the toString method using email. + */ + public void testToString_Email() + { + try{ + jsonobject = 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\"}}}}"); + assertEquals( + "{\"Envelope\": {\"Body\": {\"ns1:doGoogleSearch\": {\n \"oe\": \"latin1\",\n \"SOAP-ENV:encodingStyle\": \"http://schemas.xmlsoap.org/soap/encoding/\",\n \"start\": 0,\n \"q\": \"'+search+'\",\n \"ie\": \"latin1\",\n \"safeSearch\": false,\n \"xmlns:ns1\": \"urn:GoogleSearch\",\n \"maxResults\": 10,\n \"key\": \"GOOGLEKEY\",\n \"filter\": true\n}}}}", + jsonobject.toString(2)); + assertEquals( + "latin1http://schemas.xmlsoap.org/soap/encoding/0'+search+'latin1falseurn:GoogleSearch10GOOGLEKEYtrue", + XML.toString(jsonobject)); + } catch (JSONException e) + { + fail(e.getMessage()); + } + } + + /** + * Tests the toString method using http header. + */ + public void testToString_HttpHeader() + { + try{ + jsonobject = new JSONObject( + "{nix: null, nux: false, null: 'null', 'Request-URI': '/', Method: 'GET', 'HTTP-Version': 'HTTP/1.0'}"); + assertEquals( + "{\n \"Request-URI\": \"/\",\n \"nix\": null,\n \"nux\": false,\n \"Method\": \"GET\",\n \"HTTP-Version\": \"HTTP/1.0\",\n \"null\": \"null\"\n}", + jsonobject.toString(2)); + assertTrue(jsonobject.isNull("nix")); + assertTrue(jsonobject.has("nix")); + assertEquals( + "/nullfalseGETHTTP/1.0null", + XML.toString(jsonobject)); + } catch (JSONException e) + { + fail(e.getMessage()); + } + } + +} diff --git a/test/java/org/json/TestXMLTokener.java b/test/java/org/json/TestXMLTokener.java new file mode 100644 index 000000000..e4766ccbb --- /dev/null +++ b/test/java/org/json/TestXMLTokener.java @@ -0,0 +1,473 @@ +/* + * File: TestXMLTokener.java + * Author: JSON.org + */ +package org.json; + +import org.json.JSONException; +import org.json.XMLTokener; + +import junit.framework.TestCase; + +/** + * The Class TestXMLTokener. + */ +public class TestXMLTokener extends TestCase +{ + + /** The xmltokener. */ + private XMLTokener xmltokener; + + /** + * Tests the nextContent method. + */ + public void testNextContent() + { + try + { + xmltokener = new XMLTokener("< abc>"); + assertEquals('<', xmltokener.nextContent()); + assertEquals("abc>", xmltokener.nextContent()); + assertEquals('<', xmltokener.nextContent()); + assertEquals("de f/>", xmltokener.nextContent()); + assertEquals('<', xmltokener.nextContent()); + assertEquals("/abc>", xmltokener.nextContent()); + assertEquals(null, xmltokener.nextContent()); + } catch (JSONException e) + { + fail(e.getMessage()); + } + } + + /** + * Tests the nextCdata method. + */ + public void testNextCdata() + { + try + { + xmltokener = new XMLTokener("<[CDATA[]]>"); + assertEquals('<', xmltokener.next('<')); + assertEquals('[', xmltokener.next('[')); + assertEquals("CDATA", xmltokener.nextToken()); + assertEquals('[', xmltokener.next('[')); + assertEquals("", xmltokener.nextCDATA()); + } catch (JSONException e) + { + fail(e.getMessage()); + } + } + + /** + * Tests the nextCdata method using broken cdata. + */ + public void testNextCdata_BrokenCdata1() + { + try + { + xmltokener = new XMLTokener("<[CDATA[]>"); + assertEquals('<', xmltokener.next('<')); + assertEquals('[', xmltokener.next('[')); + assertEquals("CDATA", xmltokener.nextToken()); + assertEquals('[', xmltokener.next('[')); + xmltokener.nextCDATA(); + fail("Should have thrown exception."); + } catch (JSONException e) + { + assertEquals("Unclosed CDATA at 22 [character 23 line 1]", e.getMessage()); + } + } + + /** + * Tests the nextCdata method using broken cdata. + */ + public void testNextCdata_BrokenCdata2() + { + try + { + xmltokener = new XMLTokener("<[CDATA[]]"); + assertEquals('<', xmltokener.next('<')); + assertEquals('[', xmltokener.next('[')); + assertEquals("CDATA", xmltokener.nextToken()); + assertEquals('[', xmltokener.next('[')); + xmltokener.nextCDATA(); + fail("Should have thrown exception."); + } catch (JSONException e) + { + assertEquals("Unclosed CDATA at 22 [character 23 line 1]", e.getMessage()); + } + } + + /** + * Tests the nextCdata method using broken cdata. + */ + public void testNextCdata_BrokenCdata3() + { + try + { + xmltokener = new XMLTokener("<[CDATA[]]"); + assertEquals('<', xmltokener.next('<')); + assertEquals('[', xmltokener.next('[')); + assertEquals("CDATA", xmltokener.nextToken()); + assertEquals('[', xmltokener.next('[')); + xmltokener.nextCDATA(); + fail("Should have thrown exception."); + } catch (JSONException e) + { + assertEquals("Unclosed CDATA at 22 [character 23 line 1]", e.getMessage()); + } + } + + /** + * Tests the nextCdata method using broken cdata. + */ + public void testNextCdata_BrokenCdata4() + { + try + { + xmltokener = new XMLTokener("<[CDATA["); + assertEquals('<', xmltokener.next('<')); + assertEquals('[', xmltokener.next('[')); + assertEquals("CDATA", xmltokener.nextToken()); + assertEquals('[', xmltokener.next('[')); + xmltokener.nextCDATA(); + fail("Should have thrown exception."); + } catch (JSONException e) + { + assertEquals("Unclosed CDATA at 15 [character 16 line 1]", e.getMessage()); + } + } + + /** + * Tests the nextEntity method using ampersand. + */ + public void testNextEntity_Ampersand() + { + try + { + xmltokener = new XMLTokener("<&>"); + assertEquals('<', xmltokener.next('<')); + assertEquals('&', xmltokener.next('&')); + assertEquals('&', xmltokener.nextEntity('&')); + } catch (JSONException e) + { + fail(e.getMessage()); + } + } + + /** + * Tests the nextEntity method using number entity. + */ + public void testNextEntity_NumberEntity() + { + try + { + xmltokener = new XMLTokener("<<>"); + assertEquals('<', xmltokener.next('<')); + assertEquals('&', xmltokener.next('&')); + assertEquals("<", xmltokener.nextEntity('&')); + } catch (JSONException e) + { + fail(e.getMessage()); + } + } + + /** + * Tests the nextEntity method using broken entity. + */ + public void testNextEntity_BrokenEntity() + { + try + { + xmltokener = new XMLTokener("< "); + assertEquals('<', xmltokener.next('<')); + assertEquals('&', xmltokener.next('&')); + assertEquals("<", xmltokener.nextEntity('&')); + fail("Should have thrown exception."); + } catch (JSONException e) + { + assertEquals("Missing ';' in XML entity:   at 7 [character 8 line 1]", e.getMessage()); + } + } + + /** + * Tests the nextMeta method using string. + */ + public void testNextMeta_String() + { + try + { + xmltokener = new XMLTokener(""); + assertEquals('<', xmltokener.next('<')); + assertEquals('!', xmltokener.next('!')); + assertEquals(true, xmltokener.nextMeta()); + } catch (JSONException e) + { + fail(e.getMessage()); + } + } + + /** + * Tests the nextMeta method using open string. + */ + public void testNextMeta_OpenString() + { + try + { + xmltokener = new XMLTokener(""); + assertEquals('<', xmltokener.next('<')); + assertEquals('!', xmltokener.next('!')); + xmltokener.nextMeta(); + fail("Should have thrown exception."); + } catch (JSONException e) + { + assertEquals("Unterminated string at 16 [character 17 line 1]", e.getMessage()); + } + } + + /** + * Tests the nextMeta method using symbols. + */ + public void testNextMeta_Symbols() + { + try + { + xmltokener = new XMLTokener("/=!?>"); + assertEquals('<', xmltokener.next('<')); + assertEquals('!', xmltokener.next('!')); + assertEquals('<', xmltokener.nextMeta()); + assertEquals('>', xmltokener.nextMeta()); + assertEquals('/', xmltokener.nextMeta()); + assertEquals('=', xmltokener.nextMeta()); + assertEquals('!', xmltokener.nextMeta()); + assertEquals('?', xmltokener.nextMeta()); + } catch (JSONException e) + { + fail(e.getMessage()); + } + } + + /** + * Tests the nextMeta method using misshaped. + */ + public void testNextMeta_Misshaped() + { + try + { + xmltokener = new XMLTokener(""); + assertEquals('<', xmltokener.next('<')); + assertEquals("da", xmltokener.nextToken()); + assertEquals("ta", xmltokener.nextToken()); + assertEquals('>', xmltokener.nextToken()); + } catch (JSONException e) + { + fail(e.getMessage()); + } + } + + /** + * Tests the nextToken method using tag with bad character. + */ + public void testNextToken_TagWithBadCharacter() + { + try + { + xmltokener = new XMLTokener(""); + assertEquals('<', xmltokener.next('<')); + xmltokener.nextToken(); + fail("Should have thrown exception."); + } catch (JSONException e) + { + assertEquals("Bad character in a name at 4 [character 5 line 1]", e.getMessage()); + } + } + + /** + * Tests the nextToken method using tag with misplaced less than. + */ + public void testNextToken_TagWithMisplacedLessThan() + { + try + { + xmltokener = new XMLTokener("<"); + assertEquals('<', xmltokener.next('<')); + xmltokener.nextToken(); + fail("Should have thrown exception."); + } catch (JSONException e) + { + assertEquals("Misplaced '<' at 2 [character 3 line 1]", e.getMessage()); + } + } + + /** + * Tests the nextToken method using misshaped element. + */ + public void testNextToken_MisshapedElement() + { + try + { + xmltokener = new XMLTokener("<"); + assertEquals('<', xmltokener.next('<')); + xmltokener.nextToken(); + fail("Should have thrown exception."); + } catch (JSONException e) + { + assertEquals("Misshaped element at 2 [character 3 line 1]", e.getMessage()); + } + } + + /** + * Tests the nextToken method using symbols. + */ + public void testNextToken_Symbols() + { + try + { + xmltokener = new XMLTokener("< /=!?"); + assertEquals('<', xmltokener.next('<')); + assertEquals('/', xmltokener.nextToken()); + assertEquals('=', xmltokener.nextToken()); + assertEquals('!', xmltokener.nextToken()); + assertEquals('?', xmltokener.nextToken()); + } catch (JSONException e) + { + fail(e.getMessage()); + } + } + + /** + * Tests the nextToken method using string. + */ + public void testNextToken_String() + { + try + { + xmltokener = new XMLTokener("<\"abc&123\">"); + assertEquals('<', xmltokener.next('<')); + assertEquals("abc&123", xmltokener.nextToken()); + } catch (JSONException e) + { + fail(e.getMessage()); + } + } + + /** + * Tests the nextToken method using no greater than. + */ + public void testNextToken_NoGreaterThan() + { + try + { + xmltokener = new XMLTokener(""); + assertEquals('<', xmltokener.next('<')); + xmltokener.nextToken(); + fail("Should have thrown exception."); + } catch (JSONException e) + { + assertEquals("Unterminated string at 10 [character 11 line 1]", e.getMessage()); + } + } + + /** + * Tests the skipTo method. + */ + public void testSkipTo() + { + try + { + xmltokener = new XMLTokener(""); + assertEquals('<', xmltokener.next('<')); + assertEquals(true, xmltokener.skipPast("c1")); + assertEquals('2', xmltokener.next('2')); + assertEquals(false, xmltokener.skipPast("b1")); + } catch (JSONException e) + { + fail(e.getMessage()); + } + } + + /** + * Tests the skipTo method using long parameter. + */ + public void testSkipTo_LongParameter() + { + try + { + xmltokener = new XMLTokener(""); + assertEquals('<', xmltokener.next('<')); + assertEquals(false, xmltokener.skipPast("abcdefghi")); + } catch (JSONException e) + { + fail(e.getMessage()); + } + } + +} \ No newline at end of file diff --git a/version.properties b/version.properties new file mode 100644 index 000000000..c3b537092 --- /dev/null +++ b/version.properties @@ -0,0 +1 @@ +version = 1.0.4