Class AbstractStreamDecoder

java.lang.Object
io.micronaut.serde.LimitingStream
io.micronaut.serde.support.AbstractStreamDecoder
All Implemented Interfaces:
Decoder, AutoCloseable
Direct Known Subclasses:
AbstractDecoderPerStructureStreamDecoder, JsonParserDecoder, OracleJdbcJsonParserDecoder

@Internal public abstract class AbstractStreamDecoder extends LimitingStream implements Decoder
Abstract base class for stream-based Decoders.
  • Constructor Details

  • Method Details

    • currentToken

      @Nullable protected abstract @Nullable AbstractStreamDecoder.TokenType currentToken()
      Returns:
      The current token.
    • nextToken

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

      protected 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.
    • preDecodeValue

      protected void preDecodeValue(AbstractStreamDecoder.TokenType currentToken)
      Should be called before attempting to decode a value. Has basic sanity checks, such as confirming we're not currently in a AbstractStreamDecoder.TokenType.KEY and that there's no child decoder currently running.
      Parameters:
      currentToken - The current token
    • finishStructure

      public void finishStructure(boolean consumeLeftElements) throws IOException
      Specified by:
      finishStructure in interface Decoder
      Parameters:
      consumeLeftElements - True to consume left elements
      Throws:
      IOException - If an unrecoverable error occurs
    • consumeLeftElements

      protected void consumeLeftElements(AbstractStreamDecoder.TokenType currentToken) throws IOException
      Consumes left elements.
      Parameters:
      currentToken - The current token
      Throws:
      IOException
    • hasNextArrayValue

      public boolean hasNextArrayValue()
      Specified by:
      hasNextArrayValue in interface Decoder
      Returns:
      Returns true if another array value is available.
    • getCurrentKey

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

      @Nullable public @Nullable String decodeKey() throws IOException
      Description copied from interface: Decoder
      Decodes a key, if there are no more keys to decode returns null.
      Specified by:
      decodeKey in interface Decoder
      Returns:
      The key or null if there aren't any more keys
      Throws:
      IOException - If an unrecoverable error occurs
    • decodeArray

      @NonNull public final @NonNull Decoder decodeArray(io.micronaut.core.type.Argument<?> type) throws IOException
      Description copied from interface: Decoder
      Start decoding an array.
      Specified by:
      decodeArray in interface Decoder
      Parameters:
      type - The array type
      Returns:
      The array decoder
      Throws:
      IOException - If an unrecoverable error occurs
    • decodeArray0

      protected AbstractStreamDecoder decodeArray0(AbstractStreamDecoder.TokenType currentToken) throws IOException
      Decodes the array.
      Parameters:
      currentToken - The current token
      Returns:
      The decoder
      Throws:
      IOException - The exception
    • decodeObject

      @NonNull public final @NonNull Decoder decodeObject(io.micronaut.core.type.Argument<?> type) throws IOException
      Description copied from interface: Decoder
      Decodes an object.
      Specified by:
      decodeObject in interface Decoder
      Parameters:
      type - The type, never null
      Returns:
      The object decoder
      Throws:
      IOException - If an unrecoverable error occurs
    • decodeObject0

      protected AbstractStreamDecoder decodeObject0(AbstractStreamDecoder.TokenType currentToken) throws IOException
      Decodes the object.
      Parameters:
      currentToken - The current token
      Returns:
      The decoder
      Throws:
      IOException - The exception
    • coerceScalarToString

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

      @NonNull public @NonNull String decodeString() throws IOException
      Description copied from interface: Decoder
      Decodes a string.
      Specified by:
      decodeString in interface Decoder
      Returns:
      The string, never null
      Throws:
      IOException - If an unrecoverable error occurs
    • getString

      protected abstract String getString() throws IOException
      Decode the current AbstractStreamDecoder.TokenType.STRING value. Called for no other token type.
      Returns:
      The String value
      Throws:
      IOException - if an unrecoverable error occurs
    • getBoolean

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

      public final boolean decodeBoolean() throws IOException
      Description copied from interface: Decoder
      Decodes a boolean.
      Specified by:
      decodeBoolean in interface Decoder
      Returns:
      The boolean
      Throws:
      IOException - If an unrecoverable error occurs
    • decodeByte

      public final byte decodeByte() throws IOException
      Description copied from interface: Decoder
      Decodes a byte.
      Specified by:
      decodeByte in interface Decoder
      Returns:
      The byte
      Throws:
      IOException - If an unrecoverable error occurs
    • decodeShort

      public final short decodeShort() throws IOException
      Description copied from interface: Decoder
      Decodes a short.
      Specified by:
      decodeShort in interface Decoder
      Returns:
      The short
      Throws:
      IOException - If an unrecoverable error occurs
    • decodeChar

      public final char decodeChar() throws IOException
      Description copied from interface: Decoder
      Decodes a char.
      Specified by:
      decodeChar in interface Decoder
      Returns:
      The char
      Throws:
      IOException - If an unrecoverable error occurs
    • decodeInt

      public final int decodeInt() throws IOException
      Description copied from interface: Decoder
      Decodes a int.
      Specified by:
      decodeInt in interface Decoder
      Returns:
      The int
      Throws:
      IOException - If an unrecoverable error occurs
    • decodeLong

      public final long decodeLong() throws IOException
      Description copied from interface: Decoder
      Decodes a long.
      Specified by:
      decodeLong in interface Decoder
      Returns:
      The long
      Throws:
      IOException - If an unrecoverable error occurs
    • getLong

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

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

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

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

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

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

      protected BigDecimal getBigDecimalFromNumber(Number number)
      Converts the number, probably retrieved by calling getBestNumber(), to a BigDecimal.
      Parameters:
      number - The number value
      Returns:
      The number as a big decimal
    • getBestNumberNode

      protected io.micronaut.json.tree.JsonNode getBestNumberNode() throws 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:
      IOException - if an unrecoverable error occurs
    • decodeFloat

      public final float decodeFloat() throws IOException
      Description copied from interface: Decoder
      Decodes a float.
      Specified by:
      decodeFloat in interface Decoder
      Returns:
      The float
      Throws:
      IOException - If an unrecoverable error occurs
    • decodeDouble

      public final double decodeDouble() throws IOException
      Description copied from interface: Decoder
      Decodes a double.
      Specified by:
      decodeDouble in interface Decoder
      Returns:
      The double
      Throws:
      IOException - If an unrecoverable error occurs
    • decodeBigInteger

      @NonNull public final @NonNull BigInteger decodeBigInteger() throws IOException
      Description copied from interface: Decoder
      Decodes a BigInteger.
      Specified by:
      decodeBigInteger in interface Decoder
      Returns:
      The BigInteger, never null
      Throws:
      IOException - If an unrecoverable error occurs
    • decodeBigDecimal

      @NonNull public final @NonNull BigDecimal decodeBigDecimal() throws IOException
      Description copied from interface: Decoder
      Decodes a BigDecimal.
      Specified by:
      decodeBigDecimal in interface Decoder
      Returns:
      The BigDecimal, never null
      Throws:
      IOException - If an unrecoverable error occurs
    • decodeNumber

      protected final <T> T decodeNumber(AbstractStreamDecoder.TokenType currentToken, AbstractStreamDecoder.ValueDecoder<T> fromNumberToken, Function<String,T> fromString, T zero, T one) throws IOException
      Decode a number type, applying all necessary coercions.
      Type Parameters:
      T - The number type.
      Parameters:
      currentToken - The current token
      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:
      IOException - if an unrecoverable error occurs
    • decodeBinary

      public byte @NonNull [] decodeBinary() throws IOException
      Description copied from interface: Decoder
      Decode binary data from this stream. Binary data can be serialized in multiple different ways that differ by format.
      • An array of numbers must be supported by all implementations, for compatibility. This is also the default implementation.
      • A base64 string. This is convenient for text-based formats like json, and is supported by jackson.
      • A format-specific type, for binary formats such as bson.
      • Other format specific behavior. Oracle JDBC Json will parse strings as hex, for example.
      Implementations must support the array shape, but the other shapes are optional.
      Specified by:
      decodeBinary in interface Decoder
      Returns:
      The decoded byte array
      Throws:
      IOException
    • decodeCustom

      protected final <T> T decodeCustom(AbstractStreamDecoder.ValueDecoder<T> readFunction) throws 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:
      IOException - if an unrecoverable error occurs
    • decodeCustom

      protected final <T> T decodeCustom(AbstractStreamDecoder.ValueDecoder<T> readFunction, boolean callNext) throws 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:
      IOException - if an unrecoverable error occurs
    • decodeNull

      public final boolean decodeNull() throws IOException
      Description copied from interface: Decoder
      Attempt to decode a null value. Returns false if this value is not null, and another method should be used for decoding. Returns true if this value was null, and the cursor has been advanced to the next value.
      Specified by:
      decodeNull in interface Decoder
      Returns:
      Returns true if the value was null
      Throws:
      IOException - If an unrecoverable error occurs
    • decodeBuffer

      public Decoder decodeBuffer() throws IOException
      Description copied from interface: Decoder
      Buffer the whole subtree of this value and return it as a new Decoder. The returned Decoder can be used independently to this Decoder. This means actual parsing of the subtree can be delayed.

      The returned Decoder should behave identically to this Decoder. This means that for example decoder.decodeDouble() should be equivalent to decoder.decodeBuffer().decodeDouble().

      Specified by:
      decodeBuffer in interface Decoder
      Returns:
      An independent decoder that visits this subtree.
      Throws:
      IOException - If an unrecoverable error occurs
    • decodeNode

      @NonNull public @NonNull io.micronaut.json.tree.JsonNode decodeNode() throws IOException
      Description copied from interface: Decoder
      Decodes the current state into a JsonNode.
      Specified by:
      decodeNode in interface Decoder
      Returns:
      The decoded node
      Throws:
      IOException - If an unrecoverable error occurs
    • decodeArbitrary

      @Nullable public final @Nullable Object decodeArbitrary() throws IOException
      Description copied from interface: Decoder
      Decodes the current state into an arbitrary object.

      The following should be decoded by this method:

      • Object types will be decoded into a Map
      • Array types will be decoded into a List
      • JSON primitive types into the equivalent Java wrapper type
      Specified by:
      decodeArbitrary in interface Decoder
      Returns:
      The decoded object
      Throws:
      IOException - If an unrecoverable error occurs
    • skipChildren

      protected abstract void skipChildren() throws IOException
      Throws:
      IOException - if an unrecoverable error occurs
    • skipValue

      public final void skipValue() throws IOException
      Description copied from interface: Decoder
      Skips the current value.
      Specified by:
      skipValue in interface Decoder
      Throws:
      IOException - If an unrecoverable error occurs