json - 使用 kotlinx.serialization 在 ktor 服务器中接收 Map
问题描述
我有一个运行 kotlinx.serialization 的 Ktor 服务器,因为它是 json (de)serializer 我向我的 Ktor 发送了这样一条消息:
{
"Brick(partNumber=004229, partName=Sticker Sheet for Set 295-1, dataSource=Rebrickable, brickImage=null, categoryID=58)": 5
}
这是一对 Int 和这个类:
import kotlinx.serialization.Serializable
@Serializable
data class Brick(
val partNumber: String,
val partName: String,
val dataSource: String,
val brickImage: String?,
val categoryID: Int?
)
但我得到这个错误
kotlinx.serialization.json.JsonDecodingException: Invalid JSON at 0: Expected '[, kind: MAP'
at kotlinx.serialization.json.internal.JsonReader.fail(JsonReader.kt:293)
对我来说,这意味着 kotlinx.serialization 期望 map 类有不同的语法。这对我来说很奇怪。当我将类型更改为 List> 时,它会引发相同的异常,但使用 LIST 而不是 MAP。编辑:经过进一步检查,它期望在行的开头有一个 [ 而不是 { 。我的(部分)应用程序实现
fun Application.module(testing: Boolean = false) {
install(ContentNegotiation) { serialization() }
routing {
route("user") {
route("brick") {
post {
call.request.queryParameters["userName"]
?.let { userRepository.login(it) } // Someone else is still working login nvm this
?.let { user ->
val bricks = call.receive<Map<Brick, Int>>() // This throws an error
userRepository.addBricks(user, bricks)
call.respond(HttpStatusCode.OK)
}
?: call.respond(HttpStatusCode.Unauthorized)
}
}
}
}
}
发送类的android改造函数(使用GSON):
@POST("/user/brick")
suspend fun setBricksAmounts(
@Query("userName")
userName: String,
@Body
brickAmounts: Map<Brick, Int>
)
解决方案
我认为使用类作为键在 kotlinx 序列化中不起作用,看起来该类只是序列化为字符串以将其用作键
相反,您可以在Map<String, Int>
运行后接收它
bricks.mapKeys { (jsonString, number) ->
Json(JsonConfiguration.Stable).parse(Brick.Serializer, jsonString)
}
或等效的杰克逊代码,如果你想
推荐阅读
- python - 如何创建自定义用户权限?
- javascript - Cypress.io 测试中没有进行拖放
- c# - C#- 让孩子达到给定的深度
- ruby - Ruby 正则表达式仅清除某些点
- ffmpeg - 使用 ffmpeg 将 jpg 转换为 mov 使用 DNXHD 编解码器时出错
- javascript - 如果没有来自 document.getElementById .setAttribute href 的选择选项,则隐藏按钮
- php - 嵌套的 foreach 只遍历所有循环
- python-3.x - pandas:将行的零值设置为该行数据帧的最大值
- node.js - 与 puppeteer 并行抓取
- r - 自托管 r shiny 应用程序不会显示所有内容