molikuner
12/23/2019, 2:25 PMclass InlineSerializerTest {
@Test
fun `simple TestClass serialization test`() {
val json = "\"test\""
val parsed = Json.parse(TestClass.Companion, json)
assertEquals(
TestClass("test"),
parsed
)
}
}
@Serializable(with = TestClass.Companion::class)
data class TestClass(val a: String) {
@Serializer(forClass = TestClass::class)
companion object : TestSerializer {
override val descriptor: SerialDescriptor = StringDescriptor
// when removing this function the serialization crashes.
override fun deserialize(decoder: Decoder): TestClass {
return TestClass(decoder.decodeString())
}
override fun serialize(encoder: Encoder, obj: TestClass) = throw UnsupportedOperationException()
}
}
interface TestSerializer: KSerializer<TestClass> {
override fun deserialize(decoder: Decoder): TestClass {
return TestClass(decoder.decodeString())
}
}
As you can see in the companion object, I redefined the deserialize function with the same body as the extending interface. When removing this implementation the deserialization crashes:
Invalid JSON at 0: Expected '{, kind: CLASS'
kotlinx.serialization.json.JsonDecodingException: Invalid JSON at 0: Expected '{, kind: CLASS'
at kotlinx.serialization.json.internal.JsonReader.fail(JsonReader.kt:293)
at kotlinx.serialization.json.internal.StreamingJsonInput.beginStructure(StreamingJsonInput.kt:39)
... // if needed I can provide the full stack later
Any idea how to resolve this? Whats really weird is that the error says that a class
kind descriptor would be used, but String is definetly not a class kind type.molikuner
12/23/2019, 3:09 PM@Serializable
annotation generates a default implementation for the deserialization function even though the super class / interface implements it. I will open an issue later and post the link here.molikuner
12/23/2019, 4:53 PM