keys()
+ {
+ return getMap().keySet().iterator();
+ }
+
+ /**
+ * Get the number of keys stored in the JSONObject.
+ *
+ * @return The number of keys in the JSONObject.
+ */
+ @Override
+ public final int length()
+ {
+ return getMap().size();
+ }
+
+ /**
+ * Get an optional boolean associated with a key. It returns false if there is no such key, or if
+ * the value is not Boolean.TRUE or the String "true".
+ *
+ * @param key
+ * A key string.
+ * @return The truth.
+ */
+ @Override
+ public final boolean optBoolean(String key)
+ {
+ return optBoolean(key, false);
+ }
+
+ /**
+ * Get an optional boolean associated with a key. It returns the defaultValue if there is no such
+ * key, or if it is not a Boolean or the String "true" or "false" (case insensitive).
+ *
+ * @param key
+ * A key string.
+ * @param defaultValue
+ * The default.
+ * @return The truth.
+ */
+ @Override
+ public final boolean optBoolean(String key,
+ boolean defaultValue)
+ {
+ try {
+ return getBoolean(key);
+ } catch (Exception e) {
+ return defaultValue;
+ }
+ }
+
+ /**
+ * Get an optional double associated with a key, or NaN if there is no such key or if its value is
+ * not a number. If the value is a string, an attempt will be made to evaluate it as a number.
+ *
+ * @param key
+ * A string which is the key.
+ * @return An object which is the value.
+ */
+ @Override
+ public final double optDouble(String key)
+ {
+ return optDouble(key, Double.NaN);
+ }
+
+ /**
+ * Get an optional double associated with a key, or the defaultValue if there is no such key or if
+ * its value is not a number. If the value is a string, an attempt will be made to evaluate it as
+ * a number.
+ *
+ * @param key
+ * A key string.
+ * @param defaultValue
+ * The default.
+ * @return An object which is the value.
+ */
+ @Override
+ public final double optDouble(String key,
+ double defaultValue)
+ {
+ try {
+ return getDouble(key);
+ } catch (Exception e) {
+ return defaultValue;
+ }
+ }
+
+ /**
+ * Get an optional int value associated with a key, or zero if there is no such key or if the
+ * value is not a number. If the value is a string, an attempt will be made to evaluate it as a
+ * number.
+ *
+ * @param key
+ * A key string.
+ * @return An object which is the value.
+ */
+ @Override
+ public final int optInt(String key)
+ {
+ return optInt(key, 0);
+ }
+
+ /**
+ * Get an optional int value associated with a key, or the default if there is no such key or if
+ * the value is not a number. If the value is a string, an attempt will be made to evaluate it as
+ * a number.
+ *
+ * @param key
+ * A key string.
+ * @param defaultValue
+ * The default.
+ * @return An object which is the value.
+ */
+ @Override
+ public final int optInt(String key,
+ int defaultValue)
+ {
+ try {
+ return getInt(key);
+ } catch (Exception e) {
+ return defaultValue;
+ }
+ }
+
+ @Override
+ public final Integer optInteger(String key,
+ Integer defaultValue)
+ {
+ Object object = opt(key);
+ if (object == null) {
+ return defaultValue;
+ }
+ if (object instanceof Integer) {
+ return (Integer) object;
+ }
+ if (object instanceof Number) {
+ return Integer.valueOf(((Number) object).intValue());
+ }
+ try {
+ Integer.parseInt(object.toString());
+ } catch (Exception e) {
+ }
+ return defaultValue;
+ }
+
+ @Override
+ public final Integer optInteger(String key)
+ {
+ return optInteger(key, null);
+ }
+
+ /**
+ * Get an optional long value associated with a key, or zero if there is no such key or if the
+ * value is not a number. If the value is a string, an attempt will be made to evaluate it as a
+ * number.
+ *
+ * @param key
+ * A key string.
+ * @return An object which is the value.
+ */
+ @Override
+ public final long optLong(String key)
+ {
+ return optLong(key, 0);
+ }
+
+ /**
+ * Get an optional long value associated with a key, or the default if there is no such key or if
+ * the value is not a number. If the value is a string, an attempt will be made to evaluate it as
+ * a number.
+ *
+ * @param key
+ * A key string.
+ * @param defaultValue
+ * The default.
+ * @return An object which is the value.
+ */
+ @Override
+ public final long optLong(String key,
+ long defaultValue)
+ {
+ try {
+ return getLong(key);
+ } catch (Exception e) {
+ return defaultValue;
+ }
+ }
+
+ /**
+ * Get an optional string associated with a key. It returns an empty string if there is no such
+ * key. If the value is not a string and is not null, then it is converted to a string.
+ *
+ * @param key
+ * A key string.
+ * @return A string which is the value.
+ */
+ @Override
+ public final String optString(String key)
+ {
+ return optString(key, "");
+ }
+
+ /**
+ * Get an optional string associated with a key. It returns the defaultValue if there is no such
+ * key.
+ *
+ * @param key
+ * A key string.
+ * @param defaultValue
+ * The default.
+ * @return A string which is the value.
+ */
+ @Override
+ public final String optString(String key,
+ String defaultValue)
+ {
+ Object object = opt(key);
+ return Null.getInstance().equals(object) ? defaultValue : object.toString();
+ }
+
+ /**
+ * Make a JSON text of this JSONObject. For compactness, no whitespace is added. If this would not
+ * result in a syntactically correct JSON text, then null will be returned instead.
+ *
+ * Warning: This method assumes that the data structure is acyclical.
+ *
+ * @return a printable, displayable, portable, transmittable representation of the object,
+ * beginning with { (left brace) and ending with
+ * } (right brace).
+ */
+ @Override
+ public final String toString()
+ {
+ return JSONObjects.toString(this);
+ }
+
+ /**
+ * Make a prettyprinted JSON text of this JSONObject.
+ *
+ * Warning: This method assumes that the data structure is acyclical.
+ *
+ * @param indentFactor
+ * The number of spaces to add to each level of indentation.
+ * @return a printable, displayable, portable, transmittable representation of the object,
+ * beginning with { (left brace) and ending with
+ * } (right brace).
+ */
+ @Override
+ public final String toString(int indentFactor)
+ {
+ return toString(indentFactor, 0);
+ }
+
+ /**
+ * Make a prettyprinted JSON text of this JSONObject.
+ *
+ * Warning: This method assumes that the data structure is acyclical.
+ *
+ * @param indentFactor
+ * The number of spaces to add to each level of indentation.
+ * @param indent
+ * The indentation of the top level.
+ * @return a printable, displayable, transmittable representation of the object, beginning with
+ * { (left brace) and ending with }
+ * (right brace).
+ * @throws RuntimeException
+ * If the object contains an invalid number.
+ */
+ @Override
+ public final String toString(int indentFactor,
+ int indent)
+ {
+ return JSONObjects.toString(this, indentFactor, indent);
+ }
+
+ @Override
+ public final Appendable write(Appendable buf)
+ throws IOException
+ {
+ return JSONObjects.write(this, buf);
+ }
+
+ /**
+ * Get an optional JSONArray associated with a key. It returns null if there is no such key, or if
+ * its value is not a JSONArray.
+ *
+ * @param key
+ * A key string.
+ * @return A JSONArray which is the value.
+ */
+ @Override
+ public JSONArray optJSONArray(String key)
+ {
+ Object o = opt(key);
+ return o instanceof JSONArray ? (JSONArray) o : null;
+ }
+
+ /**
+ * Get an optional JSONObject associated with a key. It returns null if there is no such key, or
+ * if its value is not a JSONObject.
+ *
+ * @param key
+ * A key string.
+ * @return A JSONObject which is the value.
+ */
+ @Override
+ public JSONObject optJSONObject(String key)
+ {
+ Object object = opt(key);
+ return object instanceof JSONObject ? (JSONObject) object : null;
+ }
+
+ /**
+ * Get an Iterator of the keys of the JSONObject. The keys will be sorted alphabetically.
+ *
+ * @return An iterator of the keys.
+ */
+ @Override
+ public final Iterator sortedKeys()
+ {
+ Set unsortedKeys = getMap().keySet();
+ switch (unsortedKeys.size()) {
+ case 0:
+ case 1:
+ return unsortedKeys.iterator();
+ default:
+ return new TreeSet(unsortedKeys).iterator();
+ }
+ }
+
+ @Override
+ public O clone(JSONBuilder builder)
+ throws JSONException
+ {
+ return JSONObjects.clone(getMap(), builder);
+ }
+
+}
diff --git a/JSON-java/src/main/java/org/json/Null.java b/JSON-java/src/main/java/org/json/Null.java
new file mode 100644
index 000000000..116fd90ed
--- /dev/null
+++ b/JSON-java/src/main/java/org/json/Null.java
@@ -0,0 +1,62 @@
+package org.json;
+
+/**
+ * JSONObject.NULL is equivalent to the value that JavaScript calls null, whilst Java's null is
+ * equivalent to the value that JavaScript calls undefined.
+ */
+public class Null
+{
+ private static final Null INSTANCE = new Null();
+
+ public static Null getInstance()
+ {
+ return INSTANCE;
+ }
+
+ private Null()
+ {
+ }
+
+ /**
+ * There is only intended to be a single instance of the NULL object, so the clone method returns
+ * itself.
+ *
+ * @return NULL.
+ */
+ @Override
+ protected final Object clone()
+ {
+ return this;
+ }
+
+ /**
+ * A Null object is equal to the null value and to itself.
+ *
+ * @param object
+ * An object to test for nullness.
+ * @return true if the object parameter is the JSONObject.NULL object or null.
+ */
+ @Override
+ public boolean equals(Object object)
+ {
+ return object == null || object == this;
+ }
+
+ @Override
+ public int hashCode()
+ {
+ return 0;
+ }
+
+ /**
+ * Get the "null" string value.
+ *
+ * @return The string "null".
+ */
+ @Override
+ public String toString()
+ {
+ return "null";
+ }
+
+}
diff --git a/JSON-java/src/main/java/org/json/StringCharBuilder.java b/JSON-java/src/main/java/org/json/StringCharBuilder.java
new file mode 100644
index 000000000..85c689da1
--- /dev/null
+++ b/JSON-java/src/main/java/org/json/StringCharBuilder.java
@@ -0,0 +1,126 @@
+package org.json;
+
+import java.util.Arrays;
+
+/**
+ * simplified copy of StringBuilder that only supports appending characters and which also supports
+ * efficient resuse. You should invoke {@link #open()} before you start using it and
+ * {@link #close()} when you are finished with it. You cannot nest usage of a single
+ * StringCharBuilder and attempting to call {@link #open()} before it is {@link #close()}d will
+ * result in an {@link IllegalStateException}.
+ */
+public class StringCharBuilder
+ implements AutoCloseable
+{
+ public static final int UNCLAIMED = -1;
+
+ char[] value;
+ int count = UNCLAIMED;
+
+ StringCharBuilder(int capacity)
+ {
+ value = new char[capacity];
+ }
+
+ public int length()
+ {
+ return count;
+ }
+
+ public int capacity()
+ {
+ return value.length;
+ }
+
+ /**
+ * This method has the same contract as ensureCapacity, but is never synchronized.
+ */
+ private void ensureCapacityInternal(int minimumCapacity)
+ {
+ if (minimumCapacity - value.length > 0)
+ expandCapacity(minimumCapacity);
+ }
+
+ /**
+ * This implements the expansion semantics of ensureCapacity with no size check or
+ * synchronization.
+ */
+ private void expandCapacity(int minimumCapacity)
+ {
+ int newCapacity = value.length * 2 + 2;
+ if (newCapacity - minimumCapacity < 0)
+ newCapacity = minimumCapacity;
+ if (newCapacity < 0) {
+ if (minimumCapacity < 0) // overflow
+ throw new OutOfMemoryError();
+ newCapacity = Integer.MAX_VALUE;
+ }
+ value = Arrays.copyOf(value, newCapacity);
+ }
+
+ public StringCharBuilder open()
+ {
+ if (count != UNCLAIMED) {
+ throw new IllegalStateException("Cannot claim a StringCharBuilder that is in use: "
+ + toStringInternal());
+ }
+ count = 0;
+ return this;
+ }
+
+ @Override
+ public void close()
+ {
+ if (count == UNCLAIMED) {
+ throw new IllegalStateException("Cannot release a StringCharBuilder that is unclaimed");
+ }
+ count = UNCLAIMED;
+ }
+
+ private void assertIsClaimed()
+ {
+ if (count == UNCLAIMED) {
+ throw new IllegalStateException("You must claim a StringCharBuilder before using it");
+ }
+ }
+
+ public char charAt(int index)
+ {
+ assertIsClaimed();
+ if ((index < 0) || (index >= count))
+ throw new StringIndexOutOfBoundsException(index);
+ return value[index];
+ }
+
+ public void append(char c)
+ {
+ assertIsClaimed();
+ ensureCapacityInternal(count + 1);
+ value[count++] = c;
+ }
+
+ public String substring(int start,
+ int end)
+ {
+ assertIsClaimed();
+ if (start < 0)
+ throw new StringIndexOutOfBoundsException(start);
+ if (end > count)
+ throw new StringIndexOutOfBoundsException(end);
+ if (start > end)
+ throw new StringIndexOutOfBoundsException(end - start);
+ return new String(value, start, end - start);
+ }
+
+ @Override
+ public String toString()
+ {
+ return count == UNCLAIMED ? "UNCLAIMED" : toStringInternal();
+ }
+
+ private String toStringInternal()
+ {
+ return new String(value, 0, count);
+ }
+
+}
diff --git a/JSON-java/src/main/java/org/json/Test.java b/JSON-java/src/main/java/org/json/Test.java
new file mode 100755
index 000000000..bf7559870
--- /dev/null
+++ b/JSON-java/src/main/java/org/json/Test.java
@@ -0,0 +1,1416 @@
+package org.json;
+
+import java.util.Objects;
+
+import com.google.common.base.Preconditions;
+
+/*
+ * 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
+ * @author yusuke at mac.com
+ * @version 2010-12-29
+ */
+public class Test
+// extends TestCase
+{
+ // public Test(String name)
+ // {
+ // super(name);
+ // }
+ //
+ // @Override
+ // protected void setUp()
+ // throws Exception
+ // {
+ // super.setUp();
+ // }
+ //
+ // @Override
+ // protected void tearDown()
+ // throws Exception
+ // {
+ // super.tearDown();
+ // }
+
+ // public void testXML()
+ // throws Exception
+ // {
+ // WritableJSONObject 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));
+ // }
+
+ private void assertFalse(boolean v)
+ {
+ Preconditions.checkState(v == false);
+ }
+
+ private void assertTrue(boolean v)
+ {
+ Preconditions.checkState(v == true);
+ }
+
+ private void assertEquals(Object a,
+ Object b)
+ {
+ Preconditions.checkState(Objects.equals(a, b));
+ }
+
+ public void testNull()
+ throws Exception
+ {
+ WritableJSONObject jsonobject;
+
+ jsonobject = WritableJSONObject.create("{\"message\":\"null\"}");
+ System.out.println(jsonobject);
+ assertFalse(jsonobject.isNull("message"));
+ assertEquals("null", jsonobject.getString("message"));
+
+ jsonobject = WritableJSONObject.create("{\"message\":null}");
+ System.out.println(jsonobject);
+ assertTrue(jsonobject.isNull("message"));
+ assertEquals(null, jsonobject.getString("message"));
+ }
+
+ private static void testArray(String source)
+ throws JSONException
+ {
+ System.err.println(String.format("\"%s\" --> %s",
+ source,
+ ImmutableJSON.get().toJSONArray(source)));
+ }
+
+ public static void main(String[] args)
+ throws Exception
+ {
+ testArray("[]");
+ testArray("[1]");
+ testArray("[1,2]");
+ testArray("[1,2,3]");
+ testArray("[,]");
+ testArray("[,1]");
+ testArray("[1,,2]");
+ testArray("[1,]");
+
+ Test test = new Test();
+ test.testNull();
+
+ JSONObject jObj =
+ ImmutableJSON.get()
+ .toJSONObject("{\"a\":{\"b\":2, \"c\": [1,2,{\"d\":\"deep?\",\"e\":2.543}]}}");
+ System.out.println(jObj);
+ System.out.println(jObj.getClass());
+ System.out.println(jObj.getJSONObject("a").getClass());
+ jObj = WritableJSON.get().cast(jObj);
+ System.out.println(jObj);
+ System.out.println(jObj.getClass());
+ System.out.println(jObj.getJSONObject("a").getClass());
+
+ String s = "{ \"a\": \"quoted: \\\"xyz\\\"\" }";
+ System.out.println(s);
+ JSONObject o = ImmutableJSON.get().toJSONObject(s);
+ System.out.println(o);
+ System.out.println(o.getString("a"));
+ }
+
+ // public void testJSON()
+ // throws Exception
+ // {
+ // double eps = 2.220446049250313e-16;
+ // Iterator iterator;
+ // WritableJSONArray jsonarray;
+ // WritableJSONObject jsonobject;
+ // JSONStringer jsonstringer;
+ // Object object;
+ // String string;
+ //
+ // Beany beanie = new Beany("A beany object", 42, true);
+ //
+ // string = "[0.1]";
+ // jsonarray = new WritableJSONArray(string);
+ // assertEquals("[0.1]", jsonarray.toString());
+ //
+ // jsonobject = new WritableJSONObject();
+ // object = null;
+ // jsonobject.put("booga", object);
+ // jsonobject.put("wooga", JSONObject.NULL);
+ // assertEquals("{\"wooga\":null}", jsonobject.toString());
+ // assertTrue(jsonobject.isNull("booga"));
+ //
+ // jsonobject = new WritableJSONObject();
+ // 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 WritableJSONObject(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("123
456
",
+ // // 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" + " \"cook_time\": \"3 hours\",\n"
+ // // + " \"ingredient\": [\n" + " {\n" + " \"amount\": 8,\n"
+ // // + " \"content\": \"Flour\",\n" + " \"unit\": \"dL\"\n"
+ // // + " },\n" + " {\n" + " \"amount\": 10,\n"
+ // // + " \"content\": \"Yeast\",\n" + " \"unit\": \"grams\"\n"
+ // // + " },\n" + " {\n" + " \"amount\": 4,\n"
+ // // + " \"content\": \"Water\",\n" + " \"state\": \"warm\",\n"
+ // // + " \"unit\": \"dL\"\n" + " },\n" + " {\n"
+ // // + " \"amount\": 1,\n" + " \"content\": \"Salt\",\n"
+ // // + " \"unit\": \"teaspoon\"\n" + " }\n" + " ],\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" + " \"prep_time\": \"5 mins\",\n"
+ // // + " \"title\": \"Basic bread\"\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" + " \"state\": \"warm\",\n"
+ // // + " \"unit\": \"dL\"\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" + " \"childNodes\": [\n" + " {\n"
+ // + " \"childNodes\": [\n"
+ // + " \"JSONML is a transformation between\",\n"
+ // + " {\n" + " \"childNodes\": [\"JSON\"],\n"
+ // + " \"tagName\": \"b\"\n" + " },\n"
+ // + " \"and\",\n" + " {\n"
+ // + " \"childNodes\": [\"XML\"],\n"
+ // + " \"tagName\": \"b\"\n" + " },\n"
+ // + " \"that preserves ordering of document features.\"\n"
+ // + " ],\n" + " \"tagName\": \"p\"\n" + " },\n"
+ // + " {\n"
+ // + " \"childNodes\": [\"JSONML can work with JSON arrays or JSON objects.\"],\n"
+ // + " \"tagName\": \"p\"\n" + " },\n" + " {\n"
+ // + " \"childNodes\": [\n" + " \"Three\",\n"
+ // + " {\"tagName\": \"br\"},\n" + " \"little\",\n"
+ // + " {\"tagName\": \"br\"},\n" + " \"words\"\n"
+ // + " ],\n" + " \"tagName\": \"p\"\n" + " }\n"
+ // + " ],\n" + " \"class\": \"JSONML\",\n" + " \"id\": \"demo\",\n"
+ // + " \"tagName\": \"div\"\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" + " \"class\": \"JSONML\",\n"
+ // + " \"id\": \"demo\"\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 =
+ // "\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" + " \"address\": {\n" + " \"city\": \"Anytown\",\n"
+ // + " \"postalCode\": \"98765-4321\",\n" + " \"state\": \"CA\",\n"
+ // + " \"street\": \"12345 Sixth Ave\",\n" + " \"type\": \"home\"\n"
+ // + " },\n" + " \"created\": \"2006-11-11T19:23\",\n"
+ // + " \"firstName\": \"Robert\",\n" + " \"lastName\": \"Smith\",\n"
+ // + " \"modified\": \"2006-12-31T23:59\"\n" + "}}",
+ // jsonobject.toString(4));
+ //
+ // jsonobject = new WritableJSONObject(beanie);
+ // // assertEquals("{\"string\":\"A beany object\",\"BENT\":\"All uppercase
+ // // key\",\"boolean\":true,\"number\":42,\"x\":\"x\"}"
+ // // , jsonobject.toString());
+ //
+ // string =
+ // "{ \"entity\": { \"imageURL\": \"\", \"name\": \"IXXXXXXXXXXXXX\", \"id\": 12336,
+ // \"ratingCount\": null, \"averageRating\": null } }";
+ // jsonobject = new WritableJSONObject(string);
+ // assertEquals("{\"entity\": {\n" + " \"averageRating\": null,\n" + " \"id\": 12336,\n"
+ // + " \"imageURL\": \"\",\n" + " \"name\": \"IXXXXXXXXXXXXX\",\n"
+ // + " \"ratingCount\": null\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(WritableJSONObject.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" + " \"answer\": 42,\n"
+ // + " \"big\": 1.23456789E96,\n"
+ // + " \"empty-array\": [],\n"
+ // + " \"empty-object\": {},\n" + " \"false\": false,\n"
+ // + " \"long\": 9223372036854775807,\n"
+ // + " \"null\": null,\n"
+ // + " \"small\": 1.23456789E-80,\n"
+ // + " \"true\": true\n" + " },\n" + " \"two\"\n"
+ // + " ],\n" + " true\n" + " ],\n" + " 98.6,\n" + " -100,\n"
+ // + " {},\n" + " {\"one\": 1},\n" + " {\"A beany object\": 42}\n" + "]",
+ // new WritableJSONArray(jsonstringer.toString()).toString(4));
+ //
+ // int ar[] = { 1, 2, 3 };
+ // WritableJSONArray ja = new WritableJSONArray(ar);
+ // assertEquals("[1,2,3]", ja.toString());
+ //
+ // String sa[] = { "aString", "aNumber", "aBoolean" };
+ // jsonobject = new WritableJSONObject(beanie, sa);
+ // jsonobject.put("Testing JSONString interface", beanie);
+ // assertEquals("{\n" + " \"Testing JSONString interface\": {\"A beany object\":42},\n"
+ // + " \"aBoolean\": true,\n" + " \"aNumber\": 42,\n"
+ // + " \"aString\": \"A beany object\"\n" + "}",
+ // jsonobject.toString(4));
+ //
+ // jsonobject =
+ // new WritableJSONObject("{slashes: '///', closetag: '', backslash:'\\\\', ei: {quotes:
+ // '\"\\''},eo: {a: '\"quoted\"', b:\"don't\"}, quotes: [\"'\", '\"']}");
+ // assertEquals("{\n" + " \"backslash\": \"\\\\\",\n" + " \"closetag\": \"<\\/script>\",\n"
+ // + " \"ei\": {\"quotes\": \"\\\"'\"},\n" + " \"eo\": {\n"
+ // + " \"a\": \"\\\"quoted\\\"\",\n" + " \"b\": \"don't\"\n" + " },\n"
+ // + " \"quotes\": [\n" + " \"'\",\n" + " \"\\\"\"\n" + " ],\n"
+ // + " \"slashes\": \"///\"\n" + "}",
+ // jsonobject.toString(2));
+ // assertEquals("'"///"'don't"quoted"</script>\\",
+ // XML.toString(jsonobject));
+ //
+ // jsonobject =
+ // new WritableJSONObject("{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 WritableJSONObject());
+ // jsonobject.put("JSONArray", new WritableJSONArray());
+ // 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 WritableJSONArray());
+ // jsonarray.put(new WritableJSONObject());
+ // jsonobject.put("keys", WritableJSONObject.getNames(jsonobject));
+ // assertEquals("{\n" + " \"JSONArray\": [],\n" + " \"JSONObject\": {},\n"
+ // + " \"String\": \"98.6\",\n" + " \"\\\\u2028\": \"\\u2028\",\n"
+ // + " \"\\\\u2029\": \"\\u2029\",\n" + " \"bool\": \"true\",\n"
+ // + " \"double\": 1.2345678901234568E29,\n" + " \"false\": false,\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" + " \"int\": 57,\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"
+ // + " \"null\": null,\n" + " \"op\": \"Good\",\n" + " \"ten\": 10,\n"
+ // + " \"to\": null,\n" + " \"true\": true,\n" + " \"zero\": -0\n" + "}",
+ // jsonobject.toString(4));
+ // //
+ // assertEquals("null10Good[Ljava.lang.String;@4d12512757truetruefalse98765432100.01.000000011.0000000000011.01.0E-172.00.12.0E100-32string6662001.99so
+ // // "fine".so
+ // //
+ // <fine>.truefalse-0.01.2345678901234568E2998.6falsetrue<\\u2028>?\\u2028><\\u2029>?\\u2029>null",
+ // // XML.toString(j));
+ // assertEquals(98.6d, jsonobject.getDouble("String"), eps);
+ // assertTrue(jsonobject.getBoolean("bool"));
+ // assertEquals(null, jsonobject.getString("to"));
+ // assertEquals("true", jsonobject.getString("true"));
+ // 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" + " \"five\": [\n" + " \"\",\n" + " \"\"\n" + " ],\n"
+ // + " \"four\": \"\",\n" + " \"one\": 1,\n" + " \"seven\": 7,\n"
+ // + " \"six\": {\"content\": 6},\n" + " \"three\": [\n" + " 3,\n"
+ // + " \"III\",\n" + " \"T H R E E\"\n" + " ],\n"
+ // + " \"two\": \" \\\"2\\\" \"\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" + " \"one\": 1,\n"
+ // + " \"two\": \" \\\"2\\\" \"\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" + " \"mi\": 2,\n"
+ // + " \"re\": 1\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" + " \"class\": [\n" + " {\n" + " \"field\": [\n"
+ // + " {\n" + " \"bind-xml\": {\n" + " \"name\": \"ID\",\n"
+ // + " \"node\": \"attribute\"\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"
+ // + " \"name\": \"text\",\n" + " \"node\": \"text\"\n"
+ // + " },\n" + " \"name\": \"text\"\n" + " },\n"
+ // + " \"name\": \"FirstName\"\n" + " },\n" + " {\n"
+ // + " \"field\": {\n" + " \"bind-xml\": {\n"
+ // + " \"name\": \"text\",\n" + " \"node\": \"text\"\n"
+ // + " },\n" + " \"name\": \"text\"\n" + " },\n"
+ // + " \"name\": \"MI\"\n" + " },\n" + " {\n" + " \"field\": {\n"
+ // + " \"bind-xml\": {\n" + " \"name\": \"text\",\n"
+ // + " \"node\": \"text\"\n" + " },\n"
+ // + " \"name\": \"text\"\n" + " },\n" + " \"name\": \"LastName\"\n"
+ // + " }\n" + " ],\n" + " \"empty\": \"\"\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" + " \"name\": \"ID\",\n"
+ // + " \"node\": \"attribute\"\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"
+ // + " \"name\": \"text\",\n"
+ // + " \"node\": \"text\"\n" + " }\n"
+ // + " ]\n" + " ]\n" + " ],\n" + " [\n"
+ // + " \"class\",\n" + " {\"name\": \"MI\"},\n" + " [\n"
+ // + " \"field\",\n" + " {\"name\": \"text\"},\n"
+ // + " [\n" + " \"bind-xml\",\n" + " {\n"
+ // + " \"name\": \"text\",\n"
+ // + " \"node\": \"text\"\n" + " }\n"
+ // + " ]\n" + " ]\n" + " ],\n" + " [\n"
+ // + " \"class\",\n" + " {\"name\": \"LastName\"},\n" + " [\n"
+ // + " \"field\",\n" + " {\"name\": \"text\"},\n"
+ // + " [\n" + " \"bind-xml\",\n" + " {\n"
+ // + " \"name\": \"text\",\n"
+ // + " \"node\": \"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" + " \"Author\": \"Anonymous\",\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" + " \"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" + " \"company\": \"MCI\",\n"
+ // + " \"email\": \"khare@mci.net\",\n" + " \"firstname\": \"Rohit\",\n"
+ // + " \"homepage\": \"http://pest.w3.org/\",\n" + " \"lastname\": \"Khare\"\n"
+ // + " },\n" + " {\n" + " \"company\": \"Caltech Infospheres Project\",\n"
+ // + " \"email\": \"adam@cs.caltech.edu\",\n" + " \"firstname\": \"Adam\",\n"
+ // + " \"homepage\": \"http://www.cs.caltech.edu/~adam/\",\n"
+ // + " \"lastname\": \"Rifkin\"\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" + " \"ID\": \"fbs0001\",\n"
+ // + " \"MI\": {\"text\": \"B\"},\n" + " \"firstName\": {\"text\": \"Fred\"},\n"
+ // + " \"lastName\": {\"text\": \"Scerbo\"}\n" + "}}",
+ // jsonobject.toString(2));
+ // assertEquals("ScerboBfbs0001Fred",
+ // XML.toString(jsonobject));
+ //
+ // jsonobject =
+ // XML.toJSONObject("Repository Address - Special Collections Library
- ABC
+ // University
- Main Library, 40 Circle Drive
- Ourtown,
+ // Pennsylvania
- 17654 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 Library
- ABC
+ // University
- Main Library, 40 Circle Drive
- Ourtown,
+ // Pennsylvania
- 17654 USA
Repository
+ // Addresssimple
",
+ // XML.toString(jsonobject));
+ //
+ // jsonobject =
+ // XML.toJSONObject("deluxe&"toot"&toot;Aeksbonusbonus2");
+ // assertEquals("{\"test\": {\n" + " \"blip\": {\n"
+ // + " \"content\": \"&\\\"toot\\\"&toot;A\",\n" + " \"sweet\": true\n"
+ // + " },\n" + " \"content\": \"deluxe\",\n" + " \"empty\": \"\",\n"
+ // + " \"intertag\": \"\",\n" + " \"status\": \"ok\",\n" + " \"w\": [\n"
+ // + " \"bonus\",\n" + " \"bonus2\"\n" + " ],\n" + " \"x\": \"eks\"\n" + "}}",
+ // jsonobject.toString(2));
+ // assertEquals("bonusbonus2deluxeok&"toot"&toot;Atrueeks",
+ // 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\": \"image/gif, image/x-xbitmap, image/jpeg, image/pjpeg,
+ // application/vnd.ms-powerpoint, application/vnd.ms-excel, application/msword, */*\",\n"
+ // + " \"Accept-Language\": \"en-us\",\n"
+ // + " \"Accept-encoding\": \"gzip, deflate\",\n"
+ // + " \"Connection\": \"keep-alive\",\n" + " \"HTTP-Version\": \"HTTP/1.0\",\n"
+ // + " \"Host\": \"www.nokko.com\",\n" + " \"Method\": \"GET\",\n"
+ // + " \"Request-URI\": \"/\",\n"
+ // + " \"User-Agent\": \"Mozilla/4.0 (compatible; MSIE 5.5; Windows 98; Win 9x 4.90; T312461;
+ // Q312461)\"\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" + " \"Connection\": \"Keep-Alive\",\n"
+ // + " \"Content-Type\": \"text/html\",\n"
+ // + " \"Date\": \"Sun, 26 May 2002 17:38:52 GMT\",\n"
+ // + " \"HTTP-Version\": \"HTTP/1.1\",\n"
+ // + " \"Keep-Alive\": \"timeout=15, max=100\",\n"
+ // + " \"Reason-Phrase\": \"Oki Doki\",\n"
+ // + " \"Server\": \"Apache/1.3.23 (Unix) mod_perl/1.26\",\n"
+ // + " \"Status-Code\": \"200\",\n" + " \"Transfer-Encoding\": \"chunked\"\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 WritableJSONObject("{nix: null, nux: false, null: 'null', 'Request-URI': '/', Method:
+ // 'GET', 'HTTP-Version': 'HTTP/1.0'}");
+ // assertEquals("{\n" + " \"HTTP-Version\": \"HTTP/1.0\",\n" + " \"Method\": \"GET\",\n"
+ // + " \"Request-URI\": \"/\",\n" + " \"nix\": null,\n" + " \"null\": \"null\",\n"
+ // + " \"nux\": false\n" + "}",
+ // jsonobject.toString(2));
+ // assertTrue(jsonobject.isNull("nix"));
+ // assertTrue(jsonobject.has("nix"));
+ // assertEquals("/nullfalseGETHTTP/1.0null",
+ // XML.toString(jsonobject));
+ // assertEquals("GET \"/\" HTTP/1.0\r\n" + "nux: false\r\n" + "null: null\r\n\r\n",
+ // HTTP.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"
+ // + " \"SOAP-ENV:encodingStyle\": \"http://schemas.xmlsoap.org/soap/encoding/\",\n"
+ // + " \"filter\": {\n" + " \"content\": true,\n"
+ // + " \"xsi:type\": \"xsd:boolean\"\n" + " },\n" + " \"ie\": {\n"
+ // + " \"content\": \"latin1\",\n" + " \"xsi:type\": \"xsd:string\"\n"
+ // + " },\n" + " \"key\": {\n" + " \"content\": \"GOOGLEKEY\",\n"
+ // + " \"xsi:type\": \"xsd:string\"\n" + " },\n"
+ // + " \"lr\": {\"xsi:type\": \"xsd:string\"},\n" + " \"maxResults\": {\n"
+ // + " \"content\": 10,\n" + " \"xsi:type\": \"xsd:int\"\n" + " },\n"
+ // + " \"oe\": {\n" + " \"content\": \"latin1\",\n"
+ // + " \"xsi:type\": \"xsd:string\"\n" + " },\n" + " \"q\": {\n"
+ // + " \"content\": \"'+search+'\",\n" + " \"xsi:type\": \"xsd:string\"\n"
+ // + " },\n" + " \"restrict\": {\"xsi:type\": \"xsd:string\"},\n"
+ // + " \"safeSearch\": {\n" + " \"content\": false,\n"
+ // + " \"xsi:type\": \"xsd:boolean\"\n" + " },\n" + " \"start\": {\n"
+ // + " \"content\": \"0\",\n" + " \"xsi:type\": \"xsd:int\"\n" + " },\n"
+ // + " \"xmlns:ns1\": \"urn:GoogleSearch\"\n" + " }},\n"
+ // + " \"xmlns:SOAP-ENV\": \"http://schemas.xmlsoap.org/soap/envelope/\",\n"
+ // + " \"xmlns:xsd\": \"http://www.w3.org/1999/XMLSchema\",\n"
+ // + " \"xmlns:xsi\": \"http://www.w3.org/1999/XMLSchema-instance\"\n" + "}}",
+ // jsonobject.toString(2));
+ //
+ // assertEquals("latin1xsd:stringhttp://schemas.xmlsoap.org/soap/encoding/xsd:string0xsd:int'+search+'xsd:string
latin1xsd: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 WritableJSONObject("{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"
+ // + " \"SOAP-ENV:encodingStyle\": \"http://schemas.xmlsoap.org/soap/encoding/\",\n"
+ // + " \"filter\": true,\n" + " \"ie\": \"latin1\",\n"
+ // + " \"key\": \"GOOGLEKEY\",\n" + " \"maxResults\": 10,\n"
+ // + " \"oe\": \"latin1\",\n" + " \"q\": \"'+search+'\",\n"
+ // + " \"safeSearch\": false,\n" + " \"start\": 0,\n"
+ // + " \"xmlns:ns1\": \"urn:GoogleSearch\"\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" + " \"f%oo\": \"b l=ah\",\n" + " \"o;n@e\": \"t.wo\"\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 WritableJSONObject("{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" +
- " \"backslash\": \"\\\\\",\n" +
- " \"closetag\": \"<\\/script>\",\n" +
- " \"ei\": {\"quotes\": \"\\\"'\"},\n" +
- " \"eo\": {\n" +
- " \"a\": \"\\\"quoted\\\"\",\n" +
- " \"b\": \"don't\"\n" +
- " },\n" +
- " \"quotes\": [\n" +
- " \"'\",\n" +
- " \"\\\"\"\n" +
- " ],\n" +
- " \"slashes\": \"///\"\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" +
- " \"JSONArray\": [],\n" +
- " \"JSONObject\": {},\n" +
- " \"String\": \"98.6\",\n" +
- " \"\\\\u2028\": \"\\u2028\",\n" +
- " \"\\\\u2029\": \"\\u2029\",\n" +
- " \"bool\": \"true\",\n" +
- " \"double\": 1.2345678901234568E29,\n" +
- " \"false\": false,\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" +
- " \"int\": 57,\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" +
- " \"null\": null,\n" +
- " \"op\": \"Good\",\n" +
- " \"ten\": 10,\n" +
- " \"to\": null,\n" +
- " \"true\": true,\n" +
- " \"zero\": -0\n" +
- "}", jsonobject.toString(4));
-// assertEquals("null10Good[Ljava.lang.String;@4d12512757truetruefalse98765432100.01.000000011.0000000000011.01.0E-172.00.12.0E100-32string6662001.99so "fine".so <fine>.truefalse-0.01.2345678901234568E2998.6falsetrue<\\u2028>?\\u2028><\\u2029>?\\u2029>null",
-// XML.toString(j));
- assertEquals(98.6d, jsonobject.getDouble("String"), eps);
- assertTrue(jsonobject.getBoolean("bool"));
- assertEquals(null, jsonobject.getString("to"));
- assertEquals("true", jsonobject.getString("true"));
- 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" +
- " \"five\": [\n" +
- " \"\",\n" +
- " \"\"\n" +
- " ],\n" +
- " \"four\": \"\",\n" +
- " \"one\": 1,\n" +
- " \"seven\": 7,\n" +
- " \"six\": {\"content\": 6},\n" +
- " \"three\": [\n" +
- " 3,\n" +
- " \"III\",\n" +
- " \"T H R E E\"\n" +
- " ],\n" +
- " \"two\": \" \\\"2\\\" \"\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" +
- " \"one\": 1,\n" +
- " \"two\": \" \\\"2\\\" \"\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" +
- " \"mi\": 2,\n" +
- " \"re\": 1\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" +
- " \"class\": [\n" +
- " {\n" +
- " \"field\": [\n" +
- " {\n" +
- " \"bind-xml\": {\n" +
- " \"name\": \"ID\",\n" +
- " \"node\": \"attribute\"\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" +
- " \"name\": \"text\",\n" +
- " \"node\": \"text\"\n" +
- " },\n" +
- " \"name\": \"text\"\n" +
- " },\n" +
- " \"name\": \"FirstName\"\n" +
- " },\n" +
- " {\n" +
- " \"field\": {\n" +
- " \"bind-xml\": {\n" +
- " \"name\": \"text\",\n" +
- " \"node\": \"text\"\n" +
- " },\n" +
- " \"name\": \"text\"\n" +
- " },\n" +
- " \"name\": \"MI\"\n" +
- " },\n" +
- " {\n" +
- " \"field\": {\n" +
- " \"bind-xml\": {\n" +
- " \"name\": \"text\",\n" +
- " \"node\": \"text\"\n" +
- " },\n" +
- " \"name\": \"text\"\n" +
- " },\n" +
- " \"name\": \"LastName\"\n" +
- " }\n" +
- " ],\n" +
- " \"empty\": \"\"\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" +
- " \"name\": \"ID\",\n" +
- " \"node\": \"attribute\"\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" +
- " \"name\": \"text\",\n" +
- " \"node\": \"text\"\n" +
- " }\n" +
- " ]\n" +
- " ]\n" +
- " ],\n" +
- " [\n" +
- " \"class\",\n" +
- " {\"name\": \"MI\"},\n" +
- " [\n" +
- " \"field\",\n" +
- " {\"name\": \"text\"},\n" +
- " [\n" +
- " \"bind-xml\",\n" +
- " {\n" +
- " \"name\": \"text\",\n" +
- " \"node\": \"text\"\n" +
- " }\n" +
- " ]\n" +
- " ]\n" +
- " ],\n" +
- " [\n" +
- " \"class\",\n" +
- " {\"name\": \"LastName\"},\n" +
- " [\n" +
- " \"field\",\n" +
- " {\"name\": \"text\"},\n" +
- " [\n" +
- " \"bind-xml\",\n" +
- " {\n" +
- " \"name\": \"text\",\n" +
- " \"node\": \"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" +
- " \"Author\": \"Anonymous\",\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" +
- " \"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" +
- " \"company\": \"MCI\",\n" +
- " \"email\": \"khare@mci.net\",\n" +
- " \"firstname\": \"Rohit\",\n" +
- " \"homepage\": \"http://pest.w3.org/\",\n" +
- " \"lastname\": \"Khare\"\n" +
- " },\n" +
- " {\n" +
- " \"company\": \"Caltech Infospheres Project\",\n" +
- " \"email\": \"adam@cs.caltech.edu\",\n" +
- " \"firstname\": \"Adam\",\n" +
- " \"homepage\": \"http://www.cs.caltech.edu/~adam/\",\n" +
- " \"lastname\": \"Rifkin\"\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" +
- " \"ID\": \"fbs0001\",\n" +
- " \"MI\": {\"text\": \"B\"},\n" +
- " \"firstName\": {\"text\": \"Fred\"},\n" +
- " \"lastName\": {\"text\": \"Scerbo\"}\n" +
- "}}", jsonobject.toString(2));
- assertEquals("ScerboBfbs0001Fred",
- XML.toString(jsonobject));
-
- jsonobject = XML.toJSONObject("Repository Address - Special Collections Library
- ABC University
- Main Library, 40 Circle Drive
- Ourtown, Pennsylvania
- 17654 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 Library
- ABC University
- Main Library, 40 Circle Drive
- Ourtown, Pennsylvania
- 17654 USA
Repository Addresssimple
",
- XML.toString(jsonobject));
-
- jsonobject = XML.toJSONObject("deluxe&"toot"&toot;Aeksbonusbonus2");
- assertEquals("{\"test\": {\n" +
- " \"blip\": {\n" +
- " \"content\": \"&\\\"toot\\\"&toot;A\",\n" +
- " \"sweet\": true\n" +
- " },\n" +
- " \"content\": \"deluxe\",\n" +
- " \"empty\": \"\",\n" +
- " \"intertag\": \"\",\n" +
- " \"status\": \"ok\",\n" +
- " \"w\": [\n" +
- " \"bonus\",\n" +
- " \"bonus2\"\n" +
- " ],\n" +
- " \"x\": \"eks\"\n" +
- "}}", jsonobject.toString(2));
- assertEquals("bonusbonus2deluxeok&"toot"&toot;Atrueeks",
- 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\": \"image/gif, image/x-xbitmap, image/jpeg, image/pjpeg, application/vnd.ms-powerpoint, application/vnd.ms-excel, application/msword, */*\",\n" +
- " \"Accept-Language\": \"en-us\",\n" +
- " \"Accept-encoding\": \"gzip, deflate\",\n" +
- " \"Connection\": \"keep-alive\",\n" +
- " \"HTTP-Version\": \"HTTP/1.0\",\n" +
- " \"Host\": \"www.nokko.com\",\n" +
- " \"Method\": \"GET\",\n" +
- " \"Request-URI\": \"/\",\n" +
- " \"User-Agent\": \"Mozilla/4.0 (compatible; MSIE 5.5; Windows 98; Win 9x 4.90; T312461; Q312461)\"\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" +
- " \"Connection\": \"Keep-Alive\",\n" +
- " \"Content-Type\": \"text/html\",\n" +
- " \"Date\": \"Sun, 26 May 2002 17:38:52 GMT\",\n" +
- " \"HTTP-Version\": \"HTTP/1.1\",\n" +
- " \"Keep-Alive\": \"timeout=15, max=100\",\n" +
- " \"Reason-Phrase\": \"Oki Doki\",\n" +
- " \"Server\": \"Apache/1.3.23 (Unix) mod_perl/1.26\",\n" +
- " \"Status-Code\": \"200\",\n" +
- " \"Transfer-Encoding\": \"chunked\"\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" +
- " \"HTTP-Version\": \"HTTP/1.0\",\n" +
- " \"Method\": \"GET\",\n" +
- " \"Request-URI\": \"/\",\n" +
- " \"nix\": null,\n" +
- " \"null\": \"null\",\n" +
- " \"nux\": false\n" +
- "}", jsonobject.toString(2));
- assertTrue(jsonobject.isNull("nix"));
- assertTrue(jsonobject.has("nix"));
- assertEquals("/nullfalseGETHTTP/1.0null",
- XML.toString(jsonobject));
- assertEquals("GET \"/\" HTTP/1.0\r\n" +
- "nux: false\r\n" +
- "null: null\r\n\r\n", HTTP.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" +
- " \"SOAP-ENV:encodingStyle\": \"http://schemas.xmlsoap.org/soap/encoding/\",\n" +
- " \"filter\": {\n" +
- " \"content\": true,\n" +
- " \"xsi:type\": \"xsd:boolean\"\n" +
- " },\n" +
- " \"ie\": {\n" +
- " \"content\": \"latin1\",\n" +
- " \"xsi:type\": \"xsd:string\"\n" +
- " },\n" +
- " \"key\": {\n" +
- " \"content\": \"GOOGLEKEY\",\n" +
- " \"xsi:type\": \"xsd:string\"\n" +
- " },\n" +
- " \"lr\": {\"xsi:type\": \"xsd:string\"},\n" +
- " \"maxResults\": {\n" +
- " \"content\": 10,\n" +
- " \"xsi:type\": \"xsd:int\"\n" +
- " },\n" +
- " \"oe\": {\n" +
- " \"content\": \"latin1\",\n" +
- " \"xsi:type\": \"xsd:string\"\n" +
- " },\n" +
- " \"q\": {\n" +
- " \"content\": \"'+search+'\",\n" +
- " \"xsi:type\": \"xsd:string\"\n" +
- " },\n" +
- " \"restrict\": {\"xsi:type\": \"xsd:string\"},\n" +
- " \"safeSearch\": {\n" +
- " \"content\": false,\n" +
- " \"xsi:type\": \"xsd:boolean\"\n" +
- " },\n" +
- " \"start\": {\n" +
- " \"content\": \"0\",\n" +
- " \"xsi:type\": \"xsd:int\"\n" +
- " },\n" +
- " \"xmlns:ns1\": \"urn:GoogleSearch\"\n" +
- " }},\n" +
- " \"xmlns:SOAP-ENV\": \"http://schemas.xmlsoap.org/soap/envelope/\",\n" +
- " \"xmlns:xsd\": \"http://www.w3.org/1999/XMLSchema\",\n" +
- " \"xmlns:xsi\": \"http://www.w3.org/1999/XMLSchema-instance\"\n" +
- "}}", jsonobject.toString(2));
-
- assertEquals("latin1xsd:stringhttp://schemas.xmlsoap.org/soap/encoding/xsd:string0xsd:int'+search+'xsd:string
latin1xsd: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" +
- " \"SOAP-ENV:encodingStyle\": \"http://schemas.xmlsoap.org/soap/encoding/\",\n" +
- " \"filter\": true,\n" +
- " \"ie\": \"latin1\",\n" +
- " \"key\": \"GOOGLEKEY\",\n" +
- " \"maxResults\": 10,\n" +
- " \"oe\": \"latin1\",\n" +
- " \"q\": \"'+search+'\",\n" +
- " \"safeSearch\": false,\n" +
- " \"start\": 0,\n" +
- " \"xmlns:ns1\": \"urn:GoogleSearch\"\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" +
- " \"f%oo\": \"b l=ah\",\n" +
- " \"o;n@e\": \"t.wo\"\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 string