Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
ObjectSerializer: Respect sequential decoding (#2273)
This was broken in #1916 Since ObjectSerializer calls decodeElementIndex() now, it should also call decodeSequentially(). Co-authored-by: Pavel Vasin <git@vasin.nl>
- Loading branch information
1 parent
0a6eb81
commit 8007574
Showing
3 changed files
with
77 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
56 changes: 56 additions & 0 deletions
56
core/commonTest/src/kotlinx/serialization/internal/DummySequentialDecoder.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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 <T : Any?> decodeSerializableElement(descriptor: SerialDescriptor, index: Int, deserializer: DeserializationStrategy<T>, previousValue: T?): T = notImplemented() | ||
override fun <T : Any> decodeNullableSerializableElement(descriptor: SerialDescriptor, index: Int, deserializer: DeserializationStrategy<T?>, previousValue: T?): T? = notImplemented() | ||
} |
18 changes: 18 additions & 0 deletions
18
core/commonTest/src/kotlinx/serialization/internal/ObjectSerializerTest.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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 | ||
} |