6

I want to decode a json string containing a list of objects in a polymorphic class structure using kotlinx.serialization in a Kotlin Multiplatform project, but it works only on JVM, not on Native. Here is a minimum reproducible example:

@Serializable
abstract class Project {
    abstract val name: String
}

@Serializable
@SerialName("BasicProject")
data class BasicProject(override val name: String): Project()

@Serializable
@SerialName("OwnedProject")
data class OwnedProject(override val name: String, val owner: String) : Project()

fun main() {
    val data = Json.decodeFromString<List<Project>>("""
        [
            {"type":"BasicProject","name":"example"},
            {"type":"OwnedProject","name":"kotlinx.serialization","owner":"kotlin"} 
        ]
    """))
}  

This works on JVM but throws the following exception on Native:

kotlinx.serialization.SerializationException: Serializer for class ‘Project’ 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.message

This problem has been discussed before in the context of encoding and some workarounds have been suggested, e.g. here, but my problem is decoding. Is there a workaround, or do I simply have to implement my own json parser?

jerha202
  • 187
  • 1
  • 9
  • Actually, it's not working even in JVM without explicit specifing `serializersModule` for `Json`: `val module = SerializersModule { polymorphic(Project::class) { subclass(BasicProject::class); subclass(OwnedProject::class) } }; val data = Json { serializersModule = module }.decodeFromString>(...)` – Михаил Нафталь Mar 10 '21 at 10:22
  • Will it work in Kotlin/Native if you specify above-mentioned `serializersModule` and pass serializer explicitly: `object ListOfProjectSerializer : KSerializer> by ListSerializer(Project.serializer()); val data = Json { serializersModule = module }.decodeFromString(ListOfProjectSerializer, ...)` ? – Михаил Нафталь Mar 10 '21 at 10:26
  • Yes that works, thank you so much! If you post it as an answer I will mark it accepted. – jerha202 Mar 10 '21 at 10:56

1 Answers1

7

You need to explicitly pass respectful serializer and serializersModule:

object ListOfProjectSerializer : KSerializer<List<Project>> by ListSerializer(Project.serializer())

val module = SerializersModule {
    polymorphic(Project::class) {
        subclass(BasicProject::class)
        subclass(OwnedProject::class)
    }
}

fun main() {
    val data = Json { serializersModule = module }.decodeFromString(
        ListOfProjectSerializer,
        """
        [
            {"type":"BasicProject","name":"example"},
            {"type":"OwnedProject","name":"kotlinx.serialization","owner":"kotlin"} 
        ]
        """
    )
}
  • polymorphic() now requires three arguments, not two – Dzmitry Lazerka Jan 12 '22 at 21:35
  • 1
    @DzmitryLazerka This code is still valid with the current latest `kotlinx.serialization` (1.3.2). `polymorphic` method has an [overload, which is defined as an extension method](https://kotlin.github.io/kotlinx.serialization/kotlinx-serialization-core/kotlinx-serialization-core/kotlinx.serialization.modules/polymorphic.html) and used here. It has 3 arguments, but two of them have default values. – Михаил Нафталь Jan 13 '22 at 09:08
  • @МихаилНафталь the link does not work. I cant find extension method. EDIT: nvr mind, I had to add `import kotlinx.serialization.modules.*` – artman Jan 25 '22 at 13:47
  • Seems, they've revamped docs; current correct link: https://kotlin.github.io/kotlinx.serialization/kotlinx-serialization-core/kotlinx.serialization.modules/polymorphic.html – Михаил Нафталь Jan 25 '22 at 16:34