Package io.micronaut.serde.support
Class AbstractStreamDecoder
- java.lang.Object
-
- io.micronaut.serde.support.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.DecoderAbstract base class for stream-basedDecoders.
-
-
Nested Class Summary
Nested Classes Modifier and Type Class Description protected static classAbstractStreamDecoder.TokenTypeThe token type.static interfaceAbstractStreamDecoder.ValueDecoder<R>Decoder function for a single value.
-
Constructor Summary
Constructors Modifier Constructor Description protectedAbstractStreamDecoder(AbstractStreamDecoder parent)Child constructor.protectedAbstractStreamDecoder(java.lang.Class<?> view)Root constructor.
-
Method Summary
All Methods Instance Methods Abstract Methods Concrete Methods Modifier and Type Method Description protected voidbackFromChild(AbstractStreamDecoder child)Called when the old child has finished processing and returns control of the stream to the parent.protected abstract java.lang.StringcoerceScalarToString()Decode any non-null scalar value (number, string or boolean) to its string representation.protected abstract AbstractStreamDecodercreateChildDecoder()Create a new child decoder usingAbstractStreamDecoder(AbstractStreamDecoder).protected abstract AbstractStreamDecoder.TokenTypecurrentToken()java.lang.ObjectdecodeArbitrary()io.micronaut.serde.DecoderdecodeArray(io.micronaut.core.type.Argument<?> type)java.math.BigDecimaldecodeBigDecimal()java.math.BigIntegerdecodeBigInteger()booleandecodeBoolean()io.micronaut.serde.DecoderdecodeBuffer()bytedecodeByte()chardecodeChar()protected <T> TdecodeCustom(AbstractStreamDecoder.ValueDecoder<T> readFunction)Decode a custom type.protected <T> TdecodeCustom(AbstractStreamDecoder.ValueDecoder<T> readFunction, boolean callNext)Decode a custom type.doubledecodeDouble()floatdecodeFloat()intdecodeInt()java.lang.StringdecodeKey()longdecodeLong()booleandecodeNull()protected <T> TdecodeNumber(AbstractStreamDecoder.ValueDecoder<T> fromNumberToken, java.util.function.Function<java.lang.String,T> fromString, T zero, T one)Decode a number type, applying all necessary coercions.io.micronaut.serde.DecoderdecodeObject(io.micronaut.core.type.Argument<?> type)shortdecodeShort()java.lang.StringdecodeString()voidfinishStructure()protected abstract java.lang.NumbergetBestNumber()Decode the currentAbstractStreamDecoder.TokenType.NUMBERvalue as aNumbervalue.protected io.micronaut.json.tree.JsonNodegetBestNumberNode()Decode the currentAbstractStreamDecoder.TokenType.NUMBERvalue as a numericJsonNode.protected abstract java.math.BigDecimalgetBigDecimal()Decode the currentAbstractStreamDecoder.TokenType.NUMBERvalue as aBigDecimalvalue.protected abstract java.math.BigIntegergetBigInteger()Decode the currentAbstractStreamDecoder.TokenType.NUMBERvalue as aBigIntegervalue.protected abstract booleangetBoolean()Decode the currentAbstractStreamDecoder.TokenType.BOOLEANvalue.protected abstract java.lang.StringgetCurrentKey()Get the current object field name.protected abstract doublegetDouble()Decode the currentAbstractStreamDecoder.TokenType.NUMBERvalue as a double value.protected intgetInteger()Decode the currentAbstractStreamDecoder.TokenType.NUMBERvalue as a long value.protected abstract longgetLong()Decode the currentAbstractStreamDecoder.TokenType.NUMBERvalue as a long value.booleanhasNextArrayValue()protected abstract voidnextToken()Move to the next token.protected abstract voidskipChildren()If we are at aAbstractStreamDecoder.TokenType.START_OBJECTorAbstractStreamDecoder.TokenType.START_ARRAY, skip to the matchingAbstractStreamDecoder.TokenType.END_OBJECTorAbstractStreamDecoder.TokenType.END_ARRAY.voidskipValue()protected java.io.IOExceptionunexpectedToken(AbstractStreamDecoder.TokenType expected)
-
-
-
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
-
currentToken
@Nullable protected abstract AbstractStreamDecoder.TokenType currentToken()
- Returns:
- The current token.
-
nextToken
protected abstract void nextToken() throws java.io.IOExceptionMove 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 ofcurrentToken().- Returns:
- The exception that should be thrown to signify an unexpected token.
-
finishStructure
public final void finishStructure() throws java.io.IOException- Specified by:
finishStructurein interfaceio.micronaut.serde.Decoder- Throws:
java.io.IOException
-
backFromChild
protected void backFromChild(AbstractStreamDecoder child) throws java.io.IOException
Called when the old child has finished processing and returns control of the stream to the parent. Current token is assumed to beAbstractStreamDecoder.TokenType.END_ARRAYorAbstractStreamDecoder.TokenType.END_OBJECT. However wecurrentToken()may hold an outdated value untilnextToken()is called, which this method does.- Parameters:
child- The now-invalid child decoder.- Throws:
java.io.IOException- if an unrecoverable error occurs
-
hasNextArrayValue
public final boolean hasNextArrayValue()
- Specified by:
hasNextArrayValuein interfaceio.micronaut.serde.Decoder
-
getCurrentKey
protected abstract java.lang.String getCurrentKey() throws java.io.IOExceptionGet the current object field name. Only called forAbstractStreamDecoder.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:
decodeKeyin interfaceio.micronaut.serde.Decoder- Throws:
java.io.IOException
-
createChildDecoder
protected abstract AbstractStreamDecoder createChildDecoder()
Create a new child decoder usingAbstractStreamDecoder(AbstractStreamDecoder).- Returns:
- The new decoder
-
decodeArray
@NonNull public final io.micronaut.serde.Decoder decodeArray(io.micronaut.core.type.Argument<?> type) throws java.io.IOException- Specified by:
decodeArrayin interfaceio.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:
decodeObjectin interfaceio.micronaut.serde.Decoder- Throws:
java.io.IOException
-
coerceScalarToString
protected abstract java.lang.String coerceScalarToString() throws java.io.IOExceptionDecode 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:
decodeStringin interfaceio.micronaut.serde.Decoder- Throws:
java.io.IOException
-
getBoolean
protected abstract boolean getBoolean() throws java.io.IOExceptionDecode the currentAbstractStreamDecoder.TokenType.BOOLEANvalue. 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:
decodeBooleanin interfaceio.micronaut.serde.Decoder- Throws:
java.io.IOException
-
decodeByte
public final byte decodeByte() throws java.io.IOException- Specified by:
decodeBytein interfaceio.micronaut.serde.Decoder- Throws:
java.io.IOException
-
decodeShort
public final short decodeShort() throws java.io.IOException- Specified by:
decodeShortin interfaceio.micronaut.serde.Decoder- Throws:
java.io.IOException
-
decodeChar
public final char decodeChar() throws java.io.IOException- Specified by:
decodeCharin interfaceio.micronaut.serde.Decoder- Throws:
java.io.IOException
-
decodeInt
public final int decodeInt() throws java.io.IOException- Specified by:
decodeIntin interfaceio.micronaut.serde.Decoder- Throws:
java.io.IOException
-
decodeLong
public final long decodeLong() throws java.io.IOException- Specified by:
decodeLongin interfaceio.micronaut.serde.Decoder- Throws:
java.io.IOException
-
getLong
protected abstract long getLong() throws java.io.IOExceptionDecode the currentAbstractStreamDecoder.TokenType.NUMBERvalue 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.IOExceptionDecode the currentAbstractStreamDecoder.TokenType.NUMBERvalue 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.IOExceptionDecode the currentAbstractStreamDecoder.TokenType.NUMBERvalue 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.IOExceptionDecode the currentAbstractStreamDecoder.TokenType.NUMBERvalue as aBigIntegervalue. 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.IOExceptionDecode the currentAbstractStreamDecoder.TokenType.NUMBERvalue as aBigDecimalvalue. 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.IOExceptionDecode the currentAbstractStreamDecoder.TokenType.NUMBERvalue as aNumbervalue. 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.IOExceptionDecode the currentAbstractStreamDecoder.TokenType.NUMBERvalue as a numericJsonNode. Called for no other token type. Default implementation tries to construct a node fromgetBestNumber().- Returns:
- The number value
- Throws:
java.io.IOException- if an unrecoverable error occurs
-
decodeFloat
public final float decodeFloat() throws java.io.IOException- Specified by:
decodeFloatin interfaceio.micronaut.serde.Decoder- Throws:
java.io.IOException
-
decodeDouble
public final double decodeDouble() throws java.io.IOException- Specified by:
decodeDoublein interfaceio.micronaut.serde.Decoder- Throws:
java.io.IOException
-
decodeBigInteger
@NonNull public final java.math.BigInteger decodeBigInteger() throws java.io.IOException- Specified by:
decodeBigIntegerin interfaceio.micronaut.serde.Decoder- Throws:
java.io.IOException
-
decodeBigDecimal
@NonNull public final java.math.BigDecimal decodeBigDecimal() throws java.io.IOException- Specified by:
decodeBigDecimalin interfaceio.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 ifcurrentToken()is aAbstractStreamDecoder.TokenType.NUMBER.fromString- Called for the textual value ifcurrentToken()is aAbstractStreamDecoder.TokenType.STRING. Should throwNumberFormatExceptionon 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. TheAbstractStreamDecoderparameter to the function will just bethis, but this allows subclasses to avoid capturingthisto 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. TheAbstractStreamDecoderparameter to the function will just bethis, but this allows subclasses to avoid capturingthisto 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:
decodeNullin interfaceio.micronaut.serde.Decoder- Throws:
java.io.IOException
-
decodeBuffer
public io.micronaut.serde.Decoder decodeBuffer() throws java.io.IOException- Specified by:
decodeBufferin interfaceio.micronaut.serde.Decoder- Throws:
java.io.IOException
-
decodeArbitrary
@Nullable public final java.lang.Object decodeArbitrary() throws java.io.IOException- Specified by:
decodeArbitraryin interfaceio.micronaut.serde.Decoder- Throws:
java.io.IOException
-
skipChildren
protected abstract void skipChildren() throws java.io.IOExceptionIf we are at aAbstractStreamDecoder.TokenType.START_OBJECTorAbstractStreamDecoder.TokenType.START_ARRAY, skip to the matchingAbstractStreamDecoder.TokenType.END_OBJECTorAbstractStreamDecoder.TokenType.END_ARRAY. Else, do nothing.- Throws:
java.io.IOException- if an unrecoverable error occurs
-
skipValue
public final void skipValue() throws java.io.IOException- Specified by:
skipValuein interfaceio.micronaut.serde.Decoder- Throws:
java.io.IOException
-
-