You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@tapestry.apache.org by jk...@apache.org on 2017/03/09 10:16:28 UTC

[1/2] tapestry-5 git commit: TAP5-2575: rewrite some of tapestry-json based upon code from https://github.com/tdunning/open-json

Repository: tapestry-5
Updated Branches:
  refs/heads/master beaa642b4 -> aaa0d550a


http://git-wip-us.apache.org/repos/asf/tapestry-5/blob/aaa0d550/tapestry-json/src/main/java/org/apache/tapestry5/json/JSONStringer.java
----------------------------------------------------------------------
diff --git a/tapestry-json/src/main/java/org/apache/tapestry5/json/JSONStringer.java b/tapestry-json/src/main/java/org/apache/tapestry5/json/JSONStringer.java
new file mode 100644
index 0000000..8612635
--- /dev/null
+++ b/tapestry-json/src/main/java/org/apache/tapestry5/json/JSONStringer.java
@@ -0,0 +1,295 @@
+/*
+ * Copyright (C) 2010 The Android Open Source Project
+ *
+ * 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.apache.tapestry5.json;
+
+import java.util.ArrayList;
+import java.util.Arrays;
+import java.util.List;
+
+// Note: this class was written without inspecting the non-free org.json sourcecode.
+
+/**
+ * Implements {@link JSONObject#toString} and {@link JSONArray#toString}. Most
+ * application developers should use those methods directly and disregard this
+ * API. For example:<pre>
+ * JSONObject object = ...
+ * String json = object.toString();</pre>
+ *
+ * <p>Stringers only encode well-formed JSON strings. In particular:
+ * <ul>
+ * <li>The stringer must have exactly one top-level array or object.
+ * <li>Lexical scopes must be balanced: every call to {@link #array} must
+ * have a matching call to {@link #endArray} and every call to {@link
+ * #object} must have a matching call to {@link #endObject}.
+ * <li>Arrays may not contain keys (property names).
+ * <li>Objects must alternate keys (property names) and values.
+ * <li>Values are inserted with either literal {@link #value(Object) value}
+ * calls, or by nesting arrays or objects.
+ * </ul>
+ * Calls that would result in a malformed JSON string will fail with a
+ * {@link RuntimeException}.
+ *
+ * <p>This class provides no facility for pretty-printing (ie. indenting)
+ * output. To encode indented output, use {@link JSONObject#toString(int)} or
+ * {@link JSONArray#toString(int)}.
+ *
+ * <p>Some implementations of the API support at most 20 levels of nesting.
+ * Attempts to create more than 20 levels of nesting may fail with a {@link
+ * RuntimeException}.
+ *
+ * <p>Each stringer may be used to encode a single top level value. Instances of
+ * this class are not thread safe. Although this class is nonfinal, it was not
+ * designed for inheritance and should not be subclassed. In particular,
+ * self-use by overrideable methods is not specified. See <i>Effective Java</i>
+ * Item 17, "Design and Document or inheritance or else prohibit it" for further
+ * information.
+ */
+class JSONStringer {
+
+    /**
+     * The output data, containing at most one top-level array or object.
+     */
+    final StringBuilder out = new StringBuilder();
+
+    /**
+     * Lexical scoping elements within this stringer, necessary to insert the
+     * appropriate separator characters (ie. commas and colons) and to detect
+     * nesting errors.
+     */
+    enum Scope {
+
+        /**
+         * An array with no elements requires no separators or newlines before
+         * it is closed.
+         */
+        EMPTY_ARRAY,
+
+        /**
+         * A array with at least one value requires a comma and newline before
+         * the next element.
+         */
+        NONEMPTY_ARRAY,
+
+        /**
+         * An object with no keys or values requires no separators or newlines
+         * before it is closed.
+         */
+        EMPTY_OBJECT,
+
+        /**
+         * An object whose most recent element is a key. The next element must
+         * be a value.
+         */
+        DANGLING_KEY,
+
+        /**
+         * An object with at least one name/value pair requires a comma and
+         * newline before the next element.
+         */
+        NONEMPTY_OBJECT,
+
+        /**
+         * A special bracketless array needed by JSONStringer.join() and
+         * JSONObject.quote() only. Not used for JSON encoding.
+         */
+        NULL,
+    }
+
+    /**
+     * Unlike the original implementation, this stack isn't limited to 20
+     * levels of nesting.
+     */
+    private final List<Scope> stack = new ArrayList<Scope>();
+
+    /**
+     * A string containing a full set of spaces for a single level of
+     * indentation, or null for no pretty printing.
+     */
+    private final String indent;
+
+    JSONStringer() {
+        indent = null;
+    }
+
+    JSONStringer(int indentSpaces) {
+        char[] indentChars = new char[indentSpaces];
+        Arrays.fill(indentChars, ' ');
+        indent = new String(indentChars);
+    }
+
+    /**
+     * Enters a new scope by appending any necessary whitespace and the given
+     * bracket.
+     */
+    JSONStringer open(Scope empty, String openBracket){
+        if (stack.isEmpty() && out.length() > 0) {
+            throw new RuntimeException("Nesting problem: multiple top-level roots");
+        }
+        beforeValue();
+        stack.add(empty);
+        out.append(openBracket);
+        return this;
+    }
+
+    /**
+     * Closes the current scope by appending any necessary whitespace and the
+     * given bracket.
+     */
+    JSONStringer close(Scope empty, Scope nonempty, String closeBracket){
+        Scope context = peek();
+        if (context != nonempty && context != empty) {
+            throw new RuntimeException("Nesting problem");
+        }
+
+        stack.remove(stack.size() - 1);
+        if (context == nonempty) {
+            newline();
+        }
+        out.append(closeBracket);
+        return this;
+    }
+
+    /**
+     * Returns the value on the top of the stack.
+     */
+    private Scope peek(){
+        if (stack.isEmpty()) {
+            throw new RuntimeException("Nesting problem");
+        }
+        return stack.get(stack.size() - 1);
+    }
+
+    /**
+     * Replace the value on the top of the stack with the given value.
+     */
+    private void replaceTop(Scope topOfStack) {
+        stack.set(stack.size() - 1, topOfStack);
+    }
+
+
+    void string(String value) {
+        out.append("\"");
+        char currentChar = 0;
+
+        for (int i = 0, length = value.length(); i < length; i++) {
+            char previousChar = currentChar;
+            currentChar = value.charAt(i);
+
+            /*
+             * From RFC 4627, "All Unicode characters may be placed within the
+             * quotation marks except for the characters that must be escaped:
+             * quotation mark, reverse solidus, and the control characters
+             * (U+0000 through U+001F)."
+             */
+            switch (currentChar) {
+                case '"':
+                case '\\':
+                    out.append('\\').append(currentChar);
+                    break;
+
+                case '/':
+                    // it makes life easier for HTML embedding of javascript if we escape </ sequences
+                    if (previousChar == '<') {
+                        out.append('\\');
+                    }
+                    out.append(currentChar);
+                    break;
+
+                case '\t':
+                    out.append("\\t");
+                    break;
+
+                case '\b':
+                    out.append("\\b");
+                    break;
+
+                case '\n':
+                    out.append("\\n");
+                    break;
+
+                case '\r':
+                    out.append("\\r");
+                    break;
+
+                case '\f':
+                    out.append("\\f");
+                    break;
+
+                default:
+                    if (currentChar <= 0x1F || (currentChar >= 0x0080 && currentChar < 0x00a0) || (currentChar >= 0x2000 && currentChar < 0x2100)) {
+                        out.append(String.format("\\u%04x", (int) currentChar));
+                    } else {
+                        out.append(currentChar);
+                    }
+                    break;
+            }
+
+        }
+        out.append("\"");
+    }
+
+    private void newline() {
+        if (indent == null) {
+            return;
+        }
+
+        out.append("\n");
+        for (int i = 0; i < stack.size(); i++) {
+            out.append(indent);
+        }
+    }
+
+    /**
+     * Inserts any necessary separators and whitespace before a literal value,
+     * inline array, or inline object. Also adjusts the stack to expect either a
+     * closing bracket or another element.
+     */
+    private void beforeValue(){
+        if (stack.isEmpty()) {
+            return;
+        }
+
+        Scope context = peek();
+        if (context == Scope.EMPTY_ARRAY) { // first in array
+            replaceTop(Scope.NONEMPTY_ARRAY);
+            newline();
+        } else if (context == Scope.NONEMPTY_ARRAY) { // another in array
+            out.append(',');
+            newline();
+        } else if (context == Scope.DANGLING_KEY) { // value for key
+            out.append(indent == null ? ":" : ": ");
+            replaceTop(Scope.NONEMPTY_OBJECT);
+        } else if (context != Scope.NULL) {
+            throw new RuntimeException("Nesting problem");
+        }
+    }
+
+    /**
+     * Returns the encoded JSON string.
+     *
+     * <p>If invoked with unterminated arrays or unclosed objects, this method's
+     * return value is undefined.
+     *
+     * <p><strong>Warning:</strong> although it contradicts the general contract
+     * of {@link Object#toString}, this method returns null if the stringer
+     * contains no data.
+     */
+    @Override
+    public String toString() {
+        return out.length() == 0 ? null : out.toString();
+    }
+}

http://git-wip-us.apache.org/repos/asf/tapestry-5/blob/aaa0d550/tapestry-json/src/main/java/org/apache/tapestry5/json/JSONTokener.java
----------------------------------------------------------------------
diff --git a/tapestry-json/src/main/java/org/apache/tapestry5/json/JSONTokener.java b/tapestry-json/src/main/java/org/apache/tapestry5/json/JSONTokener.java
index f40730b..38fb443 100644
--- a/tapestry-json/src/main/java/org/apache/tapestry5/json/JSONTokener.java
+++ b/tapestry-json/src/main/java/org/apache/tapestry5/json/JSONTokener.java
@@ -1,397 +1,514 @@
-// Copyright 2007 The Apache Software Foundation
-//
-// 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.
+/*
+ * Copyright (C) 2010 The Android Open Source Project
+ *
+ * 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.apache.tapestry5.json;
 
-/*
- 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.
- */
+// Note: this class was written without inspecting the non-free org.json sourcecode.
+
+import java.io.IOException;
+import java.io.Reader;
 
 /**
- * A JSONTokener takes a source string and extracts characters and tokens from it. It is used by the JSONObject and
- * JSONArray constructors to parse JSON source strings.
+ * Parses a JSON (<a href="http://www.ietf.org/rfc/rfc4627.txt">RFC 4627</a>)
+ * encoded string into the corresponding object. Most clients of
+ * this class will use only need the {@link #JSONTokener(String) constructor}
+ * and {@link #nextValue} method. Example usage: <pre>
+ * String json = "{"
+ *         + "  \"query\": \"Pizza\", "
+ *         + "  \"locations\": [ 94043, 90210 ] "
+ *         + "}";
+ *
+ * JSONObject object = (JSONObject) new JSONTokener(json).nextValue();
+ * String query = object.getString("query");
+ * JSONArray locations = object.getJSONArray("locations");</pre>
  *
- * @author JSON.org
- * @version 2
+ * <p>For best interoperability and performance use JSON that complies with
+ * RFC 4627. For legacy reasons
+ * this parser is lenient, so a successful parse does not indicate that the
+ * input string was valid JSON. All of the following syntax errors will be
+ * ignored:
+ * <ul>
+ * <li>End of line comments starting with {@code //} or {@code #} and ending
+ * with a newline character.
+ * <li>C-style comments starting with {@code /*} and ending with
+ * {@code *}{@code /}. Such comments may not be nested.
+ * <li>Strings that are unquoted or {@code 'single quoted'}.
+ * <li>Hexadecimal integers prefixed with {@code 0x} or {@code 0X}.
+ * <li>Octal integers prefixed with {@code 0}.
+ * <li>Array elements separated by {@code ;}.
+ * <li>Unnecessary array separators. These are interpreted as if null was the
+ * omitted value.
+ * <li>Key-value pairs separated by {@code =} or {@code =>}.
+ * <li>Key-value pairs separated by {@code ;}.
+ * </ul>
+ *
+ * <p>Each tokener may be used to parse a single JSON string. Instances of this
+ * class are not thread safe. Although this class is nonfinal, it was not
+ * designed for inheritance and should not be subclassed. In particular,
+ * self-use by overrideable methods is not specified. See <i>Effective Java</i>
+ * Item 17, "Design and Document or inheritance or else prohibit it" for further
+ * information.
  */
