Package eu.woolplatform.utils.json
Class JsonObjectStreamReader
- java.lang.Object
-
- eu.woolplatform.utils.json.JsonObjectStreamReader
-
- All Implemented Interfaces:
Closeable,AutoCloseable
public class JsonObjectStreamReader extends Object implements Closeable
This class can read JSON values and tokens from a stream. This is a higher-level interface built uponJsonStreamReader, which returns atomic tokens. There are read methods that you can call when you expect a specific type of value (for example a string or an integer) or token (for example START_LIST). Each method validates and consumes the relevant tokens.
-
-
Constructor Summary
Constructors Constructor Description JsonObjectStreamReader(JsonStreamReader reader)Constructs a new JSON object stream reader.JsonObjectStreamReader(InputStream input)Constructs a new JSON object stream reader.JsonObjectStreamReader(Reader reader)Constructs a new JSON object 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.booleanreadBoolean()Reads a boolean value.bytereadByte()Reads a byte value.doublereadDouble()Reads a number token and returns its double value.floatreadFloat()Reads a number token and returns its float value.intreadInt()Reads an integer value.List<?>readList()Reads a list value.longreadLong()Reads a long value.Map<String,?>readObject()Reads an object value.shortreadShort()Reads a short value.StringreadString()Reads a string value (not null).JsonAtomicTokenreadToken()Reads the next token.JsonAtomicTokenreadToken(JsonAtomicToken.Type type)Reads the next token and validates that it has the specified type.ObjectreadValue()Reads a JSON value.
-
-
-
Constructor Detail
-
JsonObjectStreamReader
public JsonObjectStreamReader(InputStream input)
Constructs a new JSON object stream reader. It reads UTF-8 characters from the specified input stream.- Parameters:
input- the input stream
-
JsonObjectStreamReader
public JsonObjectStreamReader(Reader reader)
Constructs a new JSON object stream reader.- Parameters:
reader- the underlying reader
-
JsonObjectStreamReader
public JsonObjectStreamReader(JsonStreamReader reader)
Constructs a new JSON object stream reader.- Parameters:
reader- the underlying reader
-
-
Method Detail
-
close
public void close() throws IOExceptionCloses this reader and the underlying reader.- Specified by:
closein interfaceAutoCloseable- Specified by:
closein interfaceCloseable- Throws:
IOException- if the underlying reader can't be closed
-
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
-
getToken
public JsonAtomicToken getToken() throws JsonParseException, IOException
Returns the current token. If the reader is at the end of the document, this method returns null.- Returns:
- the current token
- Throws:
JsonParseException- if the JSON content is invalidIOException- if a reading error occurs
-
readToken
public JsonAtomicToken readToken() throws JsonParseException, IOException
Reads the next token. After this method the reader will be positioned after the returned token. If there are no more tokens, this method returns null.- Returns:
- the token or null
- Throws:
JsonParseException- if the JSON content is invalidIOException- if a reading error occurs
-
readToken
public JsonAtomicToken readToken(JsonAtomicToken.Type type) throws JsonParseException, IOException
Reads the next token and validates that it has the specified type. If this method succeeds, the reader will be positioned after the returned token. Otherwise it will remain at the current position.- Parameters:
type- the token type- Returns:
- the token with the specified type
- Throws:
JsonParseException- if the JSON content is invalid, there is no more token, or the next token has a different typeIOException- if a reading error occurs
-
readString
public String readString() throws JsonParseException, IOException
Reads a string value (not null). If this method succeeds, the reader will be positioned after the string.- Returns:
- the string (not null)
- Throws:
JsonParseException- if the JSON content is invalid or the reader is not positioned at the start of a stringIOException- if a reading error occurs
-
readByte
public byte readByte() throws JsonParseException, IOExceptionReads a byte value. This method validates whether the reader is positioned at a number token and its value is a byte. If this method succeeds, the reader will be positioned after the byte value.- Returns:
- the byte value
- Throws:
JsonParseException- if the JSON content is invalid or the reader is not positioned at the start of a byte valueIOException- if a reading error occurs
-
readShort
public short readShort() throws JsonParseException, IOExceptionReads a short value. This method validates whether the reader is positioned at a number token and its value is a short. If this method succeeds, the reader will be positioned after the short value.- Returns:
- the short value
- Throws:
JsonParseException- if the JSON content is invalid or the reader is not positioned at the start of a short valueIOException- if a reading error occurs
-
readInt
public int readInt() throws JsonParseException, IOExceptionReads an integer value. This method validates whether the reader is positioned at a number token and its value is an integer. If this method succeeds, the reader will be positioned after the integer.- Returns:
- the integer value
- Throws:
JsonParseException- if the JSON content is invalid or the reader is not positioned at the start of an integerIOException- if a reading error occurs
-
readLong
public long readLong() throws JsonParseException, IOExceptionReads a long value. This method validates whether the reader is positioned at a number token and its value is a long. If this method succeeds, the reader will be positioned after the long value.- Returns:
- the long value
- Throws:
JsonParseException- if the JSON content is invalid or the reader is not positioned at the start of a long valueIOException- if a reading error occurs
-
readFloat
public float readFloat() throws JsonParseException, IOExceptionReads a number token and returns its float value. If this method succeeds, the reader will be positioned after the number.- Returns:
- the float value
- Throws:
JsonParseException- if the JSON content is invalid or the reader is not positioned at the start of a numberIOException- if a reading error occurs
-
readDouble
public double readDouble() throws JsonParseException, IOExceptionReads a number token and returns its double value. If this method succeeds, the reader will be positioned after the number.- Returns:
- the double value
- Throws:
JsonParseException- if the JSON content is invalid or the reader is not positioned at the start of a numberIOException- if a reading error occurs
-
readBoolean
public boolean readBoolean() throws JsonParseException, IOExceptionReads a boolean value. If this method succeeds, the reader will be positioned after the boolean value.- Returns:
- the boolean value
- Throws:
JsonParseException- if the JSON content is invalid or the reader is not positioned at the start of a boolean valueIOException- if a reading error occurs
-
readObject
public Map<String,?> readObject() throws JsonParseException, IOException
Reads an object value. If this method succeeds, the reader will be positioned after the object value.- Returns:
- the object value
- Throws:
JsonParseException- if the JSON content is invalid or the reader is not positioned at the start of an object valueIOException- if a reading error occurs
-
readList
public List<?> readList() throws JsonParseException, IOException
Reads a list value. If this method succeeds, the reader will be positioned after the list value.- Returns:
- the list value
- Throws:
JsonParseException- if the JSON content is invalid or the reader is not positioned at the start of a list valueIOException- if a reading error occurs
-
readValue
public Object readValue() throws JsonParseException, IOException
Reads a JSON value. If this method succeeds, the reader will be positioned after the value. It returns one of the following types:- Returns:
- the value
- Throws:
JsonParseException- if the JSON content is invalid or the reader is not positioned at the start of a valueIOException- if a reading error occurs
-
-