Nathan
10/03/2022, 8:03 PMinterface IFoo
@Serializable
class FooA: IFoo
@Serializable
class FooB: IFoo
val myJson = Json {
ignoreUnknownKeys = true
isLenient = true
explicitNulls = false
encodeDefaults = false
serializersModule = SerializersModule {
polymorphic(IFoo::class, FooA::class, FooA.serializer())
polymorphic(IFoo::class, FooB::class, FooB.serializer())
}
}
val listOfFoo = myJson.decodeFromString<<List<IFoo>>(...)
The above ^ works fine in the Android code, but errors out when called from iOS:
`kotlinx.serialization.SerializationException: Serializer for class ‘IFoo’ is not found.
Mark the class as @Serializable or provide the serializer explicitly.
On Kotlin/Native explicitly declared serializer should be used for interfaces and enums without @Serializable annotation`Is there a step that I am missing with setting up the SerializersModule to make it compatible with iOS? Again, this works perfectly fine from the Android Versions: • Kotlin: 1.7.10 • Kotlin serialization: 1.4.0 ◦ org.jetbrains.kotlinx:kotlinx-serialization-core ◦ org.jetbrains.kotlinx:kotlinx-serialization-json EDIT: https://kotlinlang.slack.com/archives/C7A1U5PTM/p1601187298013400 I found this older post with someone asking the same question and they seemed to solve it by downgrading ?
Nathan
10/14/2022, 7:24 PMmyJson.decodeFromString(jsonString)
you have to do myJson.decodeFromString(ListSerializer(PolymorphicSerializer(IFoo::class)), jsonString)
2. You make the interface an abstract class
Resources that led me here:
• https://github.com/Kotlin/kotlinx.serialization/issues/1000
• https://github.com/Kotlin/kotlinx.serialization/issues/1077
• https://youtrack.jetbrains.com/issue/KT-41339