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

Missing support for JSON Map #339

Open
lobodpav opened this issue Jul 31, 2021 · 3 comments
Open

Missing support for JSON Map #339

lobodpav opened this issue Jul 31, 2021 · 3 comments

Comments

@lobodpav
Copy link

Hello,

Trying to deserialize a simple JSON map

{ 
    "1": { "name": "Pavel",  "age": 38 },
    "2": { "name": "Jessie", "age": 72 }
}

without a need to do any extra processing. Something like this:

klaxon.parse<Map<String, Person>>(jsonMap)

Here goes the test I wrote to demonstrate it is possible, but in a bit complicated way.
I might be getting it wrong and do something incorrectly. Would you advise, please?

package test

import com.beust.klaxon.JsonObject
import com.beust.klaxon.Klaxon

fun main() {
    testJsonMapWorking()
    testJsonMapFailing()
}

private fun testJsonMapFailing() {
    val klaxon = Klaxon()
    val people = klaxon.parse<Map<String, Person>>(jsonMap)

    assert(people?.size == 2)
    // fails on class com.beust.klaxon.JsonObject cannot be cast to class test.Person
    assert(people?.get("1") == Person("Pavel", 38))
}

private fun testJsonMapWorking() {
    val klaxon = Klaxon()
    val people = klaxon.parse<Map<String, JsonObject>>(jsonMap)
        ?.mapValues { klaxon.parseFromJsonObject<Person>(it.value) }

    assert(people?.size == 2)
    assert(people?.get("1") == Person("Pavel", 38))
}

private data class Person(
    val name: String,
    val age: Int?,
)

private val jsonMap = """
{ 
    "1": { "name": "Pavel",  "age": 38 },
    "2": { "name": "Jessie", "age": 72 }
}
""".trimIndent()
@AddictArts
Copy link

You should look at the test MapTest.kt at https://github.com/cbeust/klaxon/blob/master/klaxon/src/test/kotlin/com/beust/klaxon/MapTest.kt

Maybe you did, implies a HashMap should work. The below is a possible solution, not great, but works.

data class First(
    @Json(name = "1")
    val num: Person)
data class Second(
    @Json(name = "2")
    val num: Person)

val r2 = Klaxon().parse<First>(jsonMap)
println(r2?.num?.name)
println(r2?.num?.age)

// Pavel
// 38

@lobodpav
Copy link
Author

lobodpav commented Dec 2, 2021

Unfortunately, that way won't work in my case. The number of items in the JSON is dynamic and out of my control.

@AddictArts
Copy link

Unfortunately, that way won't work in my case. The number of items in the JSON is dynamic and out of my control.

Understood. I tried the HashMap as it is in the Test, but it didn't work for me, unsure of why. @cbeust any ideas?

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

2 participants