Package eu.woolplatform.utils.json
Class JsonStreamReader
- java.lang.Object
-
- eu.woolplatform.utils.json.JsonStreamReader
-
public class JsonStreamReader extends Object
This class can read atomic JSON tokens from a stream. After construction the reader is positioned before the first token. CallmoveNext()to move to the first token. Then you can get the current token withgetToken().
-
-
Constructor Summary
Constructors Constructor Description JsonStreamReader(InputStream input)Constructs a new JSON stream reader.JsonStreamReader(Reader reader)Constructs a new JSON stream reader.
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description voidclose()Closes this reader and the underlying reader.intgetDocumentLine()Returns the current line number in the document.intgetDocumentLinePos()Returns the current character number in the current line in the document.JsonAtomicTokengetToken()Returns the current token.intgetTokenStartLine()Returns the line number in the document where the current token starts.intgetTokenStartLinePos()Returns the character number in the line in the document where the current token starts.booleanisStringAtomic()Returns whether a string is an atomic token.booleanmoveNext()Moves to the next token.voidsetIsStringAtomic(boolean isStringAtomic)Sets whether a string is an atomic token.
-
-
-
Constructor Detail
-
JsonStreamReader
public JsonStreamReader(InputStream input)
Constructs a new JSON stream reader. It reads UTF-8 characters from the specified input stream.- Parameters:
input- the input stream
-
JsonStreamReader
public JsonStreamReader(Reader reader)
Constructs a new JSON stream reader.- Parameters:
reader- the underlying reader
-
-
Method Detail
-
close
public void close() throws IOExceptionCloses this reader and the underlying reader.- Throws:
IOException- if the underlying reader can't be closed
-
setIsStringAtomic
public void setIsStringAtomic(boolean isStringAtomic)
Sets whether a string is an atomic token. If this is true (default), you will receive tokenSTRING. Otherwise you will receive tokensSTART_STRING,STRING_CHARACTERandEND_STRING. Reading a string as an atomic token is much more efficient, but you may want to receive separate character tokens for long strings.- Parameters:
isStringAtomic- true if a string is an atomic token (default), false otherwise
-
isStringAtomic
public boolean isStringAtomic()
Returns whether a string is an atomic token. If this is true (default), you will receive tokenSTRING. Otherwise you will receive tokensSTART_STRING,STRING_CHARACTERandEND_STRING. Reading a string as an atomic token is more efficient, but you may want to receive separate character tokens for very long strings.- Returns:
- true if a string is an atomic token (default), false otherwise
-
getDocumentLine
public int getDocumentLine()
Returns the current line number in the document. The first line is 1. If the reader is positioned at a token, then the document position is at the end of that token.- Returns:
- the current line number
-
getDocumentLinePos
public int getDocumentLinePos()
Returns the current character number in the current line in the document. The first character is 1. If the reader is positioned at a token, then the document position is at the end of that token.- Returns:
- the current character number
-
getTokenStartLine
public int getTokenStartLine()
Returns the line number in the document where the current token starts. The first line is 1. If the reader is before the first token, then this method returns 1. If the reader is after the last token, then this method return the line at the end of the document.- Returns:
- the line number where the current token starts
-
getTokenStartLinePos
public int getTokenStartLinePos()
Returns the character number in the line in the document where the current token starts. The first character is 1. If the reader is before the first token, then this method returns 1. If the reader is after the last token, then this method return the line at the end of the document.- Returns:
- the character number where the current token starts
-
moveNext
public boolean moveNext() throws JsonParseException, IOExceptionMoves to the next token. The first time you call this method, it moves to the first token. If the end of the stream is reached and the complete document is valid, this method returns false, meaning there are no more tokens.- Returns:
- true if there is another token, false if the end of the document
- Throws:
JsonParseException- if the JSON content is invalidIOException- if a reading error occurs
-
getToken
public JsonAtomicToken getToken()
Returns the current token. If the reader is positioned before the first token or after the last token, this method returns null.- Returns:
- the current token or null
-
-