Class JSONTokener
java.lang.Object
org.springframework.boot.configurationprocessor.json.JSONTokener
Parses a JSON (RFC 4627) encoded
string into the corresponding object. Most clients of this class will use only need the
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 the
following syntax errors will be ignored:
- End of line comments starting with
//
or#
and ending with a newline character. - C-style comments starting with
/*
and ending with*
/
. Such comments may not be nested. - Strings that are unquoted or
'single quoted'
. - Hexadecimal integers prefixed with
0x
or0X
. - Octal integers prefixed with
0
. - Array elements separated by
;
. - Unnecessary array separators. These are interpreted as if null was the omitted value.
- Key-value pairs separated by
=
or=>
. - Key-value pairs separated by
;
.
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.
-
Constructor Summary
-
Method Summary
Modifier and TypeMethodDescriptionvoid
back()
static int
dehexchar
(char hex) boolean
more()
char
next()
char
next
(char c) next
(int length) char
nextString
(char quote) Returns the string up to but not includingquote
, unescaping any character escape sequences encountered along the way.nextTo
(char excluded) Returns the next value from the input.void
char
skipTo
(char to) syntaxError
(String message) Returns an exception containing the given message plus the current position and the entire input string.toString()
Returns the current position and the entire input string.
-
Constructor Details
-
JSONTokener
- Parameters:
in
- JSON encoded string. Null is not permitted and will yield a tokener that throwsNullPointerExceptions
when methods are called.
-
-
Method Details
-
nextValue
Returns the next value from the input.- Returns:
- a
JSONObject
,JSONArray
, String, Boolean, Integer, Long, Double orJSONObject.NULL
. - Throws:
JSONException
- if the input is malformed.
-
nextString
Returns the string up to but not includingquote
, 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.- Parameters:
quote
- either ' or ".- Returns:
- the string up to but not including
quote
- Throws:
NumberFormatException
- if any unicode escape sequences are malformed.JSONException
- if processing of json failed
-
syntaxError
Returns an exception containing the given message plus the current position and the entire input string.- Parameters:
message
- the message- Returns:
- an exception
-
toString
Returns the current position and the entire input string. -
more
public boolean more() -
next
public char next() -
next
- Throws:
JSONException
-
nextClean
- Throws:
JSONException
-
next
- Throws:
JSONException
-
nextTo
-
nextTo
-
skipPast
-
skipTo
public char skipTo(char to) -
back
public void back() -
dehexchar
public static int dehexchar(char hex)
-