Class JSONReader
- JSON strings are represented as java.lang.String.
- JSON true and false are represented as java.lang.Boolean.
- JSON null is represented as
JSONNull.INSTANCE
. - JSON numbers are represented as Java java.math.BigDecimal.
- JSON arrays are represented as java.util.List.
- JSON maps/objects are represented as java.util.Map.
Syntax errors are reported with JSONSyntaxError or, in case of short input, EOFException.
This class is able to read multiple adjacent JSON values from a single input stream. However, some care is needed when doing this, since this class maintains a one-character internal lookahead buffer. Reading a single JSON value generally consumes up to one character more than needed. For example, given a reader with ready input "123x", JSONReader will consume all four bytes. When reading multiple JSON values from a stream, it is important to use the same JSONReader object, since it will maintain its internal lookahead buffer between objects and so will not accidentally discard input.
Furthermore, when given a Reader that is not a LineNumberReader, this class creates a wrapping LineNumberReader, which may internally consume input from the underlying reader in a way not under our control.
Finally, this class can be used as a simple SAX-style JSON tokenizer; see nextLexeme()
and the
class JSONEventReader
.
-
Nested Class Summary
Nested ClassesModifier and TypeClassDescriptionstatic enum
Most JSON tokens are self-representing; the remainder are represented with instances of Lexeme. -
Field Summary
FieldsModifier and TypeFieldDescriptionprotected int
protected int
protected int
protected LineNumberReader
-
Constructor Summary
ConstructorsConstructorDescriptionJSONReader
(Reader r) Construct a reader that reads JSON text from the given Reader. -
Method Summary
Modifier and TypeMethodDescriptionprotected Object
_read()
array()
protected boolean
atEOF()
protected int
buffer()
protected boolean
check
(char expected) protected boolean
checkDrop
(char expected) protected char
curr()
protected void
drop()
void
Consumes any whitespace in the stream, and returns normally if it then finds itself at the end of the stream.Retrieve the underlying LineNumberReader.Read the next JSON token from the input stream.protected Object
number()
object()
read()
Reads and returns the next JSON value.protected Object
static Object
Reads and returns a single JSON value from the given Reader.protected static Object
Reads and returns a single JSON value from the given Reader.static Object
Reads and returns a single JSON value from the given input JSON source text.static Object
Reads and returns a single JSON value from the given input JSON source text.As read(), but wraps the result inJSONValue
.static JSONValue
Reads and returns a single JSONValue from the given Reader.static JSONValue
Reads and returns a single JSONValue from the given input JSON source text.static JSONValue
Reads and returns a single JSONValue from the given input JSON source text.protected void
protected Object
string
(char sep) Read a string with a specific delimiter (either ' or ")protected Object
valueGuard
(Object value)
-
Field Details
-
NO_TOKEN
protected int NO_TOKEN -
EOF
protected int EOF -
reader
-
_buffer
protected int _buffer
-
-
Constructor Details
-
JSONReader
Construct a reader that reads JSON text from the given Reader. If the Reader is not a LineNumberReader, it is wrapped in a LineNumberReader.- Parameters:
r
- Input to the JSONReader.
-
-
Method Details
-
getReader
Retrieve the underlying LineNumberReader. -
readFrom
Reads and returns a single JSON value from the given Reader. Calls expectEOF() after reading, to ensure no trailing junk is present.- Throws:
IOException
-
readValue
Reads and returns a single JSONValue from the given Reader. Calls expectEOF() after reading, to ensure no trailing junk is present.- Throws:
IOException
-
readFrom
Reads and returns a single JSON value from the given input JSON source text. Calls expectEOF() after reading, to ensure no trailing junk is present.- Throws:
IOException
-
readValue
Reads and returns a single JSONValue from the given input JSON source text. Calls expectEOF() after reading, to ensure no trailing junk is present.- Throws:
IOException
-
readFrom
Reads and returns a single JSON value from the given input JSON source text. If ensureSingleValue is true, calls expectEOF() after reading, to ensure no trailing junk is present. Otherwise, ignores any input following the JSON value returned.- Throws:
IOException
-
readValue
Reads and returns a single JSONValue from the given input JSON source text. If ensureSingleValue is true, calls expectEOF() after reading, to ensure no trailing junk is present. Otherwise, ignores any input following the JSON value returned.- Throws:
IOException
-
readFrom
Reads and returns a single JSON value from the given Reader. If ensureSingleValue is true, calls expectEOF() after reading, to ensure no trailing junk is present. Otherwise, leaves the given Reader in good condition to yield additional input. This is protected rather than public because reading multiple JSON values depends on the state of the internal lookahead buffer, which with this static method is clearly not preserved. It would be dangerous to encourage use of this method to read multiple JSON values from a stream. Instead, a long-running instance of JSONReader should be used to parse the whole stream.- Throws:
IOException
-
drop
- Throws:
IOException
-
buffer
- Throws:
IOException
-
atEOF
- Throws:
IOException
-
curr
- Throws:
IOException
-
check
- Throws:
IOException
-
checkDrop
- Throws:
IOException
-
skipWhiteSpace
- Throws:
IOException
-
valueGuard
- Throws:
JSONSyntaxError
-
read
Reads and returns the next JSON value. Throws EOFException if no complete JSON value is available.- Throws:
IOException
-
readValue
As read(), but wraps the result inJSONValue
.- Throws:
IOException
-
expectEOF
Consumes any whitespace in the stream, and returns normally if it then finds itself at the end of the stream. Throws JSONSyntaxError if, after consuming whitespace, some non-whitespace input remains to be consumed. Useful for enforcing rules about files containing some fixed number of JSON values and no more.- Throws:
IOException
-
readAtom
- Throws:
IOException
-
nextLexeme
Read the next JSON token from the input stream. Strings, numbers, booleans and null are returned as the Java representations of their JSON values, as described in the class comment for this class. Array and object delimiters are returned as instances ofJSONReader.Lexeme
. Throws EOFException at the end of the input.- Throws:
IOException
-
_read
- Throws:
IOException
-
object
- Throws:
IOException
-
array
- Throws:
IOException
-
number
- Throws:
IOException
-
string
Read a string with a specific delimiter (either ' or ")- Throws:
IOException
-