Class 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 upon JsonStreamReader, 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 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

      • 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
      • 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 invalid
        IOException - 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 type
        IOException - 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 string
        IOException - if a reading error occurs
      • readByte

        public byte readByte()
                      throws JsonParseException,
                             IOException
        Reads 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 value
        IOException - if a reading error occurs
      • readShort

        public short readShort()
                        throws JsonParseException,
                               IOException
        Reads 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 value
        IOException - if a reading error occurs
      • readInt

        public int readInt()
                    throws JsonParseException,
                           IOException
        Reads 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 integer
        IOException - if a reading error occurs
      • readLong

        public long readLong()
                      throws JsonParseException,
                             IOException
        Reads 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 value
        IOException - if a reading error occurs
      • readFloat

        public float readFloat()
                        throws JsonParseException,
                               IOException
        Reads 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 number
        IOException - if a reading error occurs
      • readDouble

        public double readDouble()
                          throws JsonParseException,
                                 IOException
        Reads 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 number
        IOException - if a reading error occurs
      • readBoolean

        public boolean readBoolean()
                            throws JsonParseException,
                                   IOException
        Reads 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 value
        IOException - 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 value
        IOException - 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 value
        IOException - if a reading error occurs