1

The meta is simple, but how do I model analysis for Kotlinx-Serialization?

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

I have this at the moment.

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

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

But I get the error.

kotlinx.serialization.json.internal.JsonDecodingException: Polymorphic serializer was not found for missing class discriminator ('null')
es0329
  • 1,366
  • 1
  • 18
  • 35
  • Serializer for `Map` is build-in in kotlinx.serialization library (see https://github.com/Kotlin/kotlinx.serialization/blob/master/docs/builtin-classes.md#maps), so the problem is unrelated to serialization of `Map`, it's caused by something else. Please, provide a [minimal reproducible example](https://stackoverflow.com/help/minimal-reproducible-example). – Михаил Нафталь Jul 21 '21 at 15:57

1 Answers1

0

The solution was to model it as a suspend method in Retrofit and wrap its return type in Response.

@GET("/observation")
suspend fun getObservation(
    @Query("subject")
    subject: String
): Response<Observation>
es0329
  • 1,366
  • 1
  • 18
  • 35