klaxon icon indicating copy to clipboard operation
klaxon copied to clipboard

Unable to instantiate MyClass

Open azeroht opened this issue 2 years ago • 0 comments

Hi,

I'm having this error message com.beust.klaxon.KlaxonException: Unable to instantiate Pdv (class def below) while I try to serialize a string (JSON format).

Here is a part of the stringified JSON I am working on:

{
  "pdv": [
    {
      "pop": "R",
      "ville": "Camphin-en-Pévèle",
      "prix": [
        {
          "valeur": "2.208",
          "maj": "2022-03-07 16:29:16",
          "id": "1",
          "nom": "Gazole"
        },
        {
          "valeur": "1.123",
          "maj": "2022-03-07 16:29:17",
          "id": "3",
          "nom": "E85"
        },
        {
          "valeur": "2.105",
          "maj": "2022-03-07 16:29:17",
          "id": "5",
          "nom": "E10"
        },
        {
          "valeur": "2.174",
          "maj": "2022-03-07 16:29:17",
          "id": "6",
          "nom": "SP98"
        }
      ],
      "latitude": "5059477.455",
      "adresse": "RD 93 GRANDE RUE",
      "id": "59780003",
      "services": {
        "service": [
          "Station de gonflage",
          "Laverie",
          "Lavage automatique",
          "Automate CB 24/24"
        ]
      },
      "cp": "59780",
      "longitude": "325781.84717474"
    }
  ]
}

Here is my entity:

import com.beust.klaxon.Json

class PdvListeEntity {
    data class Prix(
        val valeur: String? = null,
        val maj: String? = null,
        val id: String? = null,
        val nom: String? = null,
    )

    data class Service(
        val service: List<String>? = listOf(),
    )

    data class Pdv(
        val id: String? = null,
        val pop: String? = null,
        val adresse: String? = null,
        val ville: String? = null,
        val cp: String? = null,
        val latitude: String? = null,
        val longitude: String? = null,
        @Json(name = "services", ignored = true)
        val listServices: List<Service>? = listOf(),
        @Json(name = "prix")
        val listPrix: List<Prix>? = listOf(),
    )

    data class PdvListe(
        @Json(name = "pdv")
        val listPdv: List<Pdv>? = listOf(),
    )
}

And here is how I parse

Klaxon().parse<PdvListeEntity.PdvListe>(theUpperJson)

I found similar issues but couldn't find a way to resolve it. I've noticed that when I ignore listPrix, it works fine.

I am using klaxon 5.5 and kotlin 1.6.10

Thanks in advance,

azeroht avatar Mar 15 '22 01:03 azeroht