public class JsonReader extends ReplayReader implements Iterable<String>, Iterator<String>
Reader for JSON streams that reads JSON tokens
sequentially, while asserting the content conforms to the
RFC 4627 specification.
This implementation provides the following features:
String: readToken(). (Please see readToken() for a
definition of: token).int
starting position of each token: readTokenStart(). (This position
can be dereferenced via: buf()).Iterable interface, to sequentially iterate over
each token: iterator().getIndex().setIndex(int).read(),
read(char[]). and read(char[], int, int) methods.
ReplayReader.ReadbackCharArrayWriterbuffer, closedin| Constructor and Description |
|---|
JsonReader(Reader reader)
Construct a new
JsonReader for JSON content to be read from the
specified Reader, that ignores inter-token whitespace. |
JsonReader(Reader reader,
boolean ignoreWhitespace)
Construct a new
JsonReader for JSON content to be read from the
specified Reader. |
| Modifier and Type | Method and Description |
|---|---|
protected char[] |
buf()
Returns the
char[] buffer of the underlying
JsonReplayReader. |
protected int |
getEndPosition(int index)
Returns the end position at
index. |
int |
getIndex()
Returns the index of the most recently read token.
|
int |
getPosition()
Returns the buffer position of the most recently read char.
|
protected int |
getStartPosition(int index)
Returns the start position at
index. |
boolean |
hasNext()
Returns
true if the iteration has more tokens. |
Iterator<String> |
iterator()
Returns this
JsonReader, since it is itself an implementation of
the Iterator interface. |
void |
mark(int readlimit) |
String |
next()
Returns the next token in the iteration.
|
int |
read()
Reads a single character, and transparently unescapes string-literal
unicode (
"
") and two-character ("\n") escape codes
into UTF-8 as defined in RFC
4627, Section 2.5. |
int |
read(char[] cbuf)
Reads characters into an array, and transparently unescapes string-literal
unicode (
"
") and two-character ("\n") escape codes
into UTF-8, as defined in
RFC 4627, Section 2.5. |
int |
read(char[] cbuf,
int off,
int len)
Reads characters into a portion of an array, and transparently unescapes
string-literal unicode (
"
") and two-character ("\n")
escape codes into UTF-8, as defined in
RFC 4627, Section 2.5. |
String |
readToken()
Read the next JSON token.
|
protected int |
readTokenStart()
Read until the end of the next token, and return the start index of the
token that was just read.
|
void |
reset() |
void |
setIndex(int index)
Sets the token index of this reader, such that
getIndex() returns
the specified index. |
protected void |
setPosition(int p)
|
int |
size()
Returns the number of tokens read thus far.
|
close, markSupported, ready, skipclone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, waitforEach, spliteratorforEachRemaining, removepublic JsonReader(Reader reader)
JsonReader for JSON content to be read from the
specified Reader, that ignores inter-token whitespace. This
constructor is equivalent to calling new JsonReader(reader, true).reader - The Reader from which JSON is to be read.public JsonReader(Reader reader, boolean ignoreWhitespace)
JsonReader for JSON content to be read from the
specified Reader.reader - The Reader from which JSON is to be read.ignoreWhitespace - If ignoreWhitespace == false, inter-token
whitespace will not be skipped.public int getIndex()
public void setIndex(int index)
getIndex() returns
the specified index. Note, that calling readToken() after this
method will return the token with index index + 1.index - The index to be set.IllegalArgumentException - If index < -1 || size() <= index.public int size()
setIndex(int).setIndex(int).protected int getStartPosition(int index)
index.index - The token index.index.protected int getEndPosition(int index)
index.index - The token index.index.protected void setPosition(int p)
p - The position.IllegalArgumentException - If p is negative, or if p
is greater than the length of the underlying buffer.public int getPosition()
public String readToken() throws IOException, JsonParseException
{ } [ ] : , ^".*"$ ^".*"$ ^-?(0|[1-9]\d*)(\.\d+)?([eE][+-]?([1-9]\d*))?$ ^null|true|false$ ^[ \n\r\t]+$Note: If this instance ignores whitespace, this method will skip whitespace tokens.
null if the end of content has been
reached.IOException - If an I/O error has occurred.JsonParseException - If the content is not well formed.public void mark(int readlimit)
mark in class ReplayReaderpublic void reset()
reset in class ReplayReaderpublic int read()
throws IOException
"
") and two-character ("\n") escape codes
into UTF-8 as defined in RFC
4627, Section 2.5.
Characters read with this method advance the characters of the tokens to
which they belong. Therefore, when partially reading a token with
read(), subsequent calls to readToken() will return
the remaining characters of the token that have not yet been returned by
read(). Characters read with this method undergo the same
token-level error checking as in readTokenStart() or
readToken().
0x00-0xffff), or -1 if the end of the stream has been
reached.IOException - If an I/O error has occurred.JsonParseException - If the content is not well formed.readTokenStart(),
readToken()public int read(char[] cbuf,
int off,
int len)
throws IOException
"
") and two-character ("\n")
escape codes into UTF-8, as defined in
RFC 4627, Section 2.5.
Characters read with this method advance the characters of the tokens to
which they belong. Therefore, when partially reading a token with
read(char[],int,int), subsequent calls to readToken()
will return the remaining characters of the token that have not yet been
returned by read(). Characters read with this method undergo
the same token-level error checking as in readTokenStart() or
readToken().
cbuf - Destination buffer.off - Offset at which to start storing characters.len - Maximum number of characters to read.IOException - If an I/O error has occurred.IndexOutOfBoundsException - If off is negative, or
len is negative, or len is greater than
cbuf.length - off.JsonParseException - If the content is not well formed.readTokenStart(),
readToken()public int read(char[] cbuf)
throws IOException
"
") and two-character ("\n") escape codes
into UTF-8, as defined in
RFC 4627, Section 2.5.
Characters read with this method advance the characters of the tokens to
which they belong. Therefore, when partially reading a token with
read(char[]), subsequent calls to readToken() will return
the remaining characters of the token that have not yet been returned by
read(). Characters read with this method undergo the same
token-level validation as in readTokenStart() or
readToken().
cbuf - Destination buffer.IOException - If an I/O error has occurred.IndexOutOfBoundsException - If off is negative, or
len is negative, or len is greater than
cbuf.length - off.JsonParseException - If the content is not well formed.readTokenStart(),
readToken()public Iterator<String> iterator()
JsonReader, since it is itself an implementation of
the Iterator interface. The iterator iterates over the JSON token
strings produced by JsonReader.readToken().public boolean hasNext()
throws UncheckedIOException,
JsonParseException
true if the iteration has more tokens. (In other words,
returns true if next() would return a token rather than
throw an exception).hasNext in interface Iterator<String>true if the iteration has more tokens.UncheckedIOException - If an IOException occurs while
reading from the underlying stream.JsonParseException - If the content is not well formed.public String next() throws UncheckedIOException, JsonParseException
next in interface Iterator<String>NoSuchElementException - If the iteration has no more tokens.UncheckedIOException - If an IOException occurs while reading
from the underlying stream.JsonParseException - If the content is not well formed.protected int readTokenStart()
throws IOException,
JsonParseException
getPosition(). If the end of content has been
reached, this method returns -1.IOException - If an I/O error has occurred.JsonParseException - If the content is not well formed.nextToken()protected char[] buf()
char[] buffer of the underlying
JsonReplayReader.char[] buffer of the underlying
JsonReplayReader.Copyright © 2020 OpenJAX. All rights reserved.