Kotlinx 序列化中的映射和变量键名

Maps and variable key names in Kotlinx-Serialization

meta 很简单,但是如何为 Kotlinx 序列化建模 analysis

{
  "meta": {
    "subject": "33306",
    "interval": "weekly"
  },
  "analysis": {
    "2021-07-20": {
      "dose": "0.6410"
    },
    "2021-07-16": {
      "dose": "0.9570"
    },
    "2021-07-09": {
      "dose": "0.6880"
    }
  }
}

我现在有这个。

@Serializable
class Observation(
    @SerialName("meta")
    val meta: Meta,

    @SerialName("analysis")
    val analysis: Map<String, Map<String, String>>
)

但是我得到了错误。

kotlinx.serialization.json.internal.JsonDecodingException: Polymorphic serializer was not found for missing class discriminator ('null')

解决方案是在 Retrofit 中将其建模为 suspend 方法,并将其 return 类型包装在 Response.

@GET("/observation")
suspend fun getObservation(
    @Query("subject")
    subject: String
): Response<Observation>