Issue with polymorphism and KMM: Setup is ```interface IFoo @Serializable class FooA: IFoo @Seri...
n
Issue with polymorphism and KMM: Setup is
Copy code
interface IFoo 

@Serializable
class FooA: IFoo

@Serializable
class FooB: IFoo
Copy code
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())
    }
}
Copy code
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 ?
Update: I solved this 2 ways. 1. You provide an explicit serializer. Instead of putting
myJson.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/1000https://github.com/Kotlin/kotlinx.serialization/issues/1077https://youtrack.jetbrains.com/issue/KT-41339