public class JSONTokener
extends java.lang.Object
constructor
and nextValue() method. Example usage:
String json = "{"
+ " \"query\": \"Pizza\", "
+ " \"locations\": [ 94043, 90210 ] "
+ "}";
JSONObject object = (JSONObject) new JSONTokener(json).nextValue();
String query = object.getString("query");
JSONArray locations = object.getJSONArray("locations");
For best interoperability and performance use JSON that complies with
RFC 4627, such as that generated by JSONStringer. 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:
// or # and ending
with a newline character.
/* and ending with
*/. Such comments may not be nested.
'single quoted'.
0x or 0X.
0.
;.
= or =>.
;.
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 Effective Java Item 17, "Design and Document or inheritance or else prohibit it" for further information.
| Modifier and Type | Field and Description |
|---|---|
private java.lang.String |
in
The input JSON.
|
private int |
pos
The index of the next character to be returned by
next(). |
| Constructor and Description |
|---|
JSONTokener(java.lang.String in) |
| Modifier and Type | Method and Description |
|---|---|
void |
back()
Unreads the most recent character of input.
|
static int |
dehexchar(char hex)
Returns the integer [0..15] value for the given hex character, or -1
for non-hex input.
|
boolean |
more()
Returns true until the input has been exhausted.
|
char |
next()
Returns the next available character, or the null character '\0' if all
input has been exhausted.
|
char |
next(char c)
Returns the next available character if it equals
c. |
java.lang.String |
next(int length)
Returns the next
length characters of the input. |
char |
nextClean()
Returns the next character that is not whitespace and does not belong to
a comment.
|
private int |
nextCleanInternal() |
java.lang.String |
nextString(char quote)
Returns the string up to but not including
quote, unescaping any
character escape sequences encountered along the way. |
java.lang.String |
nextTo(char excluded)
Equivalent to
nextTo(String.valueOf(excluded)). |
java.lang.String |
nextTo(java.lang.String excluded)
Returns the
trimmed string holding the characters up
to but not including the first of:
any character in excluded
a newline character '\n'
a carriage return '\r'
|
private java.lang.String |
nextToInternal(java.lang.String excluded)
Returns the string up to but not including any of the given characters or
a newline character.
|
java.lang.Object |
nextValue()
Returns the next value from the input.
|
private JSONArray |
readArray()
Reads a sequence of values and the trailing closing brace ']' of an
array.
|
private char |
readEscapeCharacter()
Unescapes the character identified by the character or characters that
immediately follow a backslash.
|
private java.lang.Object |
readLiteral()
Reads a null, boolean, numeric or unquoted string literal value.
|
private JSONObject |
readObject()
Reads a sequence of key/value pairs and the trailing closing brace '}' of
an object.
|
void |
skipPast(java.lang.String thru)
Advances past all input up to and including the next occurrence of
thru. |
char |
skipTo(char to)
Advances past all input up to but not including the next occurrence of
to. |
private void |
skipToEndOfLine()
Advances the position until after the next newline character.
|
JSONException |
syntaxError(java.lang.String message)
Returns an exception containing the given message plus the current
position and the entire input string.
|
java.lang.String |
toString()
Returns the current position and the entire input string.
|
private final java.lang.String in
private int pos
next(). When
the input is exhausted, this equals the input's length.public JSONTokener(java.lang.String in)
in - JSON encoded string. Null is not permitted and will yield a
tokener that throws NullPointerExceptions when methods are
called.public java.lang.Object nextValue()
throws JSONException
JSONObject, JSONArray, String, Boolean,
Integer, Long, Double or JSONObject.NULL.JSONException - if the input is malformed.private int nextCleanInternal()
throws JSONException
JSONExceptionprivate void skipToEndOfLine()
public java.lang.String nextString(char quote)
throws JSONException
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.quote - either ' or ".JSONExceptionprivate char readEscapeCharacter()
throws JSONException
JSONExceptionprivate java.lang.Object readLiteral()
throws JSONException
JSONExceptionprivate java.lang.String nextToInternal(java.lang.String excluded)
private JSONObject readObject() throws JSONException
JSONExceptionprivate JSONArray readArray() throws JSONException
JSONExceptionpublic JSONException syntaxError(java.lang.String message)
public java.lang.String toString()
toString in class java.lang.Objectpublic boolean more()
public char next()
public char next(char c)
throws JSONException
c. Otherwise an
exception is thrown.JSONExceptionpublic char nextClean()
throws JSONException
JSONExceptionpublic java.lang.String next(int length)
throws JSONException
length characters of the input.
The returned string shares its backing character array with this
tokener's input string. If a reference to the returned string may be held
indefinitely, you should use new String(result) to copy it first
to avoid memory leaks.
JSONException - if the remaining input is not long enough to
satisfy this request.public java.lang.String nextTo(java.lang.String excluded)
trimmed string holding the characters up
to but not including the first of:
excluded
The returned string shares its backing character array with this
tokener's input string. If a reference to the returned string may be held
indefinitely, you should use new String(result) to copy it first
to avoid memory leaks.
public java.lang.String nextTo(char excluded)
nextTo(String.valueOf(excluded)).public void skipPast(java.lang.String thru)
thru. If the remaining input doesn't contain thru, the
input is exhausted.public char skipTo(char to)
to. If the remaining input doesn't contain to, the input
is unchanged.public void back()
public static int dehexchar(char hex)
hex - a character in the ranges [0-9], [A-F] or [a-f]. Any other
character will yield a -1 result.