Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Unable to instantiate MyClass #355

Open
azeroht opened this issue Mar 15, 2022 · 0 comments
Open

Unable to instantiate MyClass #355

azeroht opened this issue Mar 15, 2022 · 0 comments

Comments

@azeroht
Copy link

azeroht commented Mar 15, 2022

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,

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant