【发布时间】:2021-05-20 10:17:09
【问题描述】:
我正在尝试为 ktor 中的 Websocket 反序列化/序列化 JSON Payload。
目前我正在做类似的事情
// Global property
lateinit var globalGson: Gson
// fun Application.module()
install(ContentNegotiation) {
gson {
setDateFormat(DateFormat.LONG)
setPrettyPrinting()
globalGson = create() // init global property
}
}
在处理 websocket 时:
session.incoming.consumeEach { frame ->
if (frame is Frame.Text)
globalGson.fromJson(frame.readText(), MyClass::class.java) // use global property
}
是否有推荐的方式将 Websocket 功能与 GSON 功能结合使用?
是否有任何优雅的方法可以访问特定类型的已注册 ContentNeogation 并在没有 ApplicationPipeline 的情况下使用它? (我相信 WebSocketSession 没有)
【问题讨论】:
标签: json serialization websocket gson ktor