我正在尝试制作一个简单的服务器,它提供 JSON 中的序列化列表。待序列化的List是官方博文多态序列化部分中的示例。
但是使用 ktor 的序列化功能,我得到以下异常。
21:53:25.536 [nioEventLoopGroup-4-1] ERROR ktor.application - Unhandled: GET - /
java.lang.IllegalStateException: Serializing collections of different element types is not yet supported. Selected serializers: [DirectMessage, BroadcastMessage]
at io.ktor.serialization.SerializerLookupKt.elementSerializer(SerializerLookup.kt:71)
Run Code Online (Sandbox Code Playgroud)
由于密封类是选择 Kotlin 的一个关键特性,我真的很想知道为什么不支持它。
ktor-serialization 不支持这一点有什么充分的理由吗?或者我应该发布一个问题以从SerializerLookup.kt中删除此检查?
我通过在 IntelliJ 中选择“新建项目”>“Kotlin”>“应用程序”来编写此代码。修改后的代码如下所示。
我的服务器.kt:
21:53:25.536 [nioEventLoopGroup-4-1] ERROR ktor.application - Unhandled: GET - /
java.lang.IllegalStateException: Serializing collections of different element types is not yet supported. Selected serializers: [DirectMessage, BroadcastMessage]
at io.ktor.serialization.SerializerLookupKt.elementSerializer(SerializerLookup.kt:71)
Run Code Online (Sandbox Code Playgroud)
我的 build.gradle.kts:
import io.ktor.application.*
import io.ktor.features.*
import io.ktor.response.*
import …Run Code Online (Sandbox Code Playgroud)