Class ReaderBasedJsonParser

  • All Implemented Interfaces:
    Versioned, java.io.Closeable, java.lang.AutoCloseable

    public class ReaderBasedJsonParser
    extends ParserBase
    This is a concrete implementation of JsonParser, which is based on a Reader to handle low-level character conversion tasks.
    • Constructor Detail

      • ReaderBasedJsonParser

        public ReaderBasedJsonParser​(IOContext ctxt,
                                     int features,
                                     java.io.Reader r,
                                     ObjectCodec codec,
                                     CharsToNameCanonicalizer st,
                                     char[] inputBuffer,
                                     int start,
                                     int end,
                                     boolean bufferRecyclable)
        Constructor called when caller wants to provide input buffer directly (or needs to, in case of bootstrapping having read some of contents) and it may or may not be recyclable use standard recycle context.
        Parameters:
        ctxt - I/O context to use
        features - Standard stream read features enabled
        r - Reader used for reading actual content, if any; null if none
        codec - ObjectCodec to delegate object deserialization to
        st - Name canonicalizer to use
        inputBuffer - Input buffer to read initial content from (before Reader)
        start - Pointer in inputBuffer that has the first content character to decode
        end - Pointer past the last content character in inputBuffer
        bufferRecyclable - Whether inputBuffer passed is managed by Jackson core (and thereby needs recycling)
        Since:
        2.4
      • ReaderBasedJsonParser

        public ReaderBasedJsonParser​(IOContext ctxt,
                                     int features,
                                     java.io.Reader r,
                                     ObjectCodec codec,
                                     CharsToNameCanonicalizer st)
        Constructor called when input comes as a Reader, and buffer allocation can be done using default mechanism.
        Parameters:
        ctxt - I/O context to use
        features - Standard stream read features enabled
        r - Reader used for reading actual content, if any; null if none
        codec - ObjectCodec to delegate object deserialization to
        st - Name canonicalizer to use
    • Method Detail

      • getReadCapabilities

        public JacksonFeatureSet<StreamReadCapability> getReadCapabilities()
        Description copied from class: JsonParser
        Accessor for getting metadata on capabilities of this parser, based on underlying data format being read (directly or indirectly).
        Overrides:
        getReadCapabilities in class JsonParser
        Returns:
        Set of read capabilities for content to read via this parser
      • releaseBuffered

        public int releaseBuffered​(java.io.Writer w)
                            throws java.io.IOException
        Description copied from class: JsonParser
        Method that can be called to push back any content that has been read but not consumed by the parser. This is usually done after reading all content of interest using parser. Content is released by writing it to given writer if possible; if underlying input is char-based it can released, if not (byte-based) it can not.
        Overrides:
        releaseBuffered in class JsonParser
        Parameters:
        w - Writer to which buffered but unprocessed content is written to
        Returns:
        -1 if the underlying content source is not char-based (that is, input can not be sent to Writer; otherwise number of chars released (0 if there was nothing to release)
        Throws:
        java.io.IOException - if write using Writer threw exception
      • getInputSource

        public java.lang.Object getInputSource()
        Description copied from class: JsonParser
        Method that can be used to get access to object that is used to access input being parsed; this is usually either InputStream or Reader, depending on what parser was constructed with. Note that returned value may be null in some cases; including case where parser implementation does not want to exposed raw source to caller. In cases where input has been decorated, object returned here is the decorated version; this allows some level of interaction between users of parser and decorator object.

        In general use of this accessor should be considered as "last effort", i.e. only used if no other mechanism is applicable.

        Overrides:
        getInputSource in class JsonParser
        Returns:
        Input source this parser was configured with
      • getText

        public final java.lang.String getText()
                                       throws java.io.IOException
        Method for accessing textual representation of the current event; if no current event (before first call to nextToken(), or after encountering end-of-input), returns null. Method can be called for any event.
        Specified by:
        getText in class ParserMinimalBase
        Returns:
        Textual value associated with the current token (one returned by JsonParser.nextToken() or other iteration methods)
        Throws:
        java.io.IOException - for low-level read issues, or JsonParseException for decoding problems
      • getText

        public int getText​(java.io.Writer writer)
                    throws java.io.IOException
        Description copied from class: JsonParser
        Method to read the textual representation of the current token in chunks and pass it to the given Writer. Conceptually same as calling:
          writer.write(parser.getText());
        
        but should typically be more efficient as longer content does need to be combined into a single String to return, and write can occur directly from intermediate buffers Jackson uses.
        Overrides:
        getText in class JsonParser
        Parameters:
        writer - Writer to write textual content to
        Returns:
        The number of characters written to the Writer
        Throws:
        java.io.IOException - for low-level read issues or writes using passed writer, or JsonParseException for decoding problems
      • getValueAsString

        public final java.lang.String getValueAsString()
                                                throws java.io.IOException
        Description copied from class: JsonParser
        Method that will try to convert value of current token to a String. JSON Strings map naturally; scalar values get converted to their textual representation. If representation can not be converted to a String value (including structured types like Objects and Arrays and null token), default value of null will be returned; no exceptions are thrown.
        Overrides:
        getValueAsString in class ParserMinimalBase
        Returns:
        String value current token is converted to, if possible; null otherwise
        Throws:
        java.io.IOException - for low-level read issues, or JsonParseException for decoding problems
      • getValueAsString

        public final java.lang.String getValueAsString​(java.lang.String defValue)
                                                throws java.io.IOException
        Description copied from class: JsonParser
        Method that will try to convert value of current token to a String. JSON Strings map naturally; scalar values get converted to their textual representation. If representation can not be converted to a String value (including structured types like Objects and Arrays and null token), specified default value will be returned; no exceptions are thrown.
        Overrides:
        getValueAsString in class ParserMinimalBase
        Parameters:
        defValue - Default value to return if conversion to String is not possible
        Returns:
        String value current token is converted to, if possible; def otherwise
        Throws:
        java.io.IOException - for low-level read issues, or JsonParseException for decoding problems
      • getTextCharacters

        public final char[] getTextCharacters()
                                       throws java.io.IOException
        Description copied from class: JsonParser
        Method similar to JsonParser.getText(), but that will return underlying (unmodifiable) character array that contains textual value, instead of constructing a String object to contain this information. Note, however, that:
        • Textual contents are not guaranteed to start at index 0 (rather, call JsonParser.getTextOffset()) to know the actual offset
        • Length of textual contents may be less than the length of returned buffer: call JsonParser.getTextLength() for actual length of returned content.

        Note that caller MUST NOT modify the returned character array in any way -- doing so may corrupt current parser state and render parser instance useless.

        The only reason to call this method (over JsonParser.getText()) is to avoid construction of a String object (which will make a copy of contents).

        Specified by:
        getTextCharacters in class ParserMinimalBase
        Returns:
        Buffer that contains the current textual value (but not necessarily at offset 0, and not necessarily until the end of buffer)
        Throws:
        java.io.IOException - for low-level read issues, or JsonParseException for decoding problems
      • getBinaryValue

        public byte[] getBinaryValue​(Base64Variant b64variant)
                              throws java.io.IOException
        Description copied from class: JsonParser
        Method that can be used to read (and consume -- results may not be accessible using other methods after the call) base64-encoded binary data included in the current textual JSON value. It works similar to getting String value via JsonParser.getText() and decoding result (except for decoding part), but should be significantly more performant.

        Note that non-decoded textual contents of the current token are not guaranteed to be accessible after this method is called. Current implementation, for example, clears up textual content during decoding. Decoded binary content, however, will be retained until parser is advanced to the next event.

        Overrides:
        getBinaryValue in class ParserBase
        Parameters:
        b64variant - Expected variant of base64 encoded content (see Base64Variants for definitions of "standard" variants).
        Returns:
        Decoded binary data
        Throws:
        java.io.IOException - for low-level read issues, or JsonParseException for decoding problems
      • readBinaryValue

        public int readBinaryValue​(Base64Variant b64variant,
                                   java.io.OutputStream out)
                            throws java.io.IOException
        Description copied from class: JsonParser
        Similar to JsonParser.readBinaryValue(OutputStream) but allows explicitly specifying base64 variant to use.
        Overrides:
        readBinaryValue in class JsonParser
        Parameters:
        b64variant - base64 variant to use
        out - Output stream to use for passing decoded binary data
        Returns:
        Number of bytes that were decoded and written via OutputStream
        Throws:
        java.io.IOException - for low-level read issues, or JsonParseException for decoding problems
      • nextToken

        public final JsonToken nextToken()
                                  throws java.io.IOException
        Description copied from class: JsonParser
        Main iteration method, which will advance stream enough to determine type of the next token, if any. If none remaining (stream has no content other than possible white space before ending), null will be returned.
        Specified by:
        nextToken in class ParserMinimalBase
        Returns:
        Next token from the stream, if any found, or null to indicate end-of-input
        Throws:
        java.io.IOException - for low-level read issues, or JsonParseException for decoding problems
      • finishToken

        public void finishToken()
                         throws java.io.IOException
        Description copied from class: JsonParser
        Method that may be used to force full handling of the current token so that even if lazy processing is enabled, the whole contents are read for possible retrieval. This is usually used to ensure that the token end location is available, as well as token contents (similar to what calling, say JsonParser.getTextCharacters(), would achieve).

        Note that for many dataformat implementations this method will not do anything; this is the default implementation unless overridden by sub-classes.

        Overrides:
        finishToken in class JsonParser
        Throws:
        java.io.IOException - for low-level read issues, or JsonParseException for decoding problems
      • nextFieldName

        public boolean nextFieldName​(SerializableString sstr)
                              throws java.io.IOException
        Description copied from class: JsonParser
        Method that fetches next token (as if calling JsonParser.nextToken()) and verifies whether it is JsonToken.FIELD_NAME with specified name and returns result of that comparison. It is functionally equivalent to:
          return (nextToken() == JsonToken.FIELD_NAME) && str.getValue().equals(getCurrentName());
        
        but may be faster for parser to verify, and can therefore be used if caller expects to get such a property name from input next.
        Overrides:
        nextFieldName in class JsonParser
        Parameters:
        sstr - Property name to compare next token to (if next token is JsonToken.FIELD_NAME)
        Returns:
        True if parser advanced to JsonToken.FIELD_NAME with specified name; false otherwise (different token or non-matching name)
        Throws:
        java.io.IOException - for low-level read issues, or JsonParseException for decoding problems
      • nextTextValue

        public final java.lang.String nextTextValue()
                                             throws java.io.IOException
        Description copied from class: JsonParser
        Method that fetches next token (as if calling JsonParser.nextToken()) and if it is JsonToken.VALUE_STRING returns contained String value; otherwise returns null. It is functionally equivalent to:
          return (nextToken() == JsonToken.VALUE_STRING) ? getText() : null;
        
        but may be faster for parser to process, and can therefore be used if caller expects to get a String value next from input.
        Overrides:
        nextTextValue in class JsonParser
        Returns:
        Text value of the JsonToken.VALUE_STRING token parser advanced to; or null if next token is of some other type
        Throws:
        java.io.IOException - for low-level read issues, or JsonParseException for decoding problems
      • nextIntValue

        public final int nextIntValue​(int defaultValue)
                               throws java.io.IOException
        Description copied from class: JsonParser
        Method that fetches next token (as if calling JsonParser.nextToken()) and if it is JsonToken.VALUE_NUMBER_INT returns 32-bit int value; otherwise returns specified default value It is functionally equivalent to:
          return (nextToken() == JsonToken.VALUE_NUMBER_INT) ? getIntValue() : defaultValue;
        
        but may be faster for parser to process, and can therefore be used if caller expects to get an int value next from input.

        NOTE: value checks are performed similar to JsonParser.getIntValue()

        Overrides:
        nextIntValue in class JsonParser
        Parameters:
        defaultValue - Value to return if next token is NOT of type JsonToken.VALUE_NUMBER_INT
        Returns:
        Integer (int) value of the JsonToken.VALUE_NUMBER_INT token parser advanced to; or defaultValue if next token is of some other type
        Throws:
        java.io.IOException - for low-level read issues, or JsonParseException for decoding problems
        InputCoercionException - if integer number does not fit in Java int
      • nextLongValue

        public final long nextLongValue​(long defaultValue)
                                 throws java.io.IOException
        Description copied from class: JsonParser
        Method that fetches next token (as if calling JsonParser.nextToken()) and if it is JsonToken.VALUE_NUMBER_INT returns 64-bit long value; otherwise returns specified default value It is functionally equivalent to:
          return (nextToken() == JsonToken.VALUE_NUMBER_INT) ? getLongValue() : defaultValue;
        
        but may be faster for parser to process, and can therefore be used if caller expects to get a long value next from input.

        NOTE: value checks are performed similar to JsonParser.getLongValue()

        Overrides:
        nextLongValue in class JsonParser
        Parameters:
        defaultValue - Value to return if next token is NOT of type JsonToken.VALUE_NUMBER_INT
        Returns:
        long value of the JsonToken.VALUE_NUMBER_INT token parser advanced to; or defaultValue if next token is of some other type
        Throws:
        java.io.IOException - for low-level read issues, or JsonParseException for decoding problems
        InputCoercionException - if integer number does not fit in Java long
      • nextBooleanValue

        public final java.lang.Boolean nextBooleanValue()
                                                 throws java.io.IOException
        Description copied from class: JsonParser
        Method that fetches next token (as if calling JsonParser.nextToken()) and if it is JsonToken.VALUE_TRUE or JsonToken.VALUE_FALSE returns matching Boolean value; otherwise return null. It is functionally equivalent to:
          JsonToken t = nextToken();
          if (t == JsonToken.VALUE_TRUE) return Boolean.TRUE;
          if (t == JsonToken.VALUE_FALSE) return Boolean.FALSE;
          return null;
        
        but may be faster for parser to process, and can therefore be used if caller expects to get a Boolean value next from input.
        Overrides:
        nextBooleanValue in class JsonParser
        Returns:
        Boolean value of the JsonToken.VALUE_TRUE or JsonToken.VALUE_FALSE token parser advanced to; or null if next token is of some other type
        Throws:
        java.io.IOException - for low-level read issues, or JsonParseException for decoding problems
      • getTokenLocation

        public JsonLocation getTokenLocation()
        Description copied from class: ParserBase
        Method that return the starting location of the current token; that is, position of the first character from input that starts the current token.
        Overrides:
        getTokenLocation in class ParserBase
        Returns:
        Starting location of the token parser currently points to
      • getCurrentLocation

        public JsonLocation getCurrentLocation()
        Description copied from class: ParserBase
        Method that returns location of the last processed character; usually for error reporting purposes
        Overrides:
        getCurrentLocation in class ParserBase
        Returns:
        Location of the last processed input unit (byte or character)