/** * Construct a JSONObject from a source JSON text string. * This is the most commonly used JSONObject constructor. * @param source A string beginning * with <code>{</code> <small>(left brace)</small> and ending * with <code>}</code> <small>(right brace)</small>. * @exception JSONException If there is a syntax error in the source * string or a duplicated key. */ public JSONObject(final String source) throws JSONException { this(new JSONTokener(source)); }
/** * Consume the next character, and check that it matches a specified * character. * @param c The character to match. * @return The character. * @throws JSONException if the character does not match. */ public char next(final char c) throws JSONException { char n = next(); if (n != c) { throw syntaxError("Expected '" + c + "' and instead saw '" + n + "'"); } return n; }
if (x.nextClean() != '{') throw x.syntaxError("A JSONObject text must begin with '{'"); c = x.nextClean(); switch (c) throw x.syntaxError("A JSONObject text must end with '}'"); case '}': return; default: x.back(); key = x.nextValue().toString(); c = x.nextClean(); if (c == '=') if (x.next() != '>') x.back(); throw x.syntaxError("Expected a ':' after a key"); putOnce(key, x.nextValue()); switch (x.nextClean()) if (x.nextClean() == '}')
char c = nextClean(); String s; return nextString(c); case '{': back(); return new JSONObject(this); case '[': case '(': back(); return new JSONArray(this); c = next(); back(); throw syntaxError("Missing value");
/** * Make a JSONException to signal a syntax error. * * @param message The error message. * @return A JSONException object, suitable for throwing */ public JSONException syntaxError(final String message) { return new JSONException(message + toString()); }
/** * Get the next char in the string, skipping whitespace. * @throws JSONException * @return A character, or 0 if there are no more characters. */ public char nextClean() throws JSONException { for (;;) { char c = next(); if (c == 0 || c > ' ') { return c; } } }
throw syntaxError("Substring bounds error");
if (x.nextClean() != '{') throw x.syntaxError("A JSONObject text must begin with '{'"); c = x.nextClean(); switch (c) throw x.syntaxError("A JSONObject text must end with '}'"); case '}': return; default: x.back(); key = x.nextValue().toString(); c = x.nextClean(); if (c == '=') if (x.next() != '>') x.back(); throw x.syntaxError("Expected a ':' after a key"); putOnce(key, x.nextValue()); switch (x.nextClean()) if (x.nextClean() == '}')
char c = nextClean(); String s; return nextString(c); case '{': back(); return new JSONObject(this); case '[': case '(': back(); return new JSONArray(this); c = next(); back(); throw syntaxError("Missing value");
/** * Make a JSONException to signal a syntax error. * * @param message The error message. * @return A JSONException object, suitable for throwing */ public JSONException syntaxError(final String message) { return new JSONException(message + toString()); }
/** * Get the next char in the string, skipping whitespace. * @throws JSONException * @return A character, or 0 if there are no more characters. */ public char nextClean() throws JSONException { for (;;) { char c = next(); if (c == 0 || c > ' ') { return c; } } }
throw syntaxError("Substring bounds error");
char c = x.nextClean(); char q; if (c == '[') throw x.syntaxError("A JSONArray text must start with '['"); if (x.nextClean() == ']') x.back(); for (;;) if (x.nextClean() == ',') x.back(); myArrayList.add(null); x.back(); myArrayList.add(x.nextValue()); c = x.nextClean(); switch (c) if (x.nextClean() == ']') x.back(); break; case ']': throw x.syntaxError("Expected a '" + new Character(q) + "'");
/** * Consume the next character, and check that it matches a specified * character. * @param c The character to match. * @return The character. * @throws JSONException if the character does not match. */ public char next(final char c) throws JSONException { char n = next(); if (n != c) { throw syntaxError("Expected '" + c + "' and instead saw '" + n + "'"); } return n; }
/** * Construct a JSONArray from a source JSON text. * @param source A string that begins with * <code>[</code> <small>(left bracket)</small> * and ends with <code>]</code> <small>(right bracket)</small>. * @throws JSONException If there is a syntax error. */ public JSONArray(final String source) throws JSONException { this(new JSONTokener(source)); }
char c = x.nextClean(); char q; if (c == '[') throw x.syntaxError("A JSONArray text must start with '['"); if (x.nextClean() == ']') x.back(); for (;;) if (x.nextClean() == ',') x.back(); myArrayList.add(null); x.back(); myArrayList.add(x.nextValue()); c = x.nextClean(); switch (c) if (x.nextClean() == ']') x.back(); break; case ']': throw x.syntaxError("Expected a '" + Character.valueOf(q) + "'");
/** * Get the text up but not including the specified character or the * end of line, whichever comes first. * @param d A delimiter character. * @return A string. */ public String nextTo(final char d) throws JSONException { StringBuffer sb = new StringBuffer(); for (;;) { char c = next(); if (c == d || c == 0 || c == '\n' || c == '\r') { if (c != 0) { back(); } return sb.toString().trim(); } sb.append(c); } }
for (;;) c = next(); switch (c) case '\n': case '\r': throw syntaxError("Unterminated string"); case '\\': c = next(); switch (c) break; case 'u': sb.append((char)Integer.parseInt(next(4), 16)); break; case 'x': sb.append((char)Integer.parseInt(next(2), 16)); break; default: