Package io.micronaut.serde.support
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
Abstract base class for stream-based
Decoder
s.-
Nested Class Summary
Modifier and TypeClassDescriptionprotected static enum
The token type.static interface
Decoder function for a single value.Nested classes/interfaces inherited from class io.micronaut.serde.LimitingStream
LimitingStream.RemainingLimits
-
Field Summary
Fields inherited from class io.micronaut.serde.LimitingStream
DEFAULT_LIMITS, DEFAULT_MAXIMUM_DEPTH
-
Constructor Summary
ConstructorDescriptionAbstractStreamDecoder
(@NonNull LimitingStream.RemainingLimits remainingLimits) -
Method Summary
Modifier and TypeMethodDescriptionprotected abstract String
coerceScalarToString
(AbstractStreamDecoder.TokenType currentToken) Decode any non-null scalar value (number, string or boolean) to its string representation.protected void
consumeLeftElements
(AbstractStreamDecoder.TokenType currentToken) Consumes left elements.protected abstract @Nullable AbstractStreamDecoder.TokenType
final @Nullable Object
Decodes the current state into an arbitrary object.final @NonNull Decoder
decodeArray
(io.micronaut.core.type.Argument<?> type) Start decoding an array.protected AbstractStreamDecoder
decodeArray0
(AbstractStreamDecoder.TokenType currentToken) Decodes the array.final @NonNull BigDecimal
Decodes a BigDecimal.final @NonNull BigInteger
Decodes a BigInteger.byte @NonNull []
Decode binary data from this stream.final boolean
Decodes a boolean.Buffer the whole subtree of this value and return it as a newDecoder
.final byte
Decodes a byte.final char
Decodes a char.protected final <T> T
decodeCustom
(AbstractStreamDecoder.ValueDecoder<T> readFunction) Decode a custom type.protected final <T> T
decodeCustom
(AbstractStreamDecoder.ValueDecoder<T> readFunction, boolean callNext) Decode a custom type.final double
Decodes a double.final float
Decodes a float.final int
Decodes a int.@Nullable String
Decodes a key, if there are no more keys to decode returnsnull
.final long
Decodes a long.@NonNull io.micronaut.json.tree.JsonNode
Decodes the current state into aJsonNode
.final boolean
Attempt to decode a null value.protected final <T> T
decodeNumber
(AbstractStreamDecoder.TokenType currentToken, AbstractStreamDecoder.ValueDecoder<T> fromNumberToken, Function<String, T> fromString, T zero, T one) Decode a number type, applying all necessary coercions.final @NonNull Decoder
decodeObject
(io.micronaut.core.type.Argument<?> type) Decodes an object.protected AbstractStreamDecoder
decodeObject0
(AbstractStreamDecoder.TokenType currentToken) Decodes the object.final short
Decodes a short.@NonNull String
Decodes a string.void
finishStructure
(boolean consumeLeftElements) protected abstract Number
Decode the currentAbstractStreamDecoder.TokenType.NUMBER
value as aNumber
value.protected io.micronaut.json.tree.JsonNode
Decode the currentAbstractStreamDecoder.TokenType.NUMBER
value as a numericJsonNode
.protected abstract BigDecimal
Decode the currentAbstractStreamDecoder.TokenType.NUMBER
value as aBigDecimal
value.protected BigDecimal
getBigDecimalFromNumber
(Number number) Converts the number, probably retrieved by callinggetBestNumber()
, to aBigDecimal
.protected abstract BigInteger
Decode the currentAbstractStreamDecoder.TokenType.NUMBER
value as aBigInteger
value.protected abstract boolean
Decode the currentAbstractStreamDecoder.TokenType.BOOLEAN
value.protected abstract String
Get the current object field name.protected abstract double
Decode the currentAbstractStreamDecoder.TokenType.NUMBER
value as a double value.protected int
Decode the currentAbstractStreamDecoder.TokenType.NUMBER
value as a long value.protected abstract long
getLong()
Decode the currentAbstractStreamDecoder.TokenType.NUMBER
value as a long value.protected abstract String
Decode the currentAbstractStreamDecoder.TokenType.STRING
value.boolean
protected abstract void
Move to the next token.protected void
preDecodeValue
(AbstractStreamDecoder.TokenType currentToken) Should be called before attempting to decode a value.protected abstract void
If we are at aAbstractStreamDecoder.TokenType.START_OBJECT
orAbstractStreamDecoder.TokenType.START_ARRAY
, skip to the matchingAbstractStreamDecoder.TokenType.END_OBJECT
orAbstractStreamDecoder.TokenType.END_ARRAY
.final void
Skips the current value.protected IOException
Methods inherited from class io.micronaut.serde.LimitingStream
childLimits, decreaseDepth, increaseDepth, limitsFromConfiguration, ourLimits
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
Methods inherited from interface io.micronaut.serde.Decoder
close, createDeserializationException, decodeArray, decodeBigDecimalNullable, decodeBigIntegerNullable, decodeBinaryNullable, decodeBooleanNullable, decodeByteNullable, decodeCharNullable, decodeDoubleNullable, decodeFloatNullable, decodeIntNullable, decodeLongNullable, decodeNumber, decodeNumberNullable, decodeObject, decodeShortNullable, decodeStringNullable, finishStructure
-
Constructor Details
-
AbstractStreamDecoder
-
-
Method Details
-
currentToken
- Returns:
- The current token.
-
nextToken
Move to the next token.- Throws:
IOException
- if an unrecoverable error occurs
-
unexpectedToken
- Parameters:
expected
- The token type that was expected in place ofcurrentToken()
.- Returns:
- The exception that should be thrown to signify an unexpected token.
-
preDecodeValue
Should be called before attempting to decode a value. Has basic sanity checks, such as confirming we're not currently in aAbstractStreamDecoder.TokenType.KEY
and that there's no child decoder currently running.- Parameters:
currentToken
- The current token
-
finishStructure
- Specified by:
finishStructure
in interfaceDecoder
- Parameters:
consumeLeftElements
- True to consume left elements- Throws:
IOException
- If an unrecoverable error occurs
-
consumeLeftElements
Consumes left elements.- Parameters:
currentToken
- The current token- Throws:
IOException
-
hasNextArrayValue
public boolean hasNextArrayValue()- Specified by:
hasNextArrayValue
in interfaceDecoder
- Returns:
- Returns
true
if another array value is available.
-
getCurrentKey
Get the current object field name. Only called forAbstractStreamDecoder.TokenType.KEY
.- Returns:
- The current field key
- Throws:
IOException
- if an unrecoverable error occurs
-
decodeKey
Description copied from interface:Decoder
Decodes a key, if there are no more keys to decode returnsnull
.- Specified by:
decodeKey
in interfaceDecoder
- 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 interfaceDecoder
- 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 interfaceDecoder
- Parameters:
type
- The type, nevernull
- 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
Description copied from interface:Decoder
Decodes a string.- Specified by:
decodeString
in interfaceDecoder
- Returns:
- The string, never
null
- Throws:
IOException
- If an unrecoverable error occurs
-
getString
Decode the currentAbstractStreamDecoder.TokenType.STRING
value. Called for no other token type.- Returns:
- The String value
- Throws:
IOException
- if an unrecoverable error occurs
-
getBoolean
Decode the currentAbstractStreamDecoder.TokenType.BOOLEAN
value. Called for no other token type.- Returns:
- The boolean value
- Throws:
IOException
- if an unrecoverable error occurs
-
decodeBoolean
Description copied from interface:Decoder
Decodes a boolean.- Specified by:
decodeBoolean
in interfaceDecoder
- Returns:
- The boolean
- Throws:
IOException
- If an unrecoverable error occurs
-
decodeByte
Description copied from interface:Decoder
Decodes a byte.- Specified by:
decodeByte
in interfaceDecoder
- Returns:
- The byte
- Throws:
IOException
- If an unrecoverable error occurs
-
decodeShort
Description copied from interface:Decoder
Decodes a short.- Specified by:
decodeShort
in interfaceDecoder
- Returns:
- The short
- Throws:
IOException
- If an unrecoverable error occurs
-
decodeChar
Description copied from interface:Decoder
Decodes a char.- Specified by:
decodeChar
in interfaceDecoder
- Returns:
- The char
- Throws:
IOException
- If an unrecoverable error occurs
-
decodeInt
Description copied from interface:Decoder
Decodes a int.- Specified by:
decodeInt
in interfaceDecoder
- Returns:
- The int
- Throws:
IOException
- If an unrecoverable error occurs
-
decodeLong
Description copied from interface:Decoder
Decodes a long.- Specified by:
decodeLong
in interfaceDecoder
- Returns:
- The long
- Throws:
IOException
- If an unrecoverable error occurs
-
getLong
Decode the currentAbstractStreamDecoder.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
Decode the currentAbstractStreamDecoder.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
Decode the currentAbstractStreamDecoder.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
Decode the currentAbstractStreamDecoder.TokenType.NUMBER
value as aBigInteger
value. Called for no other token type.- Returns:
- The number value
- Throws:
IOException
- if an unrecoverable error occurs
-
getBigDecimal
Decode the currentAbstractStreamDecoder.TokenType.NUMBER
value as aBigDecimal
value. Called for no other token type.- Returns:
- The number value
- Throws:
IOException
- if an unrecoverable error occurs
-
getBestNumber
Decode the currentAbstractStreamDecoder.TokenType.NUMBER
value as aNumber
value. Called for no other token type.- Returns:
- The number value
- Throws:
IOException
- if an unrecoverable error occurs
-
getBigDecimalFromNumber
Converts the number, probably retrieved by callinggetBestNumber()
, to aBigDecimal
.- Parameters:
number
- The number value- Returns:
- The number as a big decimal
-
getBestNumberNode
Decode the currentAbstractStreamDecoder.TokenType.NUMBER
value as a numericJsonNode
. Called for no other token type. Default implementation tries to construct a node fromgetBestNumber()
.- Returns:
- The number value
- Throws:
IOException
- if an unrecoverable error occurs
-
decodeFloat
Description copied from interface:Decoder
Decodes a float.- Specified by:
decodeFloat
in interfaceDecoder
- Returns:
- The float
- Throws:
IOException
- If an unrecoverable error occurs
-
decodeDouble
Description copied from interface:Decoder
Decodes a double.- Specified by:
decodeDouble
in interfaceDecoder
- Returns:
- The double
- Throws:
IOException
- If an unrecoverable error occurs
-
decodeBigInteger
Description copied from interface:Decoder
Decodes a BigInteger.- Specified by:
decodeBigInteger
in interfaceDecoder
- Returns:
- The BigInteger, never
null
- Throws:
IOException
- If an unrecoverable error occurs
-
decodeBigDecimal
Description copied from interface:Decoder
Decodes a BigDecimal.- Specified by:
decodeBigDecimal
in interfaceDecoder
- 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 IOExceptionDecode a number type, applying all necessary coercions.- Type Parameters:
T
- The number type.- Parameters:
currentToken
- The current tokenfromNumberToken
- Called ifcurrentToken()
is aAbstractStreamDecoder.TokenType.NUMBER
.fromString
- Called for the textual value ifcurrentToken()
is aAbstractStreamDecoder.TokenType.STRING
. Should throwNumberFormatException
on parse failure.zero
- The zero value.one
- The one value.- Returns:
- The parsed number.
- Throws:
IOException
- if an unrecoverable error occurs
-
decodeBinary
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.
- Specified by:
decodeBinary
in interfaceDecoder
- 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. TheAbstractStreamDecoder
parameter to the function will just bethis
, but this allows subclasses to avoid capturingthis
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. TheAbstractStreamDecoder
parameter to the function will just bethis
, but this allows subclasses to avoid capturingthis
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
Description copied from interface:Decoder
Attempt to decode a null value. Returnsfalse
if this value is not null, and another method should be used for decoding. Returnstrue
if this value was null, and the cursor has been advanced to the next value.- Specified by:
decodeNull
in interfaceDecoder
- Returns:
- Returns
true
if the value wasnull
- Throws:
IOException
- If an unrecoverable error occurs
-
decodeBuffer
Description copied from interface:Decoder
Buffer the whole subtree of this value and return it as a newDecoder
. The returnedDecoder
can be used independently to thisDecoder
. This means actual parsing of the subtree can be delayed.The returned
Decoder
should behave identically to thisDecoder
. This means that for exampledecoder.decodeDouble()
should be equivalent todecoder.decodeBuffer().decodeDouble()
.- Specified by:
decodeBuffer
in interfaceDecoder
- Returns:
- An independent decoder that visits this subtree.
- Throws:
IOException
- If an unrecoverable error occurs
-
decodeNode
Description copied from interface:Decoder
Decodes the current state into aJsonNode
.- Specified by:
decodeNode
in interfaceDecoder
- Returns:
- The decoded node
- Throws:
IOException
- If an unrecoverable error occurs
-
decodeArbitrary
Description copied from interface:Decoder
Decodes the current state into an arbitrary object.The following should be decoded by this method:
- Specified by:
decodeArbitrary
in interfaceDecoder
- Returns:
- The decoded object
- Throws:
IOException
- If an unrecoverable error occurs
-
skipChildren
If we are at aAbstractStreamDecoder.TokenType.START_OBJECT
orAbstractStreamDecoder.TokenType.START_ARRAY
, skip to the matchingAbstractStreamDecoder.TokenType.END_OBJECT
orAbstractStreamDecoder.TokenType.END_ARRAY
. Else, do nothing.- Throws:
IOException
- if an unrecoverable error occurs
-
skipValue
Description copied from interface:Decoder
Skips the current value.- Specified by:
skipValue
in interfaceDecoder
- Throws:
IOException
- If an unrecoverable error occurs
-