2

When my endpoint's JSON response's "data" element returns results, I am successfully parsing said results out into a Map<String, MyCustomDataType>.

But when my endpoint's JSON response's "data" element returns empty e.g. "data": [], my deserializing throws an error of "Invalid JSON at linenumber: Expected '{, kind: kotlinx.serialization.StructureKind$Map@6f12df1'"

In different endpoints where I use a List to deserialize the data element I could handle this empty data array result without an error by doing

@SerialName("data")
List<DataItem> = listOf(),

How do I handle this similarly using a map now? This is what I am currently unsuccessfully trying:

@Serializable
data class MyDataClass(
        @SerialName("success")
        var success: Boolean = false,
        @SerialName("data")
        var `data`: Map<String, MyCustomDataType> = emptyMap(),
        @SerialName("statusCode")
        var statusCode: Int = 0
) {
    @Serializable
    data class MyCustomDataType(
            @SerialName("updated")
            var updated: List<Updated> = listOf(),
            @SerialName("deleted")
            var deleted: List<Deleted> = listOf()
    ) {
        @Serializable
        data class Updated(
                @SerialName("myid")
                var uid: Int? = 0
        )

        @Serializable
        data class Deleted(
                @SerialName("myId")
                var uid: Int? = 0
        )
    }
}

Sample JSON Response when it parses correctly:

"myDataClass": {
  "success": true,
  "data": {
    "39": {
      "updated": [
        {
          "myid": 1794
        }
      ]
    },
    "75": {
      "updated": [
        {
          "myid": 1794
        }
      ]
    }
  },
  "statusCode": 200
}

Sample JSON when it throws an error:

{
  "myDataClass":{
    "success":true,
    "data":[],
    "statusCode":200
  }
}
ordonezalex
  • 2,645
  • 1
  • 20
  • 33
chris_dotnet
  • 844
  • 9
  • 14
  • can you add a sample JSON response? – Jegan Babu Jul 22 '19 at 16:25
  • Sure thing, I added a sample of a response when the JSON is parsing correctly, and the an example response when it crashes. – chris_dotnet Jul 22 '19 at 17:09
  • Did you write the endpoint producing the JSON? It's not consistent that `data` is changing from an array to a map depending on if there is a result. Ideally, `data` will be one or the other regardless of success. – ordonezalex Jul 22 '19 at 19:43
  • @ordonezalex Unfortunately I did not write the endpoint so that is out of my control. Yes I agree that it's not ideal and probably shouldn't be that way, but I don't think I'll be able to get it changed. – chris_dotnet Jul 22 '19 at 19:51
  • 1
    @chris_dotnet that's a tough place to be. You might want to try using polymorphism. I know you're not using Klaxon, but it [has a good example on this](https://github.com/cbeust/klaxon#polymorphic-fields) that you might be able to adapt to use with kotlinx.serialization. – ordonezalex Jul 22 '19 at 20:01
  • 1
    I explained how to handle json having different types (in your case JSONObject and JSONArray) here https://stackoverflow.com/a/56897476/5584709. – Jegan Babu Jul 23 '19 at 04:14

0 Answers0