/** * Creates a new {@code JSONArray} with values from the JSON string. * * @param json a JSON-encoded string containing an array. * @throws RuntimeException if the parse fails or doesn't yield a {@code * JSONArray}. */ public JSONArray(String json) { this(new JSONTokener(json)); }
/** * Returns the next value from the input. * * @return a {@link JSONObject}, {@link JSONArray}, String, Boolean, * Integer, Long, Double or {@link JSONObject#NULL}. * @throws RuntimeException if the input is malformed. */ 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(); } }
int first = nextCleanInternal(); if (first == '}') { return result; Object name = null; try { name = nextValue(null); } catch (RuntimeException e){ if (e.getMessage().equals("End of input" + this)){ throw syntaxError("A JSONObject text must end with '}'"); throw syntaxError("Names cannot be null"); } else { throw syntaxError("Names must be strings, but " + name + " is of type " + name.getClass().getName()); int separator = nextCleanInternal(); if (separator != ':' && separator != '=') { throw syntaxError("Expected a ':' after a key"); result.put((String) name, nextValue(null)); switch (nextCleanInternal()) { case '}': return result; continue; default:
if (commentEnd == -1) { pos = in.length(); throw syntaxError("Unclosed comment"); skipToEndOfLine(); continue; skipToEndOfLine(); continue;
throw syntaxError("Unterminated escape sequence"); builder.append(readEscapeCharacter()); start = pos; throw syntaxError("Unterminated string");
String literal = nextToInternal("{}[]/\\:,=;# \t\f"); throw syntaxError("Missing value"); } else if ("null".equalsIgnoreCase(literal)) { return JSONObject.NULL;
/** * 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 RuntimeException if the parse fails or doesn't yield a * {@code 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"); } }
int first = nextCleanInternal(); if (first == '}') { return result; Object name = null; try { name = nextValue(null); } catch (RuntimeException e){ if (e.getMessage().equals("End of input" + this)){ throw syntaxError("A JSONObject text must end with '}'"); throw syntaxError("Names cannot be null"); } else { throw syntaxError("Names must be strings, but " + name + " is of type " + name.getClass().getName()); int separator = nextCleanInternal(); if (separator != ':' && separator != '=') { throw syntaxError("Expected a ':' after a key"); result.put((String) name, nextValue(null)); switch (nextCleanInternal()) { case '}': return result; continue; default:
if (commentEnd == -1) { pos = in.length(); throw syntaxError("Unclosed comment"); skipToEndOfLine(); continue; skipToEndOfLine(); continue;
throw syntaxError("Unterminated escape sequence"); builder.append(readEscapeCharacter()); start = pos; throw syntaxError("Unterminated string");
String literal = nextToInternal("{}[]/\\:,=;# \t\f"); throw syntaxError("Missing value"); } else if ("null".equalsIgnoreCase(literal)) { return JSONObject.NULL;
/** * 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}. */ 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"); } }
/** * Returns the next value from the input. * * @return a {@link JSONObject}, {@link JSONArray}, String, Boolean, * Integer, Long, Double or {@link JSONObject#NULL}. * @throws RuntimeException if the input is malformed. */ 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(); } }
switch (nextCleanInternal()) { case -1: throw syntaxError("Expected a ',' or ']'"); case ']': if (hasTrailingSeparator) { result.put(nextValue(null)); switch (nextCleanInternal()) { case ']': return result; continue; default: throw syntaxError("Expected a ',' or ']'");
/** * 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 JSONObject(String json) { this(new JSONTokener(json)); }
/** * 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 RuntimeException if the parse fails or doesn't yield a * {@code 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"); } }
switch (nextCleanInternal()) { case -1: throw syntaxError("Expected a ',' or ']'"); case ']': if (hasTrailingSeparator) { result.put(nextValue(null)); switch (nextCleanInternal()) { case ']': return result; continue; default: throw syntaxError("Expected a ',' or ']'");
/** * 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 JSONObject(String json) { this(new JSONTokener(json)); }
/** * 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}. */ 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"); } }
/** * Creates a new {@code JSONArray} with values from the JSON string. * * @param json a JSON-encoded string containing an array. * @throws RuntimeException if the parse fails or doesn't yield a {@code * JSONArray}. */ public JSONArray(String json) { this(new JSONTokener(json)); }