You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
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?
packagetestimportcom.beust.klaxon.JsonObjectimportcom.beust.klaxon.Klaxonfunmain() {
testJsonMapWorking()
testJsonMapFailing()
}
privatefuntestJsonMapFailing() {
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.Personassert(people?.get("1") ==Person("Pavel", 38))
}
privatefuntestJsonMapWorking() {
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))
}
privatedata classPerson(
valname:String,
valage:Int?,
)
privateval jsonMap ="""{ "1": { "name": "Pavel", "age": 38 }, "2": { "name": "Jessie", "age": 72 }}""".trimIndent()
The text was updated successfully, but these errors were encountered:
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
Hello,
Trying to deserialize a simple JSON map
without a need to do any extra processing. Something like this:
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?
The text was updated successfully, but these errors were encountered: