From 80075749139be1144a4cd5dd9d32cfbf261ee975 Mon Sep 17 00:00:00 2001 From: Pavel Vasin <73423761+pavel-vasin@users.noreply.github.com> Date: Tue, 2 May 2023 16:58:24 +0300 Subject: [PATCH] ObjectSerializer: Respect sequential decoding (#2273) This was broken in https://github.com/Kotlin/kotlinx.serialization/pull/1916 Since ObjectSerializer calls decodeElementIndex() now, it should also call decodeSequentially(). Co-authored-by: Pavel Vasin --- .../internal/ObjectSerializer.kt | 3 + .../internal/DummySequentialDecoder.kt | 56 +++++++++++++++++++ .../internal/ObjectSerializerTest.kt | 18 ++++++ 3 files changed, 77 insertions(+) create mode 100644 core/commonTest/src/kotlinx/serialization/internal/DummySequentialDecoder.kt create mode 100644 core/commonTest/src/kotlinx/serialization/internal/ObjectSerializerTest.kt diff --git a/core/commonMain/src/kotlinx/serialization/internal/ObjectSerializer.kt b/core/commonMain/src/kotlinx/serialization/internal/ObjectSerializer.kt index ebb9e2e43..ac9ee8e3a 100644 --- a/core/commonMain/src/kotlinx/serialization/internal/ObjectSerializer.kt +++ b/core/commonMain/src/kotlinx/serialization/internal/ObjectSerializer.kt @@ -41,6 +41,9 @@ internal class ObjectSerializer(serialName: String, private val objectI override fun deserialize(decoder: Decoder): T { decoder.decodeStructure(descriptor) { + if (decodeSequentially()) + return@decodeStructure + when (val index = decodeElementIndex(descriptor)) { CompositeDecoder.DECODE_DONE -> { return@decodeStructure diff --git a/core/commonTest/src/kotlinx/serialization/internal/DummySequentialDecoder.kt b/core/commonTest/src/kotlinx/serialization/internal/DummySequentialDecoder.kt new file mode 100644 index 000000000..caeecdef7 --- /dev/null +++ b/core/commonTest/src/kotlinx/serialization/internal/DummySequentialDecoder.kt @@ -0,0 +1,56 @@ +/* + * Copyright 2023 JetBrains s.r.o. Use of this source code is governed by the Apache 2.0 license. + */ + +package kotlinx.serialization.internal + +import kotlinx.serialization.* +import kotlinx.serialization.descriptors.* +import kotlinx.serialization.encoding.* +import kotlinx.serialization.modules.* + +/** + * The purpose of this decoder is to check whether its methods were called currectly, + * rather than implement any concrete format. + */ +class DummySequentialDecoder( + override val serializersModule: SerializersModule = EmptySerializersModule() +) : Decoder, CompositeDecoder { + private fun notImplemented(): Nothing = throw Error("Implement this method if needed") + + override fun decodeSequentially(): Boolean = true + override fun decodeElementIndex(descriptor: SerialDescriptor): Int = throw Error("This method shouldn't be called in sequential mode") + + override fun beginStructure(descriptor: SerialDescriptor): CompositeDecoder = this + override fun endStructure(descriptor: SerialDescriptor): Unit = Unit + + override fun decodeInline(descriptor: SerialDescriptor): Decoder = notImplemented() + + override fun decodeBoolean(): Boolean = notImplemented() + override fun decodeByte(): Byte = notImplemented() + override fun decodeShort(): Short = notImplemented() + override fun decodeInt(): Int = notImplemented() + override fun decodeLong(): Long = notImplemented() + override fun decodeFloat(): Float = notImplemented() + override fun decodeDouble(): Double = notImplemented() + override fun decodeChar(): Char = notImplemented() + override fun decodeString(): String = notImplemented() + override fun decodeEnum(enumDescriptor: SerialDescriptor): Int = notImplemented() + + override fun decodeNotNullMark(): Boolean = notImplemented() + override fun decodeNull(): Nothing? = notImplemented() + + override fun decodeBooleanElement(descriptor: SerialDescriptor, index: Int): Boolean = notImplemented() + override fun decodeByteElement(descriptor: SerialDescriptor, index: Int): Byte = notImplemented() + override fun decodeShortElement(descriptor: SerialDescriptor, index: Int): Short = notImplemented() + override fun decodeIntElement(descriptor: SerialDescriptor, index: Int): Int = notImplemented() + override fun decodeLongElement(descriptor: SerialDescriptor, index: Int): Long = notImplemented() + override fun decodeFloatElement(descriptor: SerialDescriptor, index: Int): Float = notImplemented() + override fun decodeDoubleElement(descriptor: SerialDescriptor, index: Int): Double = notImplemented() + override fun decodeCharElement(descriptor: SerialDescriptor, index: Int): Char = notImplemented() + override fun decodeStringElement(descriptor: SerialDescriptor, index: Int): String = notImplemented() + + override fun decodeInlineElement(descriptor: SerialDescriptor, index: Int): Decoder = notImplemented() + override fun decodeSerializableElement(descriptor: SerialDescriptor, index: Int, deserializer: DeserializationStrategy, previousValue: T?): T = notImplemented() + override fun decodeNullableSerializableElement(descriptor: SerialDescriptor, index: Int, deserializer: DeserializationStrategy, previousValue: T?): T? = notImplemented() +} diff --git a/core/commonTest/src/kotlinx/serialization/internal/ObjectSerializerTest.kt b/core/commonTest/src/kotlinx/serialization/internal/ObjectSerializerTest.kt new file mode 100644 index 000000000..2234b8f59 --- /dev/null +++ b/core/commonTest/src/kotlinx/serialization/internal/ObjectSerializerTest.kt @@ -0,0 +1,18 @@ +/* + * Copyright 2023 JetBrains s.r.o. Use of this source code is governed by the Apache 2.0 license. + */ + +package kotlinx.serialization.internal + +import kotlin.test.* +import kotlinx.serialization.* + +class ObjectSerializerTest { + @Test + fun testSequentialDecoding() { + SimpleObject.serializer().deserialize(DummySequentialDecoder()) + } + + @Serializable + object SimpleObject +}