-class JSONTokener
-{
+class JSONTokener {
 
     /**
-     * The index of the next character.
+     * The input JSON.
      */
-    private int index;
+    private final String in;
 
     /**
-     * The source string being tokenized.
+     * The index of the next character to be returned by {@link #next}. When
+     * the input is exhausted, this equals the input's length.
      */
-    private final String source;
+    private int pos;
 
     /**
-     * Construct a JSONTokener from a string.
-     *
-     * @param source A source string, in JSON format.
+     * @param in JSON encoded string. Null is not permitted and will yield a
+     *           tokener that throws {@code NullPointerExceptions} when methods are
+     *           called.
      */
-    public JSONTokener(String source)
-    {
-        assert source != null;
-
-        index = 0;
-        this.source = source;
+    JSONTokener(String in) {
+        // consume an optional byte order mark (BOM) if it exists
+        if (in != null && in.startsWith("\ufeff")) {
+            in = in.substring(1);
+        }
+        this.in = in;
     }
 
-    /**
-     * Back up one character. This provides a sort of lookahead capability, so that you can test for a digit or letter
-     * before attempting to parse the next number or identifier.
-     */
-    public void back()
-    {
-        if (index > 0)
-        {
-            index -= 1;
+    JSONTokener(Reader input) throws IOException {
+        StringBuilder s = new StringBuilder();
+        char[] readBuf = new char[102400];
+        int n = input.read(readBuf);
+        while (n >= 0) {
+            s.append(readBuf, 0, n);
+            n = input.read(readBuf);
         }
+        in = s.toString();
+        pos = 0;
     }
 
     /**
-     * Determine if the source string still contains characters that next() can consume.
+     * Returns the next value from the input.
      *
-     * @return true if not yet at the end of the source.
+     * @return a {@link JSONObject}, {@link JSONArray}, String, Boolean,
+     * Integer, Long, Double or {@link JSONObject#NULL}.
+     * @throws RuntimeException if the input is malformed.
      */
-    public boolean more()
-    {
-        return index < source.length();
+     Object nextValue(Class<?> desiredType) {
+        int c = nextCleanInternal();
+        if (JSONObject.class.equals(desiredType) && c != '{'){
+            throw syntaxError("A JSONObject text must begin with '{'");
+        }
+        if (JSONArray.class.equals(desiredType) && c != '['){
+          throw syntaxError("A JSONArray text must start with '['");
+        }
+        switch (c) {
+            case -1:
+                throw syntaxError("End of input");
+
+            case '{':
+                return readObject();
+
+            case '[':
+                return readArray();
+
+            case '\'':
+            case '"':
+                return nextString((char) c);
+
+            default:
+                pos--;
+                return readLiteral();
+        }
     }
 
-    /**
-     * Get the next character in the source string.
-     *
-     * @return The next character, or 0 if past the end of the source string.
-     */
-    public char next()
-    {
-        if (more())
-        {
-            return source.charAt(index++);
+    private int nextCleanInternal() {
+        while (pos < in.length()) {
+            int c = in.charAt(pos++);
+            switch (c) {
+                case '\t':
+                case ' ':
+                case '\n':
+                case '\r':
+                    continue;
+
+                case '/':
+                    if (pos == in.length()) {
+                        return c;
+                    }
+
+                    char peek = in.charAt(pos);
+                    switch (peek) {
+                        case '*':
+                            // skip a /* c-style comment */
+                            pos++;
+                            int commentEnd = in.indexOf("*/", pos);
+                            if (commentEnd == -1) {
+                                pos = in.length();
+                                throw syntaxError("Unclosed comment");
+                            }
+                            pos = commentEnd + 2;
+                            continue;
+
+                        case '/':
+                            // skip a // end-of-line comment
+                            pos++;
+                            skipToEndOfLine();
+                            continue;
+
+                        default:
+                            return c;
+                    }
+
+                case '#':
+                    /*
+                     * Skip a # hash end-of-line comment. The JSON RFC doesn't
+                     * specify this behavior, but it's required to parse
+                     * existing documents. See http://b/2571423.
+                     */
+                    skipToEndOfLine();
+                    continue;
+
+                default:
+                    return c;
+            }
         }
 
-        return 0;
+        return -1;
     }
 
     /**
-     * Get the next n characters.
-     *
-     * @param n The number of characters to take.
-     * @return A string of n characters.
-     * @throws RuntimeException Substring bounds error if there are not n characters remaining in the source string.
+     * Advances the position until after the next newline character. If the line
+     * is terminated by "\r\n", the '\n' must be consumed as whitespace by the
+     * caller.
      */
-    public String next(int n)
-    {
-        int i = index;
-        int j = i + n;
-        if (j >= source.length())
-        {
-            throw syntaxError("Substring bounds error");
+    private void skipToEndOfLine() {
+        for (; pos < in.length(); pos++) {
+            char c = in.charAt(pos);
+            if (c == '\r' || c == '\n') {
+                pos++;
+                break;
+            }
         }
-        index += n;
-        return source.substring(i, j);
     }
 
     /**
-     * Get the next char in the string, skipping whitespace and comments (slashslash, slashstar, and hash).
+     * Returns the string up to but not including {@code quote}, unescaping any
+     * character escape sequences encountered along the way. The opening quote
+     * should have already been read. This consumes the closing quote, but does
+     * not include it in the returned string.
      *
-     * @return A character, or 0 if there are no more characters.
-     * @throws RuntimeException
+     * @param quote either ' or ".
+     * @return The unescaped string.
+     * @throws RuntimeException if the string isn't terminated by a closing quote correctly.
      */
-    public char nextClean()
-    {
-        for (; ;)
-        {
-            char c = next();
-            if (c == '/')
-            {
-                switch (next())
-                {
-                    case '/':
-                        do
-                        {
-                            c = next();
-                        } while (c != '\n' && c != '\r' && c != 0);
-
-                        break;
-                    case '*':
-
-                        while (true)
-                        {
-                            c = next();
-                            if (c == 0)
-                            {
-                                throw syntaxError("Unclosed comment");
-                            }
-                            if (c == '*')
-                            {
-                                if (next() == '/')
-                                {
-                                    break;
-                                }
-                                back();
-                            }
-                        }
-                        break;
-
-                    default:
-                        back();
-                        return '/';
+    private String nextString(char quote) {
+        /*
+         * For strings that are free of escape sequences, we can just extract
+         * the result as a substring of the input. But if we encounter an escape
+         * sequence, we need to use a StringBuilder to compose the result.
+         */
+        StringBuilder builder = null;
+
+        /* the index of the first character not yet appended to the builder. */
+        int start = pos;
+
+        while (pos < in.length()) {
+            int c = in.charAt(pos++);
+            if (c == quote) {
+                if (builder == null) {
+                    // a new string avoids leaking memory
+                    //noinspection RedundantStringConstructorCall
+                    return new String(in.substring(start, pos - 1));
+                } else {
+                    builder.append(in, start, pos - 1);
+                    return builder.toString();
                 }
             }
-            else if (c == '#')
-            {
-                do
-                {
-                    c = next();
-                } while (c != '\n' && c != '\r' && c != 0);
-            }
-            else if (c == 0 || c > ' ')
-            {
-                return c;
+
+            if (c == '\\') {
+                if (pos == in.length()) {
+                    throw syntaxError("Unterminated escape sequence");
+                }
+                if (builder == null) {
+                    builder = new StringBuilder();
+                }
+                builder.append(in, start, pos - 1);
+                builder.append(readEscapeCharacter());
+                start = pos;
             }
         }
+
+        throw syntaxError("Unterminated string");
     }
 
     /**
-     * Return the characters up to the next close quote character. Backslash processing is done. The formal JSON format
-     * does not allow strings in single quotes, but an implementation is allowed to accept them.
-     *
-     * @param quote The quoting character, either <code>"</code>&nbsp;<small>(double quote)</small> or
-     *              <code>'</code>&nbsp;<small>(single quote)</small>.
-     * @return A String.
-     * @throws RuntimeException Unterminated string.
+     * Unescapes the character identified by the character or characters that
+     * immediately follow a backslash. The backslash '\' should have already
+     * been read. This supports both unicode escapes "u000A" and two-character
+     * escapes "\n".
      */
-    public String nextString(char quote)
-    {
-        StringBuilder builder = new StringBuilder();
-
-        while (true)
-        {
-            char c = next();
-            switch (c)
-            {
-                case 0:
-                case '\n':
-                case '\r':
-                    throw syntaxError("Unterminated string");
-                case '\\':
-                    c = next();
-                    switch (c)
-                    {
-                        case 'b':
-                            builder.append('\b');
-                            break;
-                        case 't':
-                            builder.append('\t');
-                            break;
-                        case 'n':
-                            builder.append('\n');
-                            break;
-                        case 'f':
-                            builder.append('\f');
-                            break;
-                        case 'r':
-                            builder.append('\r');
-                            break;
-                        case 'u':
-                            builder.append((char) Integer.parseInt(next(4), 16));
-                            break;
-                        case 'x':
-                            builder.append((char) Integer.parseInt(next(2), 16));
-                            break;
-                        default:
-                            builder.append(c);
-                    }
-                    break;
-                default:
-                    if (c == quote)
-                    {
-                        return builder.toString();
-                    }
-                    builder.append(c);
+    private char readEscapeCharacter() {
+        char escaped = in.charAt(pos++);
+        switch (escaped) {
+            case 'u': {
+                if (pos + 4 > in.length()) {
+                    throw syntaxError("Unterminated escape sequence");
+                }
+                String hex = in.substring(pos, pos + 4);
+                pos += 4;
+                try {
+                    return (char) Integer.parseInt(hex, 16);
+                } catch (NumberFormatException nfe) {
+                    throw syntaxError("Invalid escape sequence: " + hex);
+                }
             }
-        }
-    }
+            case 'x': {
+              if (pos + 2 > in.length()) {
+                  throw syntaxError("Unterminated escape sequence");
+              }
+              String hex = in.substring(pos, pos + 2);
+              pos += 2;
+              try {
+                  return (char) Integer.parseInt(hex, 16);
+              } catch (NumberFormatException nfe) {
+                  throw syntaxError("Invalid escape sequence: " + hex);
+              }
 
+            }
+            case 't':
+                return '\t';
 
-    /**
-     * Get the next value. The value can be a Boolean, Double, Integer, JSONArray, JSONObject, Long, or String, or the
-     * JSONObject.NULL object.
-     *
-     * @return An object.
-     * @throws RuntimeException If syntax error.
-     */
-    public Object nextValue()
-    {
-        char c = nextClean();
-        String s;
+            case 'b':
+                return '\b';
 
-        switch (c)
-        {
-            case '"':
-            case '\'':
-                return nextString(c);
-            case '{':
-                back();
-                return new JSONObject(this);
-            case '[':
-                back();
-                return new JSONArray(this);
-        }
+            case 'n':
+                return '\n';
 
-        /*
-         * Handle unquoted text. This could be the values true, false, or null, or it can be a
-         * number. An implementation (such as this one) is allowed to also accept non-standard
-         * forms. Accumulate characters until we reach the end of the text or a formatting
-         * character.
-         */
+            case 'r':
+                return '\r';
 
-        StringBuffer sb = new StringBuffer();
-        char b = c;
-        while (c >= ' ' && ",:]}/\\\"[{;=#".indexOf(c) < 0)
-        {
-            sb.append(c);
-            c = next();
+            case 'f':
+                return '\f';
+
+            case '\'':
+            case '"':
+            case '\\':
+            default:
+                return escaped;
         }
-        back();
+    }
 
-        /*
-         * If it is true, false, or null, return the proper value.
-         */
+    /**
+     * Reads a null, boolean, numeric or unquoted string literal value. Numeric
+     * values will be returned as an Integer, Long, or Double, in that order of
+     * preference.
+     */
+    private Object readLiteral() {
+        String literal = nextToInternal("{}[]/\\:,=;# \t\f");
 
-        s = sb.toString().trim();
-        if (s.equals(""))
-        {
+        if (literal.length() == 0) {
             throw syntaxError("Missing value");
-        }
-        if (s.equalsIgnoreCase("true"))
-        {
+        } else if ("null".equalsIgnoreCase(literal)) {
+            return JSONObject.NULL;
+        } else if ("true".equalsIgnoreCase(literal)) {
             return Boolean.TRUE;
-        }
-        if (s.equalsIgnoreCase("false"))
-        {
+        } else if ("false".equalsIgnoreCase(literal)) {
             return Boolean.FALSE;
         }
-        if (s.equalsIgnoreCase("null"))
-        {
-            return JSONObject.NULL;
+
+        /* try to parse as an integral type... */
+        if (literal.indexOf('.') == -1) {
+            int base = 10;
+            String number = literal;
+            if (number.startsWith("0x") || number.startsWith("0X")) {
+                number = number.substring(2);
+                base = 16;
+            } else if (number.startsWith("0") && number.length() > 1) {
+                number = number.substring(1);
+                base = 8;
+            }
+            try {
+                long longValue = Long.parseLong(number, base);
+                if (longValue <= Integer.MAX_VALUE && longValue >= Integer.MIN_VALUE) {
+                    return (int) longValue;
+                } else {
+                    return longValue;
+                }
+            } catch (NumberFormatException e) {
+                /*
+                 * This only happens for integral numbers greater than
+                 * Long.MAX_VALUE, numbers in exponential form (5e-10) and
+                 * unquoted strings. Fall through to try floating point.
+                 */
+            }
         }
 
-        /*
-         * If it might be a number, try converting it. We support the 0- and 0x- conventions. If a
-         * number cannot be produced, then the value will just be a string. Note that the 0-, 0x-,
-         * plus, and implied string conventions are non-standard. A JSON parser is free to accept
-         * non-JSON forms as long as it accepts all correct JSON forms.
-         */
+        /* ...next try to parse as a floating point... */
+        try {
+            return Double.valueOf(literal);
+        } catch (NumberFormatException ignored) {
+        }
 
-        if ((b >= '0' && b <= '9') || b == '.' || b == '-' || b == '+')
-        {
-            if (b == '0')
-            {
-                if (s.length() > 2 && (s.charAt(1) == 'x' || s.charAt(1) == 'X'))
-                {
-                    try
-                    {
-                        return Integer.parseInt(s.substring(2), 16);
-                    }
-                    catch (Exception e)
-                    {
-                        /* Ignore the error */
-                    }
+        /* ... finally give up. We have an unquoted string */
+        //noinspection RedundantStringConstructorCall
+        return new String(literal); // a new string avoids leaking memory
+    }
+
+    /**
+     * Returns the string up to but not including any of the given characters or
+     * a newline character. This does not consume the excluded character.
+     */
+    private String nextToInternal(String excluded) {
+        int start = pos;
+        for (; pos < in.length(); pos++) {
+            char c = in.charAt(pos);
+            if (c == '\r' || c == '\n' || excluded.indexOf(c) != -1) {
+                return in.substring(start, pos);
+            }
+        }
+        return in.substring(start);
+    }
+
+    /**
+     * Reads a sequence of key/value pairs and the trailing closing brace '}' of
+     * an object. The opening brace '{' should have already been read.
+     */
+    private JSONObject readObject() {
+        JSONObject result = new JSONObject();
+
+        /* Peek to see if this is the empty object. */
+        int first = nextCleanInternal();
+        if (first == '}') {
+            return result;
+        } else if (first != -1) {
+            pos--;
+        }
+
+        while (true) {
+            Object name = null;
+            try {
+                name = nextValue(null);
+            } catch (RuntimeException e){
+                if (e.getMessage().equals("End of input" + this)){
+                    // hack to maintain compatibility with earlier releases of tapestry-json
+                    throw syntaxError("A JSONObject text must end with '}'");
                 }
-                else
-                {
-                    try
-                    {
-                        return Integer.parseInt(s, 8);
-                    }
-                    catch (Exception e)
-                    {
-                        /* Ignore the error */
-                    }
+                throw e;
+            }
+            if (!(name instanceof String)) {
+                if (name == null) {
+                    throw syntaxError("Names cannot be null");
+                } else {
+                    throw syntaxError("Names must be strings, but " + name
+                            + " is of type " + name.getClass().getName());
                 }
             }
-            try
-            {
-                return Integer.valueOf(s);
+
+            /*
+             * Expect the name/value separator to be either a colon ':', an
+             * equals sign '=', or an arrow "=>". The last two are bogus but we
+             * include them because that's what the original implementation did.
+             */
+            int separator = nextCleanInternal();
+            if (separator != ':' && separator != '=') {
+                throw syntaxError("Expected a ':' after a key");
             }
-            catch (Exception e)
-            {
-                try
-                {
-                    return Long.valueOf(s);
-                }
-                catch (Exception f)
-                {
-                    try
-                    {
-                        return Double.valueOf(s);
-                    }
-                    catch (Exception g)
-                    {
-                        return s;
+            if (pos < in.length() && in.charAt(pos) == '>') {
+                pos++;
+            }
+
+            result.put((String) name, nextValue(null));
+
+            switch (nextCleanInternal()) {
+                case '}':
+                    return result;
+                case ';':
+                case ',':
+                    continue;
+                default:
+                    throw syntaxError("Expected a ',' or '}'");
+            }
+        }
+    }
+
+    /**
+     * Reads a sequence of values and the trailing closing brace ']' of an
+     * array. The opening brace '[' should have already been read. Note that
+     * "[]" yields an empty array, but "[,]" returns a two-element array
+     * equivalent to "[null,null]".
+     */
+    private JSONArray readArray() {
+        JSONArray result = new JSONArray();
+
+        /* to cover input that ends with ",]". */
+        boolean hasTrailingSeparator = false;
+
+        while (true) {
+            switch (nextCleanInternal()) {
+                case -1:
+                    throw syntaxError("Expected a ',' or ']'");
+                case ']':
+                    if (hasTrailingSeparator) {
+                        //result.put(null);
                     }
-                }
+                    return result;
+                case ',':
+                case ';':
+                    /* A separator without a value first means "null". */
+                    result.put(JSONObject.NULL);
+                    hasTrailingSeparator = true;
+                    continue;
+                default:
+                    pos--;
+            }
+
+            result.put(nextValue(null));
+
+            switch (nextCleanInternal()) {
+                case ']':
+                    return result;
+                case ',':
+                case ';':
+                    hasTrailingSeparator = true;
+                    continue;
+                default:
+                    throw syntaxError("Expected a ',' or ']'");
             }
         }
-        return s;
     }
 
     /**
-     * Make a JSONException to signal a syntax error.
+     * Returns an exception containing the given message plus the current
+     * position and the entire input string.
      *
-     * @param message The error message.
-     * @return A JSONException object, suitable for throwing
+     * @param message The message we want to include.
+     * @return An exception that we can throw.
      */
-    RuntimeException syntaxError(String message)
-    {
-        return new RuntimeException(message + toString());
+    private RuntimeException syntaxError(String message) {
+        return new RuntimeException(message + this);
     }
 
     /**
-     * Make a printable string of this JSONTokener.
-     *
-     * @return " at character [myIndex] of [mySource]"
+     * Returns the current position and the entire input string.
      */
     @Override
-    public String toString()
-    {
-        return " at character " + index + " of " + source;
+    public String toString() {
+        // consistent with the original implementation
+        return " at character " + pos + " of " + in;
     }
+
 }

http://git-wip-us.apache.org/repos/asf/tapestry-5/blob/aaa0d550/tapestry-json/src/main/java/org/apache/tapestry5/json/package-info.java
----------------------------------------------------------------------
diff --git a/tapestry-json/src/main/java/org/apache/tapestry5/json/package-info.java b/tapestry-json/src/main/java/org/apache/tapestry5/json/package-info.java
index 5a757e7..464082c 100644
--- a/tapestry-json/src/main/java/org/apache/tapestry5/json/package-info.java
+++ b/tapestry-json/src/main/java/org/apache/tapestry5/json/package-info.java
@@ -11,7 +11,7 @@
 // limitations under the License.
 
 /**
- * Repackaged, improved (and tested) version of code originally from json.org.
+ * Repackaged, improved (and tested) version of code originally https://github.com/tdunning/open-json
  *
  * Starting in release 5.4, {@link org.apache.tapestry5.json.JSONObject} and {@link org.apache.tapestry5.json.JSONArray}
  * are serializable.


[2/2] tapestry-5 git commit: TAP5-2575: rewrite some of tapestry-json based upon code from https://github.com/tdunning/open-json

Posted by jk...@apache.org.
TAP5-2575: rewrite some of tapestry-json based upon code from https://github.com/tdunning/open-json


Project: http://git-wip-us.apache.org/repos/asf/tapestry-5/repo
Commit: http://git-wip-us.apache.org/repos/asf/tapestry-5/commit/aaa0d550
Tree: http://git-wip-us.apache.org/repos/asf/tapestry-5/tree/aaa0d550
Diff: http://git-wip-us.apache.org/repos/asf/tapestry-5/diff/aaa0d550

Branch: refs/heads/master
Commit: aaa0d550a9853c786bbac224582768218c3da1cd
Parents: beaa642
Author: Jochen Kemnade <jk...@apache.org>
Authored: Wed Mar 8 14:24:46 2017 +0100
Committer: Jochen Kemnade <jo...@eddyson.de>
Committed: Thu Mar 9 11:00:02 2017 +0100

----------------------------------------------------------------------
 tapestry-json/build.gradle                      |    2 +-
 .../java/org/apache/tapestry5/json/JSON.java    |  115 ++
 .../org/apache/tapestry5/json/JSONArray.java    |  589 ++++-----
 .../org/apache/tapestry5/json/JSONObject.java   | 1121 +++++++-----------
 .../org/apache/tapestry5/json/JSONStringer.java |  295 +++++
 .../org/apache/tapestry5/json/JSONTokener.java  |  749 +++++++-----
 .../org/apache/tapestry5/json/package-info.java |    2 +-
 7 files changed, 1550 insertions(+), 1323 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/tapestry-5/blob/aaa0d550/tapestry-json/build.gradle
----------------------------------------------------------------------
diff --git a/tapestry-json/build.gradle b/tapestry-json/build.gradle
index ccf936e..9b9d573 100644
--- a/tapestry-json/build.gradle
+++ b/tapestry-json/build.gradle
@@ -1,4 +1,4 @@
-description = "Repackaged, improved (and tested) version of code originally from json.org"
+description = "Repackaged, improved (and tested) version of code originally from https://github.com/tdunning/open-json"
 
 dependencies {
 

http://git-wip-us.apache.org/repos/asf/tapestry-5/blob/aaa0d550/tapestry-json/src/main/java/org/apache/tapestry5/json/JSON.java
----------------------------------------------------------------------
diff --git a/tapestry-json/src/main/java/org/apache/tapestry5/json/JSON.java b/tapestry-json/src/main/java/org/apache/tapestry5/json/JSON.java
new file mode 100644
index 0000000..955ee44
--- /dev/null
+++ b/tapestry-json/src/main/java/org/apache/tapestry5/json/JSON.java
@@ -0,0 +1,115 @@
+/*
+ * Copyright (C) 2010 The Android Open Source Project
+ *
+ * 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.apache.tapestry5.json;
+
+class JSON {
+    /**
+     * Returns the input if it is a JSON-permissible value; throws otherwise.
+     */
+    static double checkDouble(double d) throws RuntimeException {
+        if (Double.isInfinite(d) || Double.isNaN(d)) {
+            throw new RuntimeException("JSON does not allow non-finite numbers.");
+        }
+        return d;
+    }
+
+    static Boolean toBoolean(Object value) {
+        if (value instanceof Boolean) {
+            return (Boolean) value;
+        } else if (value instanceof String) {
+            String stringValue = (String) value;
+            if ("true".equalsIgnoreCase(stringValue)) {
+                return true;
+            } else if ("false".equalsIgnoreCase(stringValue)) {
+                return false;
+            }
+        }
+        return null;
+    }
+
+    static Double toDouble(Object value) {
+        if (value instanceof Double) {
+            return (Double) value;
+        } else if (value instanceof Number) {
+            return ((Number) value).doubleValue();
+        } else if (value instanceof String) {
+            try {
+                return Double.valueOf((String) value);
+            } catch (NumberFormatException ignored) {
+            }
+        }
+        return null;
+    }
+
+    static Integer toInteger(Object value) {
+        if (value instanceof Integer) {
+            return (Integer) value;
+        } else if (value instanceof Number) {
+            return ((Number) value).intValue();
+        } else if (value instanceof String) {
+            try {
+                return (int) Double.parseDouble((String) value);
+            } catch (NumberFormatException ignored) {
+            }
+        }
+        return null;
+    }
+
+    static Long toLong(Object value) {
+        if (value instanceof Long) {
+            return (Long) value;
+        } else if (value instanceof Number) {
+            return ((Number) value).longValue();
+        } else if (value instanceof String) {
+            try {
+                return (long) Double.parseDouble((String) value);
+            } catch (NumberFormatException ignored) {
+            }
+        }
+        return null;
+    }
+
+    static String toString(Object value) {
+        if (value instanceof String) {
+            return (String) value;
+        } else if (value != null) {
+            return String.valueOf(value);
+        }
+        return null;
+    }
+
+    static RuntimeException typeMismatch(boolean array, Object indexOrName, Object actual,
+            String requiredType) throws RuntimeException {
+        String location = array ? "JSONArray[" + indexOrName + "]" : "JSONObject[\"" + indexOrName + "\"]";
+        if (actual == null) {
+            throw new RuntimeException(location + " is null.");
+        } else {
+            throw new RuntimeException(location + " is not a " + requiredType + ".");
+        }
+    }
+
+    static RuntimeException typeMismatch(Object actual, String requiredType)
+            throws RuntimeException {
+        if (actual == null) {
+            throw new RuntimeException("Value is null.");
+        } else {
+            throw new RuntimeException("Value " + actual
+                    + " of type " + actual.getClass().getName()
+                    + " cannot be converted to " + requiredType);
+        }
+    }
+}

http://git-wip-us.apache.org/repos/asf/tapestry-5/blob/aaa0d550/tapestry-json/src/main/java/org/apache/tapestry5/json/JSONArray.java
----------------------------------------------------------------------
diff --git a/tapestry-json/src/main/java/org/apache/tapestry5/json/JSONArray.java b/tapestry-json/src/main/java/org/apache/tapestry5/json/JSONArray.java
index 385a612..0c2e8ab 100644
--- a/tapestry-json/src/main/java/org/apache/tapestry5/json/JSONArray.java
+++ b/tapestry-json/src/main/java/org/apache/tapestry5/json/JSONArray.java
@@ -1,103 +1,101 @@
-// 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.apache.tapestry5.json;
-
 /*
- * 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.
+ * Copyright (C) 2010 The Android Open Source Project
+ *
+ * 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.apache.tapestry5.json;
+
 import java.util.ArrayList;
 import java.util.Collections;
 import java.util.Iterator;
 import java.util.List;
 
+// Note: this class was written without inspecting the non-free org.json sourcecode.
+
 /**
- * A JSONArray is an ordered sequence of values. Its external text form is a string wrapped in square brackets with
- * commas separating the values. The internal form is an object having {@code get} and {@code opt} methods for
- * accessing the values by index, and {@code put} methods for adding or replacing values. The values can be any of
- * these types: {@code Boolean}, {@code JSONArray}, {@code JSONObject}, {@code Number},
- * {@code String}, or the {@code JSONObject.NULL object}.
+ * A dense indexed sequence of values. Values may be any mix of
+ * {@link JSONObject JSONObjects}, other {@link JSONArray JSONArrays}, Strings,
+ * Booleans, Integers, Longs, Doubles, {@code null} or {@link JSONObject#NULL}.
+ * Values may not be {@link Double#isNaN() NaNs}, {@link Double#isInfinite()
+ * infinities}, or of any type not listed here.
  *
- * The constructor can convert a JSON text into a Java object. The {@code toString} method converts to JSON text.
+ * {@code JSONArray} has the same type coercion behavior and
+ * optional/mandatory accessors as {@link JSONObject}. See that class'
+ * documentation for details.
  *
- * A {@code get} method returns a value if one can be found, and throws an exception if one cannot be found. An
- * {@code opt} method returns a default value instead of throwing an exception, and so is useful for obtaining
- * optional values.
+ * <strong>Warning:</strong> this class represents null in two incompatible
+ * ways: the standard Java {@code null} reference, and the sentinel value {@link
+ * JSONObject#NULL}. In particular, {@code get} fails if the requested index
+ * holds the null reference, but succeeds if it holds {@code JSONObject.NULL}.
  *
- * The generic {@code get()} and {@code opt()} methods return an object which you can cast or query for type.
- * There are also typed {@code get} and {@code opt} methods that do type checking and type coersion for you.
- *
- * The texts produced by the {@code toString} methods strictly conform to JSON syntax rules. The constructors are
- * more forgiving in the texts they will accept:
- * <ul>
- * <li>An extra {@code ,}&nbsp;<small>(comma)</small> may appear just before the closing bracket.</li>
- * <li>The {@code null} value will be inserted when there is {@code ,}&nbsp;<small>(comma)</small> elision.</li>
- * <li>Strings may be quoted with {@code '}&nbsp;<small>(single quote)</small>.</li>
- * <li>Strings do not need to be quoted at all if they do not begin with a quote or single quote, and if they do not
- * contain leading or trailing spaces, and if they do not contain any of these characters:
- * {@code { } [ ] / \ : , = ; #} and if they do not look like numbers and if they are not the reserved words
- * {@code true}, {@code false}, or {@code null}.</li>
- * <li>Values can be separated by {@code ;} <small>(semicolon)</small> as well as by {@code ,}
- * <small>(comma)</small>.</li>
- * <li>Numbers may have the {@code 0-} <small>(octal)</small> or {@code 0x-} <small>(hex)</small> prefix.</li>
- * <li>Comments written in the slashshlash, slashstar, and hash conventions will be ignored.</li>
- * </ul>
- *
- * @author JSON.org
- * @version 2
+ * Instances of this class are not thread safe.
  */
-public final class JSONArray extends JSONCollection implements Iterable<Object>
-{
+public final class JSONArray extends JSONCollection implements Iterable<Object> {
+
+    private final List<Object> values;
 
     /**
-     * The arrayList where the JSONArray's properties are kept.
+     * Creates a {@code JSONArray} with no values.
      */
-    private final List<Object> list = new ArrayList<Object>();
+    public JSONArray() {
+        values = new ArrayList<Object>();
+    }
 
     /**
-     * Construct an empty JSONArray.
+     * Creates a new {@code JSONArray} with values from the next array in the
+     * tokener.
+     *
+     * @param readFrom a tokener whose nextValue() method will yield a
+     *                 {@code JSONArray}.
+     * @throws RuntimeExeption if the parse fails or doesn't yield a
+     *                       {@code JSONArray}.
      */
-    public JSONArray()
-    {
+    JSONArray(JSONTokener readFrom) {
+        /*
+         * Getting the parser to populate this could get tricky. Instead, just
+         * parse to temporary JSONArray and then steal the data from that.
+         */
+        Object object = readFrom.nextValue(JSONArray.class);
+        if (object instanceof JSONArray) {
+            values = ((JSONArray) object).values;
+        } else {
+            throw JSON.typeMismatch(object, "JSONArray");
+        }
     }
 
-    public JSONArray(String text)
-    {
-        JSONTokener tokener = new JSONTokener(text);
-
-        parse(tokener);
+    /**
+     * Creates a new {@code JSONArray} with values from the JSON string.
+     *
+     * @param json a JSON-encoded string containing an array.
+     * @throws RuntimeExeption if the parse fails or doesn't yield a {@code
+     *                       JSONArray}.
+     */
+    public JSONArray(String json) {
+        this(new JSONTokener(json));
     }
 
-    public JSONArray(Object... values)
-    {
-        for (Object value : values)
-            put(value);
+    /**
+     * Creates a new {@code JSONArray} with values from the given primitive array.
+     *
+     * @param array The values to use.
+     * @throws RuntimeExeption if any of the values are non-finite double values (i.e. NaN or infinite)
+     */
+    public JSONArray(Object... values) {
+        this();
+        for (int i = 0; i < values.length; ++i) {
+            put(values[i]);
+        }
     }
 
     /**
@@ -115,338 +113,256 @@ public final class JSONArray extends JSONCollection implements Iterable<Object>
         return new JSONArray().putAll(iterable);
     }
 
-    @Override
-    public Iterator<Object> iterator()
-    {
-        return list.iterator();
-    }
-
     /**
-     * Construct a JSONArray from a JSONTokener.
-     *
-     * @param tokenizer
-     *         A JSONTokener
-     * @throws RuntimeException
-     *         If there is a syntax error.
+     * @return Returns the number of values in this array.
      */
-    JSONArray(JSONTokener tokenizer)
-    {
-        assert tokenizer != null;
-
-        parse(tokenizer);
-    }
-
-    private void parse(JSONTokener tokenizer)
-    {
-        if (tokenizer.nextClean() != '[')
-        {
-            throw tokenizer.syntaxError("A JSONArray text must start with '['");
-        }
-
-        if (tokenizer.nextClean() == ']')
-        {
-            return;
-        }
-
-        tokenizer.back();
-
-        while (true)
-        {
-            if (tokenizer.nextClean() == ',')
-            {
-                tokenizer.back();
-                list.add(JSONObject.NULL);
-            } else
-            {
-                tokenizer.back();
-                list.add(tokenizer.nextValue());
-            }
-
-            switch (tokenizer.nextClean())
-            {
-                case ';':
-                case ',':
-                    if (tokenizer.nextClean() == ']')
-                    {
-                        return;
-                    }
-                    tokenizer.back();
-                    break;
-
-                case ']':
-                    return;
-
-                default:
-                    throw tokenizer.syntaxError("Expected a ',' or ']'");
-            }
-        }
+    public int length() {
+        return values.size();
     }
 
     /**
-     * Get the object value associated with an index.
+     * Appends {@code value} to the end of this array.
      *
-     * @param index
-     *         The index must be between 0 and length() - 1.
-     * @return An object value.
-     * @throws RuntimeException
-     *         If there is no value for the index.
+     * @param value a {@link JSONObject}, {@link JSONArray}, String, Boolean,
+     *              Integer, Long, Double, or {@link JSONObject#NULL}}. May
+     *              not be {@link Double#isNaN() NaNs} or {@link Double#isInfinite()
+     *              infinities}. Unsupported values are not permitted and will cause the
+     *              array to be in an inconsistent state.
+     * @return this array.
      */
-    public Object get(int index)
-    {
-        return list.get(index);
+    public JSONArray put(Object value) {
+        JSONObject.testValidity(value);
+        values.add(value);
+        return this;
     }
 
     /**
-     * Remove the object associated with the index.
+     * Same as {@link #put}, with added validity checks.
      *
-     * @param index
-     *         The index must be between 0 and length() - 1.
-     * @return An object removed.
-     * @throws RuntimeException
-     *         If there is no value for the index.
+     * @param value The value to append.
      */
-    public Object remove(int index)
-    {
-        return list.remove(index);
+    void checkedPut(Object value) {
+        JSONObject.testValidity(value);
+        if (value instanceof Number) {
+            JSON.checkDouble(((Number) value).doubleValue());
+        }
+
+        put(value);
     }
 
     /**
-     * Get the boolean value associated with an index. The string values "true" and "false" are converted to boolean.
+     * Sets the value at {@code index} to {@code value}, null padding this array
+     * to the required length if necessary. If a value already exists at {@code
+     * index}, it will be replaced.
      *
-     * @param index
-     *         The index must be between 0 and length() - 1.
-     * @return The truth.
-     * @throws RuntimeException
-     *         If there is no value for the index or if the value is not convertable to boolean.
+     * @param index Where to put the value.
+     * @param value a {@link JSONObject}, {@link JSONArray}, String, Boolean,
+     *              Integer, Long, Double, {@link JSONObject#NULL}, or {@code null}. May
+     *              not be {@link Double#isNaN() NaNs} or {@link Double#isInfinite()
+     *              infinities}.
+     * @return this array.
+     * @throws RuntimeExeption If the value cannot be represented as a finite double value.
      */
-    public boolean getBoolean(int index)
-    {
-        Object value = get(index);
-
-        if (value instanceof Boolean)
+    public JSONArray put(int index, Object value) {
+        if (index < 0)
         {
-            return (Boolean) value;
+            throw new RuntimeException("JSONArray[" + index + "] not found.");
         }
-
-        if (value instanceof String)
-        {
-            String asString = (String) value;
-
-            if (asString.equalsIgnoreCase("false"))
-                return false;
-
-            if (asString.equalsIgnoreCase("true"))
-                return true;
+        JSONObject.testValidity(value);
+        if (value instanceof Number) {
+            // deviate from the original by checking all Numbers, not just floats & doubles
+            JSON.checkDouble(((Number) value).doubleValue());
         }
-
-        throw new RuntimeException("JSONArray[" + index + "] is not a Boolean.");
+        while (values.size() <= index) {
+            values.add(null);
+        }
+        values.set(index, value);
+        return this;
     }
 
     /**
-     * Get the double value associated with an index.
+     * Returns true if this array has no value at {@code index}, or if its value
+     * is the {@code null} reference or {@link JSONObject#NULL}.
      *
-     * @param index
-     *         The index must be between 0 and length() - 1.
-     * @return The value.
-     * @throws IllegalArgumentException
-     *         If the key is not found or if the value cannot be converted to a number.
+     * @param index Which value to check.
+     * @return true if the value is null.
      */
-    public double getDouble(int index)
-    {
-        Object value = get(index);
-
-        try
-        {
-            if (value instanceof Number)
-                return ((Number) value).doubleValue();
-
-            return Double.valueOf((String) value);
-        } catch (Exception e)
-        {
-            throw new IllegalArgumentException("JSONArray[" + index + "] is not a number.");
-        }
+    public boolean isNull(int index) {
+        Object value = values.get(index);
+        return value == null || value == JSONObject.NULL;
     }
 
     /**
-     * Get the int value associated with an index.
+     * Returns the value at {@code index}.
      *
-     * @param index
-     *         The index must be between 0 and length() - 1.
-     * @return The value.
-     * @throws IllegalArgumentException
-     *         If the key is not found or if the value cannot be converted to a number. if the
-     *         value cannot be converted to a number.
+     * @param index Which value to get.
+     * @return the value at the specified location.
+     * @throws RuntimeExeption if this array has no value at {@code index}, or if
+     *                       that value is the {@code null} reference. This method returns
+     *                       normally if the value is {@code JSONObject#NULL}.
      */
-    public int getInt(int index)
-    {
-        Object o = get(index);
-        return o instanceof Number ? ((Number) o).intValue() : (int) getDouble(index);
+    public Object get(int index) {
+        try {
+            Object value = values.get(index);
+            if (value == null) {
+                throw new RuntimeException("Value at " + index + " is null.");
+            }
+            return value;
+        } catch (IndexOutOfBoundsException e) {
+            throw new RuntimeException("Index " + index + " out of range [0.." + values.size() + ")");
+        }
     }
 
     /**
-     * Get the JSONArray associated with an index.
+     * Removes and returns the value at {@code index}, or null if the array has no value
+     * at {@code index}.
      *
-     * @param index
-     *         The index must be between 0 and length() - 1.
-     * @return A JSONArray value.
-     * @throws RuntimeException
-     *         If there is no value for the index. or if the value is not a JSONArray
+     * @param index Which value to remove.
+     * @return The value previously at the specified location.
      */
-    public JSONArray getJSONArray(int index)
-    {
-        Object o = get(index);
-        if (o instanceof JSONArray)
-        {
-            return (JSONArray) o;
+    public Object remove(int index) {
+        if (index < 0 || index >= values.size()) {
+            return null;
         }
-
-        throw new RuntimeException("JSONArray[" + index + "] is not a JSONArray.");
+        return values.remove(index);
     }
 
     /**
-     * Get the JSONObject associated with an index.
+     * Returns the value at {@code index} if it exists and is a boolean or can
+     * be coerced to a boolean.
      *
-     * @param index
-     *         subscript
-     * @return A JSONObject value.
-     * @throws RuntimeException
-     *         If there is no value for the index or if the value is not a JSONObject
+     * @param index Which value to get.
+     * @return the value at the specified location.
+     * @throws RuntimeExeption if the value at {@code index} doesn't exist or
+     *                       cannot be coerced to a boolean.
      */
-    public JSONObject getJSONObject(int index)
-    {
-        Object o = get(index);
-        if (o instanceof JSONObject)
-        {
-            return (JSONObject) o;
+    public boolean getBoolean(int index) {
+        Object object = get(index);
+        Boolean result = JSON.toBoolean(object);
+        if (result == null) {
+            throw JSON.typeMismatch(true, index, object, "Boolean");
         }
-
-        throw new RuntimeException("JSONArray[" + index + "] is not a JSONObject.");
+        return result;
     }
 
     /**
-     * Get the long value associated with an index.
+     * Returns the value at {@code index} if it exists and is a double or can
+     * be coerced to a double.
      *
-     * @param index
-     *         The index must be between 0 and length() - 1.
-     * @return The value.
-     * @throws IllegalArgumentException
-     *         If the key is not found or if the value cannot be converted to a number.
+     * @param index Which value to get.
+     * @return the value at the specified location.
+     * @throws RuntimeExeption if the value at {@code index} doesn't exist or
+     *                       cannot be coerced to a double.
      */
-    public long getLong(int index)
-    {
-        Object o = get(index);
-        return o instanceof Number ? ((Number) o).longValue() : (long) getDouble(index);
+    public double getDouble(int index) {
+        Object object = get(index);
+        Double result = JSON.toDouble(object);
+        if (result == null) {
+            throw JSON.typeMismatch(true, index, object, "number");
+        }
+        return result;
     }
 
     /**
-     * Get the string associated with an index.
+     * Returns the value at {@code index} if it exists and is an int or
+     * can be coerced to an int.
      *
-     * @param index
-     *         The index must be between 0 and length() - 1.
-     * @return A string value.
-     * @throws RuntimeException
-     *         If there is no value for the index.
+     * @param index Which value to get.
+     * @return the value at the specified location.
+     * @throws RuntimeExeption if the value at {@code index} doesn't exist or
+     *                       cannot be coerced to a int.
      */
-    public String getString(int index)
-    {
-        return get(index).toString();
+    public int getInt(int index) {
+        Object object = get(index);
+        Integer result = JSON.toInteger(object);
+        if (result == null) {
+            throw JSON.typeMismatch(true, index, object, "int");
+        }
+        return result;
     }
 
     /**
-     * Determine if the value is null.
+     * Returns the value at {@code index} if it exists and is a long or
+     * can be coerced to a long.
      *
-     * @param index
-     *         The index must be between 0 and length() - 1.
-     * @return true if the value at the index is null, or if there is no value.
+     * @param index Which value to get.
+     * @return the value at the specified location.
+     * @throws RuntimeExeption if the value at {@code index} doesn't exist or
+     *                       cannot be coerced to a long.
      */
-    public boolean isNull(int index)
-    {
-        return get(index) == JSONObject.NULL;
+    public long getLong(int index) {
+        Object object = get(index);
+        Long result = JSON.toLong(object);
+        if (result == null) {
+            throw JSON.typeMismatch(true, index, object, "long");
+        }
+        return result;
     }
 
     /**
-     * Get the number of elements in the JSONArray, included nulls.
+     * Returns the value at {@code index} if it exists, coercing it if
+     * necessary.
      *
-     * @return The length (or size).
+     * @param index Which value to get.
+     * @return the value at the specified location.
+     * @throws RuntimeExeption if no such value exists.
      */
-    public int length()
-    {
-        return list.size();
+    public String getString(int index) {
+        Object object = get(index);
+        String result = JSON.toString(object);
+        if (result == null) {
+            throw JSON.typeMismatch(true, index, object, "String");
+        }
+        return result;
     }
 
     /**
-     * Append an object value. This increases the array's length by one.
+     * Returns the value at {@code index} if it exists and is a {@code
+     * JSONArray}.
      *
-     * @param value
-     *         An object value. The value should be a Boolean, Double, Integer, JSONArray, JSONObject, JSONLiteral,
-     *         Long, or String, or the JSONObject.NULL singleton.
-     * @return this array
+     * @param index Which value to get.
+     * @return the value at the specified location.
+     * @throws RuntimeExeption if the value doesn't exist or is not a {@code
+     *                       JSONArray}.
      */
-    public JSONArray put(Object value)
-    {
-        // now testValidity checks for null values.
-        // assert value != null;
-
-        JSONObject.testValidity(value);
-
-        list.add(value);
-
-        return this;
+    public JSONArray getJSONArray(int index) {
+        Object object = get(index);
+        if (object instanceof JSONArray) {
+            return (JSONArray) object;
+        } else {
+            throw JSON.typeMismatch(true, index, object, "JSONArray");
+        }
     }
 
     /**
-     * Put or replace an object value in the JSONArray. If the index is greater than the length of the JSONArray, then
-     * null elements will be added as necessary to pad it out.
+     * Returns the value at {@code index} if it exists and is a {@code
+     * JSONObject}.
      *
-     * @param index
-     *         The subscript.
-     * @param value
-     *         The value to put into the array. The value should be a Boolean, Double, Integer, JSONArray,
-     *         JSONObject, JSONString, Long, or String, or the JSONObject.NULL singeton.
-     * @return this array
-     * @throws RuntimeException
-     *         If the index is negative or if the the value is an invalid number.
+     * @param index Which value to get.
+     * @return the value at the specified location.
+     * @throws RuntimeExeption if the value doesn't exist or is not a {@code
+     *                       JSONObject}.
      */
-    public JSONArray put(int index, Object value)
-    {
-        assert value != null;
-
-        if (index < 0)
-        {
-            throw new RuntimeException("JSONArray[" + index + "] not found.");
+    public JSONObject getJSONObject(int index) {
+        Object object = get(index);
+        if (object instanceof JSONObject) {
+            return (JSONObject) object;
+        } else {
+            throw JSON.typeMismatch(true, index, object, "JSONObject");
         }
-
-        JSONObject.testValidity(value);
-
-        if (index < length())
-        {
-            list.set(index, value);
-        } else
-        {
-            while (index != length())
-                list.add(JSONObject.NULL);
-
-            list.add(value);
-        }
-
-        return this;
     }
 
     @Override
-    public boolean equals(Object obj)
-    {
-        if (obj == null)
-            return false;
-
-        if (!(obj instanceof JSONArray))
-            return false;
-
-        JSONArray other = (JSONArray) obj;
-
-        return list.equals(other.list);
+    public boolean equals(Object o) {
+        return o instanceof JSONArray && ((JSONArray) o).values.equals(values);
     }
 
     @Override
+    public int hashCode() {
+        // diverge from the original, which doesn't implement hashCode
+        return values.hashCode();
+    }
+
     void print(JSONPrintSession session)
     {
         session.printSymbol('[');
@@ -455,7 +371,7 @@ public final class JSONArray extends JSONCollection implements Iterable<Object>
 
         boolean comma = false;
 
-        for (Object value : list)
+        for (Object value : values)
         {
             if (comma)
                 session.printSymbol(',');
@@ -505,6 +421,15 @@ public final class JSONArray extends JSONCollection implements Iterable<Object>
      */
     public List<Object> toList()
     {
-        return Collections.unmodifiableList(list);
+        return Collections.unmodifiableList(values);
     }
+
+
+    @Override
+    public Iterator<Object> iterator()
+    {
+        return values.iterator();
+    }
+
+
 }

http://git-wip-us.apache.org/repos/asf/tapestry-5/blob/aaa0d550/tapestry-json/src/main/java/org/apache/tapestry5/json/JSONObject.java
----------------------------------------------------------------------
diff --git a/tapestry-json/src/main/java/org/apache/tapestry5/json/JSONObject.java b/tapestry-json/src/main/java/org/apache/tapestry5/json/JSONObject.java
index a9555b8..0073ad9 100644
--- a/tapestry-json/src/main/java/org/apache/tapestry5/json/JSONObject.java
+++ b/tapestry-json/src/main/java/org/apache/tapestry5/json/JSONObject.java
@@ -1,132 +1,113 @@
-// 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.apache.tapestry5.json;
-
 /*
- * 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.
+ * Copyright (C) 2010 The Android Open Source Project
+ *
+ * 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.apache.tapestry5.json;
+
 import java.io.ObjectStreamException;
 import java.io.Serializable;
-import java.util.*;
+import java.util.Collections;
+import java.util.LinkedHashMap;
+import java.util.Map;
+import java.util.Set;
+
+// Note: this class was written without inspecting the non-free org.json sourcecode.
 
 /**
- * A JSONObject is an unordered collection of name/value pairs. Its external form is a string wrapped in curly braces
- * with colons between the names and values, and commas between the values and names. The internal form is an object
- * having <code>get</code> and <code>opt</code> methods for accessing the values by name, and <code>put</code> methods
- * for adding or replacing values by name. The values can be any of these types: <code>Boolean</code>,
- * {@link org.apache.tapestry5.json.JSONArray}, {@link org.apache.tapestry5.json.JSONLiteral}, <code>JSONObject</code>,
- * <code>Number</code>, <code>String</code>, or the <code>JSONObject.NULL</code> object. A JSONObject constructor can be
- * used to convert an external form JSON text into
- * an internal form whose values can be retrieved with the <code>get</code> and <code>opt</code> methods, or to convert
- * values into a JSON text using the <code>put</code> and <code>toString</code> methods. A <code>get</code> method
- * returns a value if one can be found, and throws an exception if one cannot be found. An <code>opt</code> method
- * returns a default value instead of throwing an exception, and so is useful for obtaining optional values.
- *
- * The generic <code>get()</code> and <code>opt()</code> methods return an object, which you can cast or query for type.
- * There are also typed <code>get</code> and <code>opt</code> methods that do type checking and type coersion for you.
- *
- * The <code>put</code> methods adds values to an object. For example,
- *
- * <pre>
- * myString = new JSONObject().put(&quot;JSON&quot;, &quot;Hello, World!&quot;).toString();
- * </pre>
+ * A modifiable set of name/value mappings. Names are unique, non-null strings.
+ * Values may be any mix of {@link JSONObject JSONObjects}, {@link JSONArray
+ * JSONArrays}, Strings, Booleans, Integers, Longs, Doubles or {@link #NULL}.
+ * Values may not be {@code null}, {@link Double#isNaN() NaNs}, {@link
+ * Double#isInfinite() infinities}, or of any type not listed here.
  *
- * produces the string <code>{"JSON": "Hello, World"}</code>.
- *
- * The texts produced by the <code>toString</code> methods strictly conform to the JSON syntax rules. The constructors
- * are more forgiving in the texts they will accept:
+ * <p>This class can coerce values to another type when requested.
  * <ul>
- * <li>An extra <code>,</code>&nbsp;<small>(comma)</small> may appear just before the closing brace.</li>
- * <li>Strings may be quoted with <code>'</code>&nbsp;<small>(single quote)</small>.</li>
- * <li>Strings do not need to be quoted at all if they do not begin with a quote or single quote, and if they do not
- * contain leading or trailing spaces, and if they do not contain any of these characters: <code>{ }
- * [ ] / \ : , = ; #</code> and if they do not look like numbers and if they are not the reserved words
- * <code>true</code>, <code>false</code>, or <code>null</code>.</li>
- * <li>Keys can be followed by <code>=</code> or {@code =>} as well as by {@code :}.</li>
- * <li>Values can be followed by <code>;</code> <small>(semicolon)</small> as well as by <code>,</code>
- * <small>(comma)</small>.</li>
- * <li>Numbers may have the <code>0-</code> <small>(octal)</small> or <code>0x-</code> <small>(hex)</small> prefix.</li>
- * <li>Comments written in the slashshlash, slashstar, and hash conventions will be ignored.</li>
+ * <li>When the requested type is a boolean, strings will be coerced using a
+ * case-insensitive comparison to "true" and "false".
+ * <li>When the requested type is a double, other {@link Number} types will
+ * be coerced using {@link Number#doubleValue() doubleValue}. Strings
+ * that can be coerced using {@link Double#valueOf(String)} will be.
+ * <li>When the requested type is an int, other {@link Number} types will
+ * be coerced using {@link Number#intValue() intValue}. Strings
+ * that can be coerced using {@link Double#valueOf(String)} will be,
+ * and then cast to int.
+ * <li><a name="lossy">When the requested type is a long, other {@link Number} types will
+ * be coerced using {@link Number#longValue() longValue}. Strings
+ * that can be coerced using {@link Double#valueOf(String)} will be,
+ * and then cast to long. This two-step conversion is lossy for very
+ * large values. For example, the string "9223372036854775806" yields the
+ * long 9223372036854775807.</a>
+ * <li>When the requested type is a String, other non-null values will be
+ * coerced using {@link String#valueOf(Object)}. Although null cannot be
+ * coerced, the sentinel value {@link JSONObject#NULL} is coerced to the
+ * string "null".
  * </ul>
- * <hr>
  *
- * This class, and the other related classes, have been heavily modified from the original source, to fit Tapestry
- * standards and to make use of JDK 1.5 features such as generics. Further, since the interest of Tapestry is primarily
- * constructing JSON (and not parsing it), many of the non-essential methods have been removed (since the original code
- * came with no tests).
+ * <p>This class can look up both mandatory and optional values:
+ * <ul>
+ * <li>Use <code>get<i>Type</i>()</code> to retrieve a mandatory value. This
+ * fails with a {@code RuntimeException} if the requested name has no value
+ * or if the value cannot be coerced to the requested type.
+ * <li>Use <code>opt()</code> to retrieve an optional value.
+ * </ul>
  *
- * Finally, support for the {@link org.apache.tapestry5.json.JSONLiteral} type has been added, which allows the exact
- * output to be controlled; useful when a JSONObject is being used as a configuration object, and must contain values
- * that are not simple data, such as an inline function (making the result not JSON).
+ * <p><strong>Warning:</strong> this class represents null in two incompatible
+ * ways: the standard Java {@code null} reference, and the sentinel value {@link
+ * JSONObject#NULL}. In particular, calling {@code put(name, null)} removes the
+ * named entry from the object but {@code put(name, JSONObject.NULL)} stores an
+ * entry whose value is {@code JSONObject.NULL}.
  *
- * @author JSON.org
- * @version 2
+ * <p>Instances of this class are not thread safe.
  */
-@SuppressWarnings(
-        {"CloneDoesntCallSuperClone"})
-public final class JSONObject extends JSONCollection
-{
+public final class JSONObject extends JSONCollection {
+
+    private static final Double NEGATIVE_ZERO = -0d;
 
     /**
-     * JSONObject.NULL is equivalent to the value that JavaScript calls null, whilst Java's null is equivalent to the
-     * value that JavaScript calls undefined.
+     * A sentinel value used to explicitly define a name with no value. Unlike
+     * {@code null}, names with this value:
+     * <ul>
+     * <li>show up in the {@link #names} array
+     * <li>show up in the {@link #keys} iterator
+     * <li>return {@code true} for {@link #has(String)}
+     * <li>do not throw on {@link #get(String)}
+     * <li>are included in the encoded JSON string.
+     * </ul>
+     *
+     * <p>This value violates the general contract of {@link Object#equals} by
+     * returning true when compared to {@code null}. Its {@link #toString}
+     * method returns "null".
      */
-    private static final class Null implements JSONString, Serializable
-    {
-        /**
-         * 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.
-         */
+    public static final Object NULL = new Serializable() {
+
+      private static final long serialVersionUID = 1L;
+
+        @SuppressWarnings("EqualsWhichDoesntCheckParameterClass")
         @Override
-        public boolean equals(Object object)
-        {
-            return object == null || object == this;
+        public boolean equals(Object o) {
+            return o == this || o == null; // API specifies this broken equals implementation
         }
 
-        /**
-         * Get the "null" string value.
-         *
-         * @return The string "null".
-         */
+        // at least make the broken equals(null) consistent with Objects.hashCode(null).
         @Override
-        public String toString()
-        {
-            return "null";
+        public int hashCode() {
+            return 0;
         }
 
         @Override
-        public String toJSONString()
-        {
+        public String toString() {
             return "null";
         }
 
@@ -135,27 +116,72 @@ public final class JSONObject extends JSONCollection
         {
             return NULL;
         }
+
+    };
+
+    private final LinkedHashMap<String, Object> nameValuePairs;
+
+    /**
+     * Creates a {@code JSONObject} with no name/value mappings.
+     */
+    public JSONObject() {
+        nameValuePairs = new LinkedHashMap<String, Object>();
     }
 
     /**
-     * The map where the JSONObject's properties are kept.
+     * Creates a new {@code JSONObject} with name/value mappings from the next
+     * object in the tokener.
+     *
+     * @param readFrom a tokener whose nextValue() method will yield a
+     *                 {@code JSONObject}.
+     * @throws RuntimeException if the parse fails or doesn't yield a
+     *                       {@code JSONObject}.
      */
-    private final Map<String, Object> properties = new LinkedHashMap<String, Object>();
+    JSONObject(JSONTokener readFrom) {
+        /*
+         * Getting the parser to populate this could get tricky. Instead, just
+         * parse to temporary JSONObject and then steal the data from that.
+         */
+        Object object = readFrom.nextValue(JSONObject.class);
+        if (object instanceof JSONObject) {
+            this.nameValuePairs = ((JSONObject) object).nameValuePairs;
+        } else {
+            throw JSON.typeMismatch(object, "JSONObject");
+        }
+    }
 
     /**
-     * It is sometimes more convenient and less ambiguous to have a <code>NULL</code> object than to use Java's
-     * <code>null</code> value. <code>JSONObject.NULL.equals(null)</code> returns <code>true</code>.
-     * <code>JSONObject.NULL.toString()</code> returns <code>"null"</code>.
+     * Creates a new {@code JSONObject} with name/value mappings from the JSON
+     * string.
+     *
+     * @param json a JSON-encoded string containing an object.
+     * @throws RuntimeException if the parse fails or doesn't yield a {@code
+     *                       JSONObject}.
      */
-    public static final Object NULL = new Null();
+    public JSONObject(String json) {
+        this(new JSONTokener(json));
+    }
 
     /**
-     * Construct an empty JSONObject.
+     * Creates a new {@code JSONObject} by copying mappings for the listed names
+     * from the given object. Names that aren't present in {@code copyFrom} will
+     * be skipped.
+     *
+     * @param copyFrom The source object.
+     * @param names    The names of the fields to copy.
+     * @throws RuntimeException On internal errors. Shouldn't happen.
      */
-    public JSONObject()
-    {
+    public JSONObject(JSONObject copyFrom, String[] names) {
+        this();
+        for (String name : names) {
+            Object value = copyFrom.opt(name);
+            if (value != null) {
+                nameValuePairs.put(name, value);
+            }
+        }
     }
 
+
     /**
      * Returns a new JSONObject that is a shallow copy of this JSONObject.
      *
@@ -164,7 +190,7 @@ public final class JSONObject extends JSONCollection
     public JSONObject copy()
     {
         JSONObject dupe = new JSONObject();
-        dupe.properties.putAll(properties);
+        dupe.nameValuePairs.putAll(nameValuePairs);
 
         return dupe;
     }
@@ -181,6 +207,8 @@ public final class JSONObject extends JSONCollection
      */
     public JSONObject(Object... keysAndValues)
     {
+        this();
+
         int i = 0;
 
         while (i < keysAndValues.length)
@@ -190,679 +218,399 @@ public final class JSONObject extends JSONCollection
     }
 
     /**
-     * Construct a JSONObject from a subset of another JSONObject. An array of strings is used to identify the keys that
-     * should be copied. Missing keys are ignored.
+     * Returns the number of name/value mappings in this object.
      *
-     * @param source
-     *         A JSONObject.
-     * @param propertyNames
-     *         The strings to copy.
-     * @throws RuntimeException
-     *         If a value is a non-finite number.
+     * @return the length of this.
      */
-    public JSONObject(JSONObject source, String... propertyNames)
-    {
-        for (String name : propertyNames)
-        {
-            Object value = source.opt(name);
-
-            if (value != null)
-                put(name, value);
-        }
+    public int length() {
+        return nameValuePairs.size();
     }
 
     /**
-     * Construct a JSONObject from a JSONTokener.
+     * Maps {@code name} to {@code value}, clobbering any existing name/value
+     * mapping with the same name. If the value is {@code null}, any existing
+     * mapping for {@code name} is removed.
      *
-     * @param x
-     *         A JSONTokener object containing the source string. @ If there is a syntax error in the source string.
+     * @param name  The name of the new value.
+     * @param value a {@link JSONObject}, {@link JSONArray}, String, Boolean,
+     *              Integer, Long, Double, {@link #NULL}, or {@code null}. May not be
+     *              {@link Double#isNaN() NaNs} or {@link Double#isInfinite()
+     *              infinities}.
+     * @return this object.
+     * @throws RuntimeException if the value is an invalid double (infinite or NaN).
      */
-    JSONObject(JSONTokener x)
-    {
-        String key;
-
-        if (x.nextClean() != '{')
-        {
-            throw x.syntaxError("A JSONObject text must begin with '{'");
+    public JSONObject put(String name, Object value) {
+        if (value == null) {
+            nameValuePairs.remove(name);
+            return this;
         }
-
-        while (true)
-        {
-            char c = x.nextClean();
-            switch (c)
-            {
-                case 0:
-                    throw x.syntaxError("A JSONObject text must end with '}'");
-                case '}':
-                    return;
-                default:
-                    x.back();
-                    key = x.nextValue().toString();
-            }
-
-            /*
-             * The key is followed by ':'. We will also tolerate '=' or '=>'.
-             */
-
-            c = x.nextClean();
-            if (c == '=')
-            {
-                if (x.next() != '>')
-                {
-                    x.back();
-                }
-            } else if (c != ':')
-            {
-                throw x.syntaxError("Expected a ':' after a key");
-            }
-            put(key, x.nextValue());
-
-            /*
-             * Pairs are separated by ','. We will also tolerate ';'.
-             */
-
-            switch (x.nextClean())
-            {
-                case ';':
-                case ',':
-                    if (x.nextClean() == '}')
-                    {
-                        return;
-                    }
-                    x.back();
-                    break;
-                case '}':
-                    return;
-                default:
-                    throw x.syntaxError("Expected a ',' or '}'");
-            }
+        testValidity(value);
+        if (value instanceof Number) {
+            // deviate from the original by checking all Numbers, not just floats & doubles
+            JSON.checkDouble(((Number) value).doubleValue());
         }
+        nameValuePairs.put(checkName(name), value);
+        return this;
     }
 
     /**
-     * Construct a JSONObject from a string. This is the most commonly used JSONObject constructor.
-     *
-     * @param string
-     *         A string beginning with <code>{</code>&nbsp;<small>(left brace)</small> and ending with <code>}</code>
-     *         &nbsp;<small>(right brace)</small>.
-     * @throws RuntimeException
-     *         If there is a syntax error in the source string.
-     */
-    public JSONObject(String string)
-    {
-        this(new JSONTokener(string));
+     * Appends {@code value} to the array already mapped to {@code name}. If
+     * this object has no mapping for {@code name}, this inserts a new mapping.
+     * If the mapping exists but its value is not an array, the existing
+     * and new values are inserted in order into a new array which is itself
+     * mapped to {@code name}. In aggregate, this allows values to be added to a
+     * mapping one at a time.
+     *
+     * Note that {@code append(String, Object)} provides better semantics.
+     * In particular, the mapping for {@code name} will <b>always</b> be a
+     * {@link JSONArray}. Using {@code accumulate} will result in either a
+     * {@link JSONArray} or a mapping whose type is the type of {@code value}
+     * depending on the number of calls to it.
+     *
+     * @param name  The name of the field to change.
+     * @param value a {@link JSONObject}, {@link JSONArray}, String, Boolean,
+     *              Integer, Long, Double, {@link #NULL} or null. May not be {@link
+     *              Double#isNaN() NaNs} or {@link Double#isInfinite() infinities}.
+     * @return this object after mutation.
+     * @throws RuntimeException If the object being added is an invalid number.
+     */
+    // TODO: Change {@code append) to {@link #append} when append is
+    // unhidden.
+    public JSONObject accumulate(String name, Object value) {
+        Object current = nameValuePairs.get(checkName(name));
+        if (current == null) {
+            return put(name, value);
+        }
+
+        if (current instanceof JSONArray) {
+            JSONArray array = (JSONArray) current;
+            array.checkedPut(value);
+        } else {
+            JSONArray array = new JSONArray();
+            array.checkedPut(current);
+            array.checkedPut(value);
+            nameValuePairs.put(name, array);
+        }
+        return this;
     }
 
     /**
-     * Accumulate values under a key. It is similar to the put method except that if there is already an object stored
-     * under the key then a JSONArray is stored under the key to hold all of the accumulated values. If there is already
-     * a JSONArray, then the new value is appended to it. In contrast, the put method replaces the previous value.
+     * Appends values to the array mapped to {@code name}. A new {@link JSONArray}
+     * mapping for {@code name} will be inserted if no mapping exists. If the existing
+     * mapping for {@code name} is not a {@link JSONArray}, a {@link RuntimeException}
+     * will be thrown.
      *
-     * @param key
-     *         A key string.
-     * @param value
-     *         An object to be accumulated under the key.
-     * @return this.
-     * @throws RuntimeException if the value is an invalid number or if the key is null.
+     * @param name  The name of the array to which the value should be appended.
+     * @param value The value to append.
+     * @return this object.
+     * @throws RuntimeException if {@code name} is {@code null} or if the mapping for
+     *                       {@code name} is non-null and is not a {@link JSONArray}.
      */
-    public JSONObject accumulate(String key, Object value)
-    {
+    public JSONObject append(String name, Object value) {
         testValidity(value);
+        Object current = nameValuePairs.get(checkName(name));
 
-        Object existing = opt(key);
-
-        if (existing == null)
-        {
-            // Note that the original implementation of this method contradicted the method
-            // documentation.
-            put(key, value);
-            return this;
-        }
-
-        if (existing instanceof JSONArray)
-        {
-            ((JSONArray) existing).put(value);
-            return this;
+        final JSONArray array;
+        if (current instanceof JSONArray) {
+            array = (JSONArray) current;
+        } else if (current == null) {
+            JSONArray newArray = new JSONArray();
+            nameValuePairs.put(name, newArray);
+            array = newArray;
+        } else {
+            throw new RuntimeException("JSONObject[\"" + name + "\"] is not a JSONArray.");
         }
 
-        // Replace the existing value, of any type, with an array that includes both the
-        // existing and the new value.
-
-        put(key, new JSONArray().put(existing).put(value));
+        array.checkedPut(value);
 
         return this;
     }
 
-    /**
-     * Append values to the array under a key. If the key does not exist in the JSONObject, then the key is put in the
-     * JSONObject with its value being a JSONArray containing the value parameter. If the key was already associated
-     * with a JSONArray, then the value parameter is appended to it.
-     *
-     * @param key
-     *         A key string.
-     * @param value
-     *         An object to be accumulated under the key.
-     * @return this. @ If the key is null or if the current value associated with the key is not a JSONArray.
-     */
-    public JSONObject append(String key, Object value)
-    {
-        testValidity(value);
-        Object o = opt(key);
-        if (o == null)
-        {
-            put(key, new JSONArray().put(value));
-        } else if (o instanceof JSONArray)
-        {
-            put(key, ((JSONArray) o).put(value));
-        } else
-        {
-            throw new RuntimeException("JSONObject[" + quote(key) + "] is not a JSONArray.");
+    String checkName(String name) {
+        if (name == null) {
+            throw new RuntimeException("Names must be non-null");
         }
-
-        return this;
+        return name;
     }
 
     /**
-     * Produce a string from a double. The string "null" will be returned if the number is not finite.
+     * Removes the named mapping if it exists; does nothing otherwise.
      *
-     * @param d
-     *         A double.
-     * @return A String.
+     * @param name The name of the mapping to remove.
+     * @return the value previously mapped by {@code name}, or null if there was
+     * no such mapping.
      */
-    static String doubleToString(double d)
-    {
-        if (Double.isInfinite(d) || Double.isNaN(d))
-        {
-            return "null";
-        }
-
-        // Shave off trailing zeros and decimal point, if possible.
-
-        String s = Double.toString(d);
-        if (s.indexOf('.') > 0 && s.indexOf('e') < 0 && s.indexOf('E') < 0)
-        {
-            while (s.endsWith("0"))
-            {
-                s = s.substring(0, s.length() - 1);
-            }
-            if (s.endsWith("."))
-            {
-                s = s.substring(0, s.length() - 1);
-            }
-        }
-        return s;
+    public Object remove(String name) {
+        return nameValuePairs.remove(name);
     }
 
     /**
-     * Get the value object associated with a key.
+     * Returns true if this object has no mapping for {@code name} or if it has
+     * a mapping whose value is {@link #NULL}.
      *
-     * @param key
-     *         A key string.
-     * @return The object associated with the key.
-     * @throws RuntimeException
-     *         if the key is not found.
-     * @see #opt(String)
+     * @param name The name of the value to check on.
+     * @return true if the field doesn't exist or is null.
      */
-    public Object get(String key)
-    {
-        Object o = opt(key);
-        if (o == null)
-        {
-            throw new RuntimeException("JSONObject[" + quote(key) + "] not found.");
-        }
-
-        return o;
+    public boolean isNull(String name) {
+        Object value = nameValuePairs.get(name);
+        return value == null || value == NULL;
     }
 
     /**
-     * Get the boolean value associated with a key.
+     * Returns true if this object has a mapping for {@code name}. The mapping
+     * may be {@link #NULL}.
      *
-     * @param key
-     *         A key string.
-     * @return The truth.
-     * @throws RuntimeException
-     *         if the value does not exist, is not a Boolean or the String "true" or "false".
+     * @param name The name of the value to check on.
+     * @return true if this object has a field named {@code name}
      */
-    public boolean getBoolean(String key)
-    {
-        Object o = get(key);
-
-        if (o instanceof Boolean)
-            return o.equals(Boolean.TRUE);
-
-        if (o instanceof String)
-        {
-            String value = (String) o;
-
-            if (value.equalsIgnoreCase("true"))
-                return true;
-
-            if (value.equalsIgnoreCase("false"))
-                return false;
-        }
-
-        throw new RuntimeException("JSONObject[" + quote(key) + "] is not a Boolean.");
+    public boolean has(String name) {
+        return nameValuePairs.containsKey(name);
     }
 
     /**
-     * Get the double value associated with a key.
+     * Returns the value mapped by {@code name}, or throws if no such mapping exists.
      *
-     * @param key
-     *         A key string.
-     * @return The numeric value. @throws RuntimeException if the key is not found or if the value is not a Number object and cannot be
-     *         converted to a number.
+     * @param name The name of the value to get.
+     * @return The value.
+     * @throws RuntimeException if no such mapping exists.
      */
-    public double getDouble(String key)
-    {
-        Object value = get(key);
-
-        try
-        {
-            if (value instanceof Number)
-                return ((Number) value).doubleValue();
-
-            // This is a bit sloppy for the case where value is not a string.
-
-            return Double.valueOf((String) value);
-        } catch (Exception e)
-        {
-            throw new RuntimeException("JSONObject[" + quote(key) + "] is not a number.");
+    public Object get(String name) {
+        Object result = nameValuePairs.get(name);
+        if (result == null) {
+            throw new RuntimeException("JSONObject[\"" + name + "\"] not found.");
         }
+        return result;
     }
 
     /**
-     * Get the int value associated with a key. If the number value is too large for an int, it will be clipped.
+     * Returns the value mapped by {@code name}, or null if no such mapping
+     * exists.
      *
-     * @param key
-     *         A key string.
-     * @return The integer value.
-     * @throws RuntimeException
-     *         if the key is not found or if the value cannot be converted to an integer.
+     * @param name The name of the value to get.
+     * @return The value.
      */
-    public int getInt(String key)
-    {
-        Object value = get(key);
-
-        if (value instanceof Number)
-            return ((Number) value).intValue();
-
-        // Very inefficient way to do this!
-        return (int) getDouble(key);
+    public Object opt(String name) {
+        return nameValuePairs.get(name);
     }
 
     /**
-     * Get the JSONArray value associated with a key.
+     * Returns the value mapped by {@code name} if it exists and is a boolean or
+     * can be coerced to a boolean, or throws otherwise.
      *
-     * @param key
-     *         A key string.
-     * @return A JSONArray which is the value.
-     * @throws RuntimeException
-     *         if the key is not found or if the value is not a JSONArray.
+     * @param name The name of the field we want.
+     * @return The selected value if it exists.
+     * @throws RuntimeException if the mapping doesn't exist or cannot be coerced
+     *                       to a boolean.
      */
-    public JSONArray getJSONArray(String key)
-    {
-        Object o = get(key);
-        if (o instanceof JSONArray)
-        {
-            return (JSONArray) o;
+    public boolean getBoolean(String name) {
+        Object object = get(name);
+        Boolean result = JSON.toBoolean(object);
+        if (result == null) {
+            throw JSON.typeMismatch(false, name, object, "Boolean");
         }
-
-        throw new RuntimeException("JSONObject[" + quote(key) + "] is not a JSONArray.");
+        return result;
     }
 
     /**
-     * Get the JSONObject value associated with a key.
+     * Returns the value mapped by {@code name} if it exists and is a double or
+     * can be coerced to a double, or throws otherwise.
      *
-     * @param key
-     *         A key string.
-     * @return A JSONObject which is the value.
-     * @throws RuntimeException
-     *         if the key is not found or if the value is not a JSONObject.
+     * @param name The name of the field we want.
+     * @return The selected value if it exists.
+     * @throws RuntimeException if the mapping doesn't exist or cannot be coerced
+     *                       to a double.
      */
-    public JSONObject getJSONObject(String key)
-    {
-        Object o = get(key);
-        if (o instanceof JSONObject)
-        {
-            return (JSONObject) o;
+    public double getDouble(String name) {
+        Object object = get(name);
+        Double result = JSON.toDouble(object);
+        if (result == null) {
+            throw JSON.typeMismatch(false, name, object, "number");
         }
-
-        throw new RuntimeException("JSONObject[" + quote(key) + "] is not a JSONObject.");
+        return result;
     }
 
     /**
-     * Get the long value associated with a key. If the number value is too long for a long, it will be clipped.
+     * Returns the value mapped by {@code name} if it exists and is an int or
+     * can be coerced to an int, or throws otherwise.
      *
-     * @param key
-     *         A key string.
-     * @return The long value.
-     * @throws RuntimeException
-     *         if the key is not found or if the value cannot be converted to a long.
+     * @param name The name of the field we want.
+     * @return The selected value if it exists.
+     * @throws RuntimeException if the mapping doesn't exist or cannot be coerced
+     *                       to an int.
      */
-    public long getLong(String key)
-    {
-        Object o = get(key);
-        return o instanceof Number ? ((Number) o).longValue() : (long) getDouble(key);
+    public int getInt(String name) {
+        Object object = get(name);
+        Integer result = JSON.toInteger(object);
+        if (result == null) {
+            throw JSON.typeMismatch(false, name, object, "int");
+        }
+        return result;
     }
 
     /**
-     * Get the string associated with a key.
+     * Returns the value mapped by {@code name} if it exists and is a long or
+     * can be coerced to a long, or throws otherwise.
+     * Note that JSON represents numbers as doubles,
      *
-     * @param key
-     *         A key string.
-     * @return A string which is the value.
-     * @throws RuntimeException
-     *         if the key is not found.
+     * so this is <a href="#lossy">lossy</a>; use strings to transfer numbers
+     * via JSON without loss.
+     *
+     * @param name The name of the field that we want.
+     * @return The value of the field.
+     * @throws RuntimeException if the mapping doesn't exist or cannot be coerced
+     *                       to a long.
      */
-    public String getString(String key)
-    {
-        return get(key).toString();
+    public long getLong(String name) {
+        Object object = get(name);
+        Long result = JSON.toLong(object);
+        if (result == null) {
+            throw JSON.typeMismatch(false, name, object, "long");
+        }
+        return result;
     }
 
     /**
-     * Determine if the JSONObject contains a specific key.
+     * Returns the value mapped by {@code name} if it exists, coercing it if
+     * necessary, or throws if no such mapping exists.
      *
-     * @param key
-     *         A key string.
-     * @return true if the key exists in the JSONObject.
+     * @param name The name of the field we want.
+     * @return The value of the field.
+     * @throws RuntimeException if no such mapping exists.
      */
-    public boolean has(String key)
-    {
-        return properties.containsKey(key);
+    public String getString(String name) {
+        Object object = get(name);
+        String result = JSON.toString(object);
+        if (result == null) {
+            throw JSON.typeMismatch(false, name, object, "String");
+        }
+        return result;
     }
 
     /**
-     * Determine if the value associated with the key is null or if there is no value.
+     * Returns the value mapped by {@code name} if it exists and is a {@code
+     * JSONArray}, or throws otherwise.
      *
-     * @param key
-     *         A key string.
-     * @return true if there is no value associated with the key or if the value is the JSONObject.NULL object.
+     * @param name The field we want to get.
+     * @return The value of the field (if it is a JSONArray.
+     * @throws RuntimeException if the mapping doesn't exist or is not a {@code
+     *                       JSONArray}.
      */
-    public boolean isNull(String key)
-    {
-        return JSONObject.NULL.equals(opt(key));
+    public JSONArray getJSONArray(String name) {
+        Object object = get(name);
+        if (object instanceof JSONArray) {
+            return (JSONArray) object;
+        } else {
+            throw JSON.typeMismatch(false, name, object, "JSONArray");
+        }
     }
 
     /**
-     * Get an enumeration of the keys of the JSONObject. Caution: the set should not be modified.
+     * Returns the value mapped by {@code name} if it exists and is a {@code
+     * JSONObject}, or throws otherwise.
      *
-     * @return An iterator of the keys.
+     * @param name The name of the field that we want.
+     * @return a specified field value (if it is a JSONObject)
+     * @throws RuntimeException if the mapping doesn't exist or is not a {@code
+     *                       JSONObject}.
      */
-    public Set<String> keys()
-    {
-        return properties.keySet();
+    public JSONObject getJSONObject(String name) {
+        Object object = get(name);
+        if (object instanceof JSONObject) {
+            return (JSONObject) object;
+        } else {
+            throw JSON.typeMismatch(false, name, object, "JSONObject");
+        }
     }
 
     /**
-     * Get the number of keys stored in the JSONObject.
+     * Returns the set of {@code String} names in this object. The returned set
+     * is a view of the keys in this object. {@link Set#remove(Object)} will remove
+     * the corresponding mapping from this object and set iterator behaviour
+     * is undefined if this object is modified after it is returned.
      *
-     * @return The number of keys in the JSONObject.
+     * See {@link #keys()}.
+     *
+     * @return The names in this object.
      */
-    public int length()
-    {
-        return properties.size();
+    public Set<String> keys() {
+        return nameValuePairs.keySet();
     }
 
     /**
-     * Produce a JSONArray containing the names of the elements of this JSONObject.
+     * Returns an array containing the string names in this object. This method
+     * returns null if this object contains no mappings.
      *
-     * @return A JSONArray containing the key strings, or null if the JSONObject is empty.
+     * @return the names.
      */
-    public JSONArray names()
-    {
-        JSONArray ja = new JSONArray();
-
-        for (String key : keys())
-        {
-            ja.put(key);
-        }
-
-        return ja.length() == 0 ? null : ja;
+    public JSONArray names() {
+        return nameValuePairs.isEmpty()
+                ? null
+                : JSONArray.from(nameValuePairs.keySet());
     }
 
     /**
-     * Produce a string from a Number.
+     * Encodes the number as a JSON string.
      *
-     * @param n
-     *         A Number
-     * @return A String. @ If n is a non-finite number.
+     * @param number a finite value. May not be {@link Double#isNaN() NaNs} or
+     *               {@link Double#isInfinite() infinities}.
+     * @return The encoded number in string form.
+     * @throws RuntimeException On internal errors. Shouldn't happen.
      */
-    static String numberToString(Number n)
-    {
-        assert n != null;
+    public static String numberToString(Number number) {
+        if (number == null) {
+            throw new RuntimeException("Number must be non-null");
+        }
 
-        testValidity(n);
+        double doubleValue = number.doubleValue();
+        JSON.checkDouble(doubleValue);
 
-        // Shave off trailing zeros and decimal point, if possible.
+        // the original returns "-0" instead of "-0.0" for negative zero
+        if (number.equals(NEGATIVE_ZERO)) {
+            return "-0";
+        }
 
-        String s = n.toString();
-        if (s.indexOf('.') > 0 && s.indexOf('e') < 0 && s.indexOf('E') < 0)
-        {
-            while (s.endsWith("0"))
-            {
-                s = s.substring(0, s.length() - 1);
-            }
-            if (s.endsWith("."))
-            {
-                s = s.substring(0, s.length() - 1);
-            }
+        long longValue = number.longValue();
+        if (doubleValue == (double) longValue) {
+            return Long.toString(longValue);
         }
-        return s;
-    }
 
-    /**
-     * Get an optional value associated with a key.
-     *
-     * @param key
-     *         A key string.
-     * @return An object which is the value, or null if there is no value.
-     * @see #get(String)
-     */
-    public Object opt(String key)
-    {
-        return properties.get(key);
+        return number.toString();
     }
 
-    /**
-     * Put a key/value pair in the JSONObject. If the value is null, then the key will be removed from the JSONObject if
-     * it is present.
-     *
-     * @param key
-     *         A key string.
-     * @param value
-     *         An object which is the value. It should be of one of these types: Boolean, Double, Integer,
-     *         JSONArray, JSONObject, JSONLiteral, Long, String, or the JSONObject.NULL object.
-     * @return this.
-     * @throws RuntimeException
-     *         If the value is non-finite number or if the key is null.
-     */
-    public JSONObject put(String key, Object value)
+    static String doubleToString(double d)
     {
-        assert key != null;
-
-        if (value != null)
-        {
-            testValidity(value);
-            properties.put(key, value);
-        } else
+        if (Double.isInfinite(d) || Double.isNaN(d))
         {
-            remove(key);
+            return "null";
         }
 
-        return this;
+        return numberToString(d);
     }
 
     /**
-     * Produce a string in double quotes with backslash sequences in all the right places,
-     * allowing JSON text to be delivered in HTML. In JSON text, a string cannot contain a control character
-     * or an unescaped quote or backslash.
+     * Encodes {@code data} as a JSON string. This applies quotes and any
+     * necessary character escaping.
      *
-     * @param string
-     *         A String
-     * @return A String correctly formatted for insertion in a JSON text.
+     * @param data the string to encode. Null will be interpreted as an empty
+     *             string.
+     * @return the quoted string.
      */
-    public static String quote(String string)
-    {
-        if (string == null || string.length() == 0)
-        {
+    public static String quote(String data) {
+        if (data == null) {
             return "\"\"";
         }
-
-        char b;
-        char c = 0;
-        int i;
-        int len = string.length();
-        StringBuilder buffer = new StringBuilder(len + 4);
-        String t;
-
-        buffer.append('"');
-        for (i = 0; i < len; i += 1)
-        {
-            b = c;
-            c = string.charAt(i);
-            switch (c)
-            {
-                case '\\':
-                case '"':
-                    buffer.append('\\');
-                    buffer.append(c);
-                    break;
-                case '/':
-                    if (b == '<')
-                    {
-                        buffer.append('\\');
-                    }
-                    buffer.append(c);
-                    break;
-                case '\b':
-                    buffer.append("\\b");
-                    break;
-                case '\t':
-                    buffer.append("\\t");
-                    break;
-                case '\n':
-                    buffer.append("\\n");
-                    break;
-                case '\f':
-                    buffer.append("\\f");
-                    break;
-                case '\r':
-                    buffer.append("\\r");
-                    break;
-                default:
-                    if (c < ' ' || (c >= '\u0080' && c < '\u00a0') || (c >= '\u2000' && c < '\u2100'))
-                    {
-                        t = "000" + Integer.toHexString(c);
-                        buffer.append("\\u").append(t.substring(t.length() - 4));
-                    } else
-                    {
-                        buffer.append(c);
-                    }
-            }
+        try {
+            JSONStringer stringer = new JSONStringer();
+            stringer.open(JSONStringer.Scope.NULL, "");
+            stringer.string(data);
+            stringer.close(JSONStringer.Scope.NULL, JSONStringer.Scope.NULL, "");
+            return stringer.toString();
+        } catch (RuntimeException e) {
+            throw new AssertionError();
         }
-        buffer.append('"');
-        return buffer.toString();
-    }
-
-    /**
-     * Remove a name and its value, if present.
-     *
-     * @param key
-     *         The name to be removed.
-     * @return The value that was associated with the name, or null if there was no value.
-     */
-    public Object remove(String key)
-    {
-        return properties.remove(key);
     }
 
-    private static final Class[] ALLOWED = new Class[]
-            {String.class, Boolean.class, Number.class, JSONObject.class, JSONArray.class, JSONString.class,
-                    JSONLiteral.class, Null.class};
-
-    /**
-     * Throw an exception if the object is an NaN or infinite number, or not a type which may be stored.
-     *
-     * @param value
-     *         The object to test. @ If o is a non-finite number.
-     */
-    @SuppressWarnings("unchecked")
-    static void testValidity(Object value)
-    {
-        if (value == null)
-            throw new IllegalArgumentException("null isn't valid in JSONObject and JSONArray. Use JSONObject.NULL instead.");
-
-        boolean found = false;
-        Class actual = value.getClass();
-
-        for (Class allowed : ALLOWED)
-        {
-            if (allowed.isAssignableFrom(actual))
-            {
-                found = true;
-                break;
-            }
-        }
-
-        if (!found)
-        {
-            List<String> typeNames = new ArrayList<String>();
-
-            for (Class c : ALLOWED)
-            {
-                String name = c.getName();
 
-                if (name.startsWith("java.lang."))
-                    name = name.substring(10);
-
-                typeNames.add(name);
-            }
-
-            Collections.sort(typeNames);
-
-            StringBuilder joined = new StringBuilder();
-            String sep = "";
-
-            for (String name : typeNames)
-            {
-                joined.append(sep);
-                joined.append(name);
-
-                sep = ", ";
-            }
-
-            String message = String.format("JSONObject properties may be one of %s. Type %s is not allowed.",
-                    joined.toString(), actual.getName());
-
-            throw new RuntimeException(message);
-        }
-
-        if (value instanceof Double)
-        {
-            Double asDouble = (Double) value;
-
-            if (asDouble.isInfinite() || asDouble.isNaN())
-            {
-                throw new RuntimeException(
-                        "JSON does not allow non-finite numbers.");
-            }
-
-            return;
-        }
-
-        if (value instanceof Float)
-        {
-            Float asFloat = (Float) value;
-
-            if (asFloat.isInfinite() || asFloat.isNaN())
-            {
-                throw new RuntimeException(
-                        "JSON does not allow non-finite numbers.");
-            }
-
-        }
-
-    }
 
     /**
      * Prints the JSONObject using the session.
@@ -889,7 +637,7 @@ public final class JSONObject extends JSONCollection
 
             session.printSymbol(':');
 
-            printValue(session, properties.get(key));
+            printValue(session, nameValuePairs.get(key));
 
             comma = true;
         }
@@ -902,6 +650,7 @@ public final class JSONObject extends JSONCollection
         session.printSymbol('}');
     }
 
+
     /**
      * Prints a value (a JSONArray or JSONObject, or a value stored in an array or object) using
      * the session.
@@ -910,7 +659,12 @@ public final class JSONObject extends JSONCollection
      */
     static void printValue(JSONPrintSession session, Object value)
     {
-        
+
+        if (value == null || value == NULL)
+        {
+            session.print("null");
+            return;
+        }
         if (value instanceof JSONObject)
         {
             ((JSONObject) value).print(session);
@@ -950,10 +704,6 @@ public final class JSONObject extends JSONCollection
         session.printQuoted(value.toString());
     }
 
-    /**
-     * Returns true if the other object is a JSONObject and its set of properties matches this object's properties.
-     */
-    @Override
     public boolean equals(Object obj)
     {
         if (obj == null)
@@ -964,7 +714,7 @@ public final class JSONObject extends JSONCollection
 
         JSONObject other = (JSONObject) obj;
 
-        return properties.equals(other.properties);
+        return nameValuePairs.equals(other.nameValuePairs);
     }
 
     /**
@@ -977,7 +727,7 @@ public final class JSONObject extends JSONCollection
      */
     public Map<String, Object> toMap()
     {
-        return Collections.unmodifiableMap(properties);
+        return Collections.unmodifiableMap(nameValuePairs);
     }
 
     /**
@@ -1000,6 +750,7 @@ public final class JSONObject extends JSONCollection
         return this;
     }
 
+
     /**
      * Navigates into a nested JSONObject, creating the JSONObject if necessary. They key must not exist,
      * or must be a JSONObject.
@@ -1013,7 +764,7 @@ public final class JSONObject extends JSONCollection
     {
         assert key != null;
 
-        Object nested = properties.get(key);
+        Object nested = nameValuePairs.get(key);
 
         if (nested != null && !(nested instanceof JSONObject))
         {
@@ -1023,9 +774,33 @@ public final class JSONObject extends JSONCollection
         if (nested == null)
         {
             nested = new JSONObject();
-            properties.put(key, nested);
+            nameValuePairs.put(key, nested);
         }
 
         return (JSONObject) nested;
     }
-}
+
+    static void testValidity(Object value)
+    {
+        if (value == null)
+          throw new IllegalArgumentException("null isn't valid in JSONObject and JSONArray. Use JSONObject.NULL instead.");
+        if (value == NULL)
+        {
+            return;
+        }
+        Class<? extends Object> clazz = value.getClass();
+        if (Boolean.class.isAssignableFrom(clazz)
+            || Number.class.isAssignableFrom(clazz)
+            || String.class.isAssignableFrom(clazz)
+            || JSONArray.class.isAssignableFrom(clazz)
+            || JSONLiteral.class.isAssignableFrom(clazz)
+            || JSONObject.class.isAssignableFrom(clazz)
+            || JSONString.class.isAssignableFrom(clazz))
+        {
+            return;
+        }
+
+        throw new RuntimeException("JSONObject properties may be one of Boolean, Number, String, org.apache.tapestry5.json.JSONArray, org.apache.tapestry5.json.JSONLiteral, org.apache.tapestry5.json.JSONObject, org.apache.tapestry5.json.JSONObject$Null, org.apache.tapestry5.json.JSONString. Type "+clazz.getName()+" is not allowed.");
+    }
+
+}
\ No newline at end of file