使用 KClass 引用作为具体化参数从 JSON 反序列化

问题描述

我正在尝试实现一个通用的序列化框架,以使用 kotlinx 序列化将传出和传入的消息转换为 json。我正在开发一个多平台应用程序,所以我试图让它在 KotlinJVM 和 KotlinJS 上运行。

为此,我向每条消息添加一个 type 字段,并使用将每个 type 字符串映射到 KClass 的映射。那张地图的类型是什么?它包含 KClass<> 对象,其类扩展 Message 类,因此在 java 中我将我的地图指定为 Map<KClass<? extends Message>,String>

我如何在 Kotlin 中做到这一点?

之后我需要根据消息的键和类型对消息进行序列化和反序列化。 Java 框架采用 Class 参数作为我想要反序列化/实例化的对象类型(例如 gson.fromJson(ClientMessage.class))。在 Kotlin 中,这是使用 reified 参数 Json.decodeFromString<Type> 完成的。我在编译时不知道消息的类型,只有对 KClass 的引用,我如何基于它实例化一个对象?

@Serializable
open class Message(val type: String) {

    companion object {
        val messageTypes: Map<KClass<out Message>,String> = mapOf(
            ClientLoginMessage::class to "clientLoginMessage",Message::class to "message"
        )

        inline fun <reified T> getMessageTypeByClass(): String = messageTypes[T::class]!! // utility for defining the type in the constructors of the individual messages
    }

    fun toJson() = Json.encodeToString(this)

    fun fromJson(json: String): Message? {
        val plainMessage = Json.decodeFromString<Message>(json) // get type string from json
        return messageTypes.entries.find { it.value == plainMessage.type }?.let {
            // how can I use the KClass from it.key as reified parameter?
            Json.decodeFromString<?????>(json) 
        }
    }
}

@Serializable
class ClientLoginMessage
         : Message(Message.getMessageTypeByClass<ClientLoginMessage>()) {}

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)