Kotlin/Native 中 kotlinx.serialization 的多态反序列化

Polymorphic deserialization with kotlinx.serialization in Kotlin/Native

我想在 Kotlin Multiplatform 项目中使用 kotlinx.serialization 解码包含多态 class 结构中对象列表的 json 字符串,但它仅适用于 JVM,不适用于在本地。这是一个最小的可重现示例:

@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"} 
        ]
    """))
}  

这在 JVM 上有效,但在 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

这个问题之前已经在 encoding 的上下文中讨论过,并且已经提出了一些解决方法,例如,但我的问题是解码。是否有解决方法,或者我只需要实现自己的 json 解析器?

您需要显式传递尊重的序列化程序和 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"} 
        ]
        """
    )
}