Kotlin - 无法序列化多态 类。找不到序列化程序
Kotlin - Cannot serialize polymorphic classes. No serializer found
我正在尝试使用 kotlinx.serialization
和 kotlin/native 序列化多态 类。我正在使用序列化指南中提供的示例:
val module = SerializersModule {
polymorphic(Project::class) {
subclass(OwnedProject::class)
}
}
val format = Json { serializersModule = module }
@Serializable
abstract class Project {
abstract val name: String
}
@Serializable
@SerialName("owned")
class OwnedProject(override val name: String, val owner: String) : Project()
fun main() {
val data: Project = OwnedProject("kotlinx.coroutines", "kotlin")
println(format.encodeToString(data))
}
此代码在 运行 与 JVM 一起工作时有效,但在 运行 与 kotlin native for linuxX64
编译和 运行 时抛出错误:
Uncaught Kotlin exception: kotlinx.serialization.SerializationException: Serializer for class 'Project' is not found. Mark the class as @Serializable or provide the serializer explicitly.
at kfun:kotlin.Throwable#<init>(kotlin.String?){} (0x294767)
at kfun:kotlin.Exception#<init>(kotlin.String?){} (0x28ea25)
at kfun:kotlin.RuntimeException#<init>(kotlin.String?){} (0x28e725)
at kfun:kotlin.IllegalArgumentException#<init>(kotlin.String?){} (0x28e925)
at kfun:kotlinx.serialization.SerializationException#<init>(kotlin.String?){} (0x350185)
at kfun:kotlinx.serialization.internal#serializerNotRegistered@kotlin.reflect.KClass<*>(){}kotlin.Nothing (0x36fa0d)
at kfun:kotlinx.serialization#serializer@kotlin.reflect.KClass<0:0>(){0§<kotlin.Any>}kotlinx.serialization.KSerializer<0:0> (0x3505c8)
at kfun:kotlinx.serialization.serializer$serializerByKTypeImpl#internal (0x3512d2)
at kfun:kotlinx.serialization#serializer(kotlin.reflect.KType){}kotlinx.serialization.KSerializer<kotlin.Any?> (0x3503c8)
...
我是不是理解错了,代码应该在两个平台上都能工作?
如何让它在本机上运行?
我从 github issues 中了解到它的当前限制。
It is a current limitation that should be properly documented (cc @qwwdfsad ). Please try format.encodeToString(PolymorphicSerializer(Project::class), data). In case of sealed classes, it should work. If it doesnt, simply use format.encodeToString(Project.serializer(), data)
是的,这是当前的限制,您可以查看Polymorphism documentation here and you can track the issue #1077
您在定义为 SerializersModule 的子class 时错过了为 OwnedProject class 声明序列化程序。
val module = SerializersModule {
polymorphic(Project::class) {
subclass(OwnedProject::class, OwnedProject.serializer()) // add here OwnedProject class serializer
}
}
我正在尝试使用 kotlinx.serialization
和 kotlin/native 序列化多态 类。我正在使用序列化指南中提供的示例:
val module = SerializersModule {
polymorphic(Project::class) {
subclass(OwnedProject::class)
}
}
val format = Json { serializersModule = module }
@Serializable
abstract class Project {
abstract val name: String
}
@Serializable
@SerialName("owned")
class OwnedProject(override val name: String, val owner: String) : Project()
fun main() {
val data: Project = OwnedProject("kotlinx.coroutines", "kotlin")
println(format.encodeToString(data))
}
此代码在 运行 与 JVM 一起工作时有效,但在 运行 与 kotlin native for linuxX64
编译和 运行 时抛出错误:
Uncaught Kotlin exception: kotlinx.serialization.SerializationException: Serializer for class 'Project' is not found. Mark the class as @Serializable or provide the serializer explicitly.
at kfun:kotlin.Throwable#<init>(kotlin.String?){} (0x294767)
at kfun:kotlin.Exception#<init>(kotlin.String?){} (0x28ea25)
at kfun:kotlin.RuntimeException#<init>(kotlin.String?){} (0x28e725)
at kfun:kotlin.IllegalArgumentException#<init>(kotlin.String?){} (0x28e925)
at kfun:kotlinx.serialization.SerializationException#<init>(kotlin.String?){} (0x350185)
at kfun:kotlinx.serialization.internal#serializerNotRegistered@kotlin.reflect.KClass<*>(){}kotlin.Nothing (0x36fa0d)
at kfun:kotlinx.serialization#serializer@kotlin.reflect.KClass<0:0>(){0§<kotlin.Any>}kotlinx.serialization.KSerializer<0:0> (0x3505c8)
at kfun:kotlinx.serialization.serializer$serializerByKTypeImpl#internal (0x3512d2)
at kfun:kotlinx.serialization#serializer(kotlin.reflect.KType){}kotlinx.serialization.KSerializer<kotlin.Any?> (0x3503c8)
...
我是不是理解错了,代码应该在两个平台上都能工作? 如何让它在本机上运行?
我从 github issues 中了解到它的当前限制。
It is a current limitation that should be properly documented (cc @qwwdfsad ). Please try format.encodeToString(PolymorphicSerializer(Project::class), data). In case of sealed classes, it should work. If it doesnt, simply use format.encodeToString(Project.serializer(), data)
是的,这是当前的限制,您可以查看Polymorphism documentation here and you can track the issue #1077
您在定义为 SerializersModule 的子class 时错过了为 OwnedProject class 声明序列化程序。
val module = SerializersModule {
polymorphic(Project::class) {
subclass(OwnedProject::class, OwnedProject.serializer()) // add here OwnedProject class serializer
}
}