diff --git a/core/commonMain/src/kotlinx/serialization/encoding/AbstractDecoder.kt b/core/commonMain/src/kotlinx/serialization/encoding/AbstractDecoder.kt index fad7ef87cf..ffe6dd3768 100644 --- a/core/commonMain/src/kotlinx/serialization/encoding/AbstractDecoder.kt +++ b/core/commonMain/src/kotlinx/serialization/encoding/AbstractDecoder.kt @@ -74,8 +74,7 @@ public abstract class AbstractDecoder : Decoder, CompositeDecoder { index: Int, deserializer: DeserializationStrategy, previousValue: T? - ): T? { - val isNullabilitySupported = deserializer.descriptor.isNullable - return if (isNullabilitySupported || decodeNotNullMark()) decodeSerializableValue(deserializer, previousValue) else decodeNull() + ): T? = decodeIfNullable(deserializer) { + decodeSerializableValue(deserializer, previousValue) } } diff --git a/core/commonMain/src/kotlinx/serialization/encoding/Decoding.kt b/core/commonMain/src/kotlinx/serialization/encoding/Decoding.kt index f29c8057fa..dc4aa2ab9e 100644 --- a/core/commonMain/src/kotlinx/serialization/encoding/Decoding.kt +++ b/core/commonMain/src/kotlinx/serialization/encoding/Decoding.kt @@ -260,12 +260,17 @@ public interface Decoder { * Decodes the nullable value of type [T] by delegating the decoding process to the given [deserializer]. */ @ExperimentalSerializationApi - public fun decodeNullableSerializableValue(deserializer: DeserializationStrategy): T? { - val isNullabilitySupported = deserializer.descriptor.isNullable - return if (isNullabilitySupported || decodeNotNullMark()) decodeSerializableValue(deserializer) else decodeNull() + public fun decodeNullableSerializableValue(deserializer: DeserializationStrategy): T? = decodeIfNullable(deserializer) { + decodeSerializableValue(deserializer) } } +@OptIn(ExperimentalSerializationApi::class) +internal inline fun Decoder.decodeIfNullable(deserializer: DeserializationStrategy, block: () -> T?): T? { + val isNullabilitySupported = deserializer.descriptor.isNullable + return if (isNullabilitySupported || decodeNotNullMark()) block() else decodeNull() +} + /** * [CompositeDecoder] is a part of decoding process that is bound to a particular structured part of * the serialized form, described by the serial descriptor passed to [Decoder.beginStructure]. diff --git a/core/commonMain/src/kotlinx/serialization/internal/Tagged.kt b/core/commonMain/src/kotlinx/serialization/internal/Tagged.kt index f1a7285824..cf71388c3a 100644 --- a/core/commonMain/src/kotlinx/serialization/internal/Tagged.kt +++ b/core/commonMain/src/kotlinx/serialization/internal/Tagged.kt @@ -206,7 +206,6 @@ public abstract class TaggedDecoder : Decoder, CompositeDecoder { protected open fun decodeSerializableValue(deserializer: DeserializationStrategy, previousValue: T?): T = decodeSerializableValue(deserializer) - // ---- Implementation of low-level API ---- override fun decodeInline(descriptor: SerialDescriptor): Decoder = @@ -284,13 +283,11 @@ public abstract class TaggedDecoder : Decoder, CompositeDecoder { index: Int, deserializer: DeserializationStrategy, previousValue: T? - ): T? = - tagBlock(descriptor.getTag(index)) { - if (decodeNotNullMark() || deserializer.descriptor.isNullable) decodeSerializableValue( - deserializer, - previousValue - ) else decodeNull() + ): T? = tagBlock(descriptor.getTag(index)) { + decodeIfNullable(deserializer) { + decodeSerializableValue(deserializer, previousValue) } + } private fun tagBlock(tag: Tag, block: () -> E): E { pushTag(tag)