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
Decoders.-
Nested Class Summary
Nested ClassesModifier and TypeClassDescriptionprotected static enumThe token type.static interfaceDecoder 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
ConstructorsConstructorDescriptionAbstractStreamDecoder(@NonNull LimitingStream.RemainingLimits remainingLimits) -
Method Summary
Modifier and TypeMethodDescriptionprotected abstract StringcoerceScalarToString(AbstractStreamDecoder.TokenType currentToken) Decode any non-null scalar value (number, string or boolean) to its string representation.protected voidconsumeLeftElements(AbstractStreamDecoder.TokenType currentToken) Consumes left elements.protected abstract @Nullable AbstractStreamDecoder.TokenTypefinal @Nullable ObjectDecodes the current state into an arbitrary object.final @NonNull DecoderdecodeArray(io.micronaut.core.type.Argument<?> type) Start decoding an array.protected AbstractStreamDecoderdecodeArray0(AbstractStreamDecoder.TokenType currentToken) Decodes the array.final @NonNull BigDecimalDecodes a BigDecimal.final @NonNull BigIntegerDecodes a BigInteger.byte @NonNull []Decode binary data from this stream.final booleanDecodes a boolean.Buffer the whole subtree of this value and return it as a newDecoder.final byteDecodes a byte.final charDecodes a char.protected final <T> TdecodeCustom(AbstractStreamDecoder.ValueDecoder<T> readFunction) Decode a custom type.protected final <T> TdecodeCustom(AbstractStreamDecoder.ValueDecoder<T> readFunction, boolean callNext) Decode a custom type.final doubleDecodes a double.final floatDecodes a float.final intDecodes a int.@Nullable StringDecodes a key, if there are no more keys to decode returnsnull.final longDecodes a long.@NonNull io.micronaut.json.tree.JsonNodeDecodes the current state into aJsonNode.final booleanAttempt to decode a null value.protected final <T> TdecodeNumber(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 DecoderdecodeObject(io.micronaut.core.type.Argument<?> type) Decodes an object.protected AbstractStreamDecoderdecodeObject0(AbstractStreamDecoder.TokenType currentToken) Decodes the object.final shortDecodes a short.@NonNull StringDecodes a string.voidfinishStructure(boolean consumeLeftElements) protected abstract NumberDecode the currentAbstractStreamDecoder.TokenType.NUMBERvalue as aNumbervalue.protected io.micronaut.json.tree.JsonNodeDecode the currentAbstractStreamDecoder.TokenType.NUMBERvalue as a numericJsonNode.protected abstract BigDecimalDecode the currentAbstractStreamDecoder.TokenType.NUMBERvalue as aBigDecimalvalue.protected BigDecimalgetBigDecimalFromNumber(Number number) Converts the number, probably retrieved by callinggetBestNumber(), to aBigDecimal.protected abstract BigIntegerDecode the currentAbstractStreamDecoder.TokenType.NUMBERvalue as aBigIntegervalue.protected abstract booleanDecode the currentAbstractStreamDecoder.TokenType.BOOLEANvalue.protected abstract StringGet the current object field name.protected abstract doubleDecode the currentAbstractStreamDecoder.TokenType.NUMBERvalue as a double value.protected intDecode the currentAbstractStreamDecoder.TokenType.NUMBERvalue as a long value.protected abstract longgetLong()Decode the currentAbstractStreamDecoder.TokenType.NUMBERvalue as a long value.protected abstract StringDecode the currentAbstractStreamDecoder.TokenType.STRINGvalue.booleanprotected abstract voidMove to the next token.protected voidpreDecodeValue(AbstractStreamDecoder.TokenType currentToken) Should be called before attempting to decode a value.protected abstract voidIf we are at aAbstractStreamDecoder.TokenType.START_OBJECTorAbstractStreamDecoder.TokenType.START_ARRAY, skip to the matchingAbstractStreamDecoder.TokenType.END_OBJECTorAbstractStreamDecoder.TokenType.END_ARRAY.final voidSkips the current value.protected IOExceptionMethods inherited from class io.micronaut.serde.LimitingStream
childLimits, decreaseDepth, increaseDepth, limitsFromConfiguration, ourLimitsMethods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, waitMethods 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.KEYand that there's no child decoder currently running.- Parameters:
currentToken- The current token
-
finishStructure
- Specified by:
finishStructurein 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:
hasNextArrayValuein interfaceDecoder- Returns:
- Returns
trueif 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:DecoderDecodes a key, if there are no more keys to decode returnsnull.- Specified by:
decodeKeyin interfaceDecoder- Returns:
- The key or
nullif 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:DecoderStart decoding an array.- Specified by:
decodeArrayin 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:DecoderDecodes an object.- Specified by:
decodeObjectin 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:DecoderDecodes a string.- Specified by:
decodeStringin interfaceDecoder- Returns:
- The string, never
null - Throws:
IOException- If an unrecoverable error occurs
-
getString
Decode the currentAbstractStreamDecoder.TokenType.STRINGvalue. Called for no other token type.- Returns:
- The String value
- Throws:
IOException- if an unrecoverable error occurs
-
getBoolean
Decode the currentAbstractStreamDecoder.TokenType.BOOLEANvalue. Called for no other token type.- Returns:
- The boolean value
- Throws:
IOException- if an unrecoverable error occurs
-
decodeBoolean
Description copied from interface:DecoderDecodes a boolean.- Specified by:
decodeBooleanin interfaceDecoder- Returns:
- The boolean
- Throws:
IOException- If an unrecoverable error occurs
-
decodeByte
Description copied from interface:DecoderDecodes a byte.- Specified by:
decodeBytein interfaceDecoder- Returns:
- The byte
- Throws:
IOException- If an unrecoverable error occurs
-
decodeShort
Description copied from interface:DecoderDecodes a short.- Specified by:
decodeShortin interfaceDecoder- Returns:
- The short
- Throws:
IOException- If an unrecoverable error occurs
-
decodeChar
Description copied from interface:DecoderDecodes a char.- Specified by:
decodeCharin interfaceDecoder- Returns:
- The char
- Throws:
IOException- If an unrecoverable error occurs
-
decodeInt
Description copied from interface:DecoderDecodes a int.- Specified by:
decodeIntin interfaceDecoder- Returns:
- The int
- Throws:
IOException- If an unrecoverable error occurs
-
decodeLong
Description copied from interface:DecoderDecodes a long.- Specified by:
decodeLongin interfaceDecoder- Returns:
- The long
- Throws:
IOException- If an unrecoverable error occurs
-
getLong
Decode the currentAbstractStreamDecoder.TokenType.NUMBERvalue 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.NUMBERvalue 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.NUMBERvalue 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.NUMBERvalue as aBigIntegervalue. Called for no other token type.- Returns:
- The number value
- Throws:
IOException- if an unrecoverable error occurs
-
getBigDecimal
Decode the currentAbstractStreamDecoder.TokenType.NUMBERvalue as aBigDecimalvalue. Called for no other token type.- Returns:
- The number value
- Throws:
IOException- if an unrecoverable error occurs
-
getBestNumber
Decode the currentAbstractStreamDecoder.TokenType.NUMBERvalue as aNumbervalue. 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.NUMBERvalue 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:DecoderDecodes a float.- Specified by:
decodeFloatin interfaceDecoder- Returns:
- The float
- Throws:
IOException- If an unrecoverable error occurs
-
decodeDouble
Description copied from interface:DecoderDecodes a double.- Specified by:
decodeDoublein interfaceDecoder- Returns:
- The double
- Throws:
IOException- If an unrecoverable error occurs
-
decodeBigInteger
Description copied from interface:DecoderDecodes a BigInteger.- Specified by:
decodeBigIntegerin interfaceDecoder- Returns:
- The BigInteger, never
null - Throws:
IOException- If an unrecoverable error occurs
-
decodeBigDecimal
Description copied from interface:DecoderDecodes a BigDecimal.- Specified by:
decodeBigDecimalin 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 throwNumberFormatExceptionon 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:DecoderDecode 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:
decodeBinaryin 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. TheAbstractStreamDecoderparameter to the function will just bethis, but this allows subclasses to avoid capturingthisto 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. 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:
IOException- if an unrecoverable error occurs
-
decodeNull
Description copied from interface:DecoderAttempt to decode a null value. Returnsfalseif this value is not null, and another method should be used for decoding. Returnstrueif this value was null, and the cursor has been advanced to the next value.- Specified by:
decodeNullin interfaceDecoder- Returns:
- Returns
trueif the value wasnull - Throws:
IOException- If an unrecoverable error occurs
-
decodeBuffer
Description copied from interface:DecoderBuffer the whole subtree of this value and return it as a newDecoder. The returnedDecodercan be used independently to thisDecoder. This means actual parsing of the subtree can be delayed.The returned
Decodershould behave identically to thisDecoder. This means that for exampledecoder.decodeDouble()should be equivalent todecoder.decodeBuffer().decodeDouble().- Specified by:
decodeBufferin interfaceDecoder- Returns:
- An independent decoder that visits this subtree.
- Throws:
IOException- If an unrecoverable error occurs
-
decodeNode
Description copied from interface:DecoderDecodes the current state into aJsonNode.- Specified by:
decodeNodein interfaceDecoder- Returns:
- The decoded node
- Throws:
IOException- If an unrecoverable error occurs
-
decodeArbitrary
Description copied from interface:DecoderDecodes the current state into an arbitrary object.The following should be decoded by this method:
- Specified by:
decodeArbitraryin interfaceDecoder- Returns:
- The decoded object
- Throws:
IOException- If an unrecoverable error occurs
-
skipChildren
If we are at aAbstractStreamDecoder.TokenType.START_OBJECTorAbstractStreamDecoder.TokenType.START_ARRAY, skip to the matchingAbstractStreamDecoder.TokenType.END_OBJECTorAbstractStreamDecoder.TokenType.END_ARRAY. Else, do nothing.- Throws:
IOException- if an unrecoverable error occurs
-
skipValue
Description copied from interface:DecoderSkips the current value.- Specified by:
skipValuein interfaceDecoder- Throws:
IOException- If an unrecoverable error occurs
-