private JSONObject readObject()

in tapestry-json/src/main/java/org/apache/tapestry5/json/JSONTokener.java [397:453]


    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 '}'");
                }
                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());
                }
            }

            /*
             * 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");
            }
            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 '}'");
            }
        }
    }