Class AbstractStreamDecoder

  • All Implemented Interfaces:
    io.micronaut.serde.Decoder, java.lang.AutoCloseable
    Direct Known Subclasses:
    AbstractChildReuseStreamDecoder

    @Internal
    public abstract class AbstractStreamDecoder
    extends java.lang.Object
    implements io.micronaut.serde.Decoder
    Abstract base class for stream-based Decoders.
    • Constructor Detail

      • AbstractStreamDecoder

        protected AbstractStreamDecoder​(@NonNull
                                        AbstractStreamDecoder parent)
        Child constructor. Should inherit the parser from the parent.
        Parameters:
        parent - The parent decoder.
      • AbstractStreamDecoder

        protected AbstractStreamDecoder​(@NonNull
                                        java.lang.Class<?> view)
        Root constructor.
        Parameters:
        view - The selected view class.
    • Method Detail

      • nextToken

        protected abstract void nextToken()
                                   throws java.io.IOException
        Move to the next token.
        Throws:
        java.io.IOException - if an unrecoverable error occurs
      • unexpectedToken

        protected java.io.IOException unexpectedToken​(AbstractStreamDecoder.TokenType expected)
        Parameters:
        expected - The token type that was expected in place of currentToken().
        Returns:
        The exception that should be thrown to signify an unexpected token.
      • finishStructure

        public final void finishStructure()
                                   throws java.io.IOException
        Specified by:
        finishStructure in interface io.micronaut.serde.Decoder
        Throws:
        java.io.IOException
      • hasNextArrayValue

        public final boolean hasNextArrayValue()
        Specified by:
        hasNextArrayValue in interface io.micronaut.serde.Decoder
      • getCurrentKey

        protected abstract java.lang.String getCurrentKey()
                                                   throws java.io.IOException
        Get the current object field name. Only called for AbstractStreamDecoder.TokenType.KEY.
        Returns:
        The current field key
        Throws:
        java.io.IOException - if an unrecoverable error occurs
      • decodeKey

        @Nullable
        public final java.lang.String decodeKey()
                                         throws java.io.IOException
        Specified by:
        decodeKey in interface io.micronaut.serde.Decoder
        Throws:
        java.io.IOException
      • decodeArray

        @NonNull
        public final io.micronaut.serde.Decoder decodeArray​(io.micronaut.core.type.Argument<?> type)
                                                     throws java.io.IOException
        Specified by:
        decodeArray in interface io.micronaut.serde.Decoder
        Throws:
        java.io.IOException
      • decodeObject

        @NonNull
        public final io.micronaut.serde.Decoder decodeObject​(io.micronaut.core.type.Argument<?> type)
                                                      throws java.io.IOException
        Specified by:
        decodeObject in interface io.micronaut.serde.Decoder
        Throws:
        java.io.IOException
      • coerceScalarToString

        protected abstract java.lang.String coerceScalarToString()
                                                          throws java.io.IOException
        Decode any non-null scalar value (number, string or boolean) to its string representation.
        Returns:
        The current value, coerced to a string
        Throws:
        java.io.IOException - if an unrecoverable error occurs
      • decodeString

        @NonNull
        public final java.lang.String decodeString()
                                            throws java.io.IOException
        Specified by:
        decodeString in interface io.micronaut.serde.Decoder
        Throws:
        java.io.IOException
      • getBoolean

        protected abstract boolean getBoolean()
                                       throws java.io.IOException
        Decode the current AbstractStreamDecoder.TokenType.BOOLEAN value. Called for no other token type.
        Returns:
        The boolean value
        Throws:
        java.io.IOException - if an unrecoverable error occurs
      • decodeBoolean

        public final boolean decodeBoolean()
                                    throws java.io.IOException
        Specified by:
        decodeBoolean in interface io.micronaut.serde.Decoder
        Throws:
        java.io.IOException
      • decodeByte

        public final byte decodeByte()
                              throws java.io.IOException
        Specified by:
        decodeByte in interface io.micronaut.serde.Decoder
        Throws:
        java.io.IOException
      • decodeShort

        public final short decodeShort()
                                throws java.io.IOException
        Specified by:
        decodeShort in interface io.micronaut.serde.Decoder
        Throws:
        java.io.IOException
      • decodeChar

        public final char decodeChar()
                              throws java.io.IOException
        Specified by:
        decodeChar in interface io.micronaut.serde.Decoder
        Throws:
        java.io.IOException
      • decodeInt

        public final int decodeInt()
                            throws java.io.IOException
        Specified by:
        decodeInt in interface io.micronaut.serde.Decoder
        Throws:
        java.io.IOException
      • decodeLong

        public final long decodeLong()
                              throws java.io.IOException
        Specified by:
        decodeLong in interface io.micronaut.serde.Decoder
        Throws:
        java.io.IOException
      • getLong

        protected abstract long getLong()
                                 throws java.io.IOException
        Decode the current AbstractStreamDecoder.TokenType.NUMBER value as a long value. Called for no other token type.
        Returns:
        The number value
        Throws:
        java.io.IOException - if an unrecoverable error occurs
      • getInteger

        protected int getInteger()
                          throws java.io.IOException
        Decode the current AbstractStreamDecoder.TokenType.NUMBER value as a long value. Called for no other token type.
        Returns:
        The number value
        Throws:
        java.io.IOException - if an unrecoverable error occurs
      • getDouble

        protected abstract double getDouble()
                                     throws java.io.IOException
        Decode the current AbstractStreamDecoder.TokenType.NUMBER value as a double value. Called for no other token type.
        Returns:
        The number value
        Throws:
        java.io.IOException - if an unrecoverable error occurs
      • getBigInteger

        protected abstract java.math.BigInteger getBigInteger()
                                                       throws java.io.IOException
        Decode the current AbstractStreamDecoder.TokenType.NUMBER value as a BigInteger value. Called for no other token type.
        Returns:
        The number value
        Throws:
        java.io.IOException - if an unrecoverable error occurs
      • getBigDecimal

        protected abstract java.math.BigDecimal getBigDecimal()
                                                       throws java.io.IOException
        Decode the current AbstractStreamDecoder.TokenType.NUMBER value as a BigDecimal value. Called for no other token type.
        Returns:
        The number value
        Throws:
        java.io.IOException - if an unrecoverable error occurs
      • getBestNumber

        protected abstract java.lang.Number getBestNumber()
                                                   throws java.io.IOException
        Decode the current AbstractStreamDecoder.TokenType.NUMBER value as a Number value. Called for no other token type.
        Returns:
        The number value
        Throws:
        java.io.IOException - if an unrecoverable error occurs
      • getBestNumberNode

        protected io.micronaut.json.tree.JsonNode getBestNumberNode()
                                                             throws java.io.IOException
        Decode the current AbstractStreamDecoder.TokenType.NUMBER value as a numeric JsonNode. Called for no other token type. Default implementation tries to construct a node from getBestNumber().
        Returns:
        The number value
        Throws:
        java.io.IOException - if an unrecoverable error occurs
      • decodeFloat

        public final float decodeFloat()
                                throws java.io.IOException
        Specified by:
        decodeFloat in interface io.micronaut.serde.Decoder
        Throws:
        java.io.IOException
      • decodeDouble

        public final double decodeDouble()
                                  throws java.io.IOException
        Specified by:
        decodeDouble in interface io.micronaut.serde.Decoder
        Throws:
        java.io.IOException
      • decodeBigInteger

        @NonNull
        public final java.math.BigInteger decodeBigInteger()
                                                    throws java.io.IOException
        Specified by:
        decodeBigInteger in interface io.micronaut.serde.Decoder
        Throws:
        java.io.IOException
      • decodeBigDecimal

        @NonNull
        public final java.math.BigDecimal decodeBigDecimal()
                                                    throws java.io.IOException
        Specified by:
        decodeBigDecimal in interface io.micronaut.serde.Decoder
        Throws:
        java.io.IOException
      • decodeNumber

        protected final <T> T decodeNumber​(AbstractStreamDecoder.ValueDecoder<T> fromNumberToken,
                                           java.util.function.Function<java.lang.String,​T> fromString,
                                           T zero,
                                           T one)
                                    throws java.io.IOException
        Decode a number type, applying all necessary coercions.
        Type Parameters:
        T - The number type.
        Parameters:
        fromNumberToken - Called if currentToken() is a AbstractStreamDecoder.TokenType.NUMBER.
        fromString - Called for the textual value if currentToken() is a AbstractStreamDecoder.TokenType.STRING. Should throw NumberFormatException on parse failure.
        zero - The zero value.
        one - The one value.
        Returns:
        The parsed number.
        Throws:
        java.io.IOException - if an unrecoverable error occurs
      • decodeCustom

        protected final <T> T decodeCustom​(AbstractStreamDecoder.ValueDecoder<T> readFunction)
                                    throws java.io.IOException
        Decode a custom type.
        Type Parameters:
        T - Value type
        Parameters:
        readFunction - Function to call for reading the value. The AbstractStreamDecoder parameter to the function will just be this, but this allows subclasses to avoid capturing this to avoid an allocation.
        Returns:
        The parsed value.
        Throws:
        java.io.IOException - if an unrecoverable error occurs
      • decodeCustom

        protected final <T> T decodeCustom​(AbstractStreamDecoder.ValueDecoder<T> readFunction,
                                           boolean callNext)
                                    throws java.io.IOException
        Decode a custom type.
        Type Parameters:
        T - Value type
        Parameters:
        readFunction - Function to call for reading the value. The AbstractStreamDecoder parameter to the function will just be this, but this allows subclasses to avoid capturing this to avoid an allocation.
        callNext - Pass "true" if next token should be read after invocation
        Returns:
        The parsed value.
        Throws:
        java.io.IOException - if an unrecoverable error occurs
      • decodeNull

        public final boolean decodeNull()
                                 throws java.io.IOException
        Specified by:
        decodeNull in interface io.micronaut.serde.Decoder
        Throws:
        java.io.IOException
      • decodeBuffer

        public io.micronaut.serde.Decoder decodeBuffer()
                                                throws java.io.IOException
        Specified by:
        decodeBuffer in interface io.micronaut.serde.Decoder
        Throws:
        java.io.IOException
      • decodeArbitrary

        @Nullable
        public final java.lang.Object decodeArbitrary()
                                               throws java.io.IOException
        Specified by:
        decodeArbitrary in interface io.micronaut.serde.Decoder
        Throws:
        java.io.IOException
      • skipValue

        public final void skipValue()
                             throws java.io.IOException
        Specified by:
        skipValue in interface io.micronaut.serde.Decoder
        Throws:
        java.io.IOException