Using JSON, is there any way to deserialize only k...
# serialization
e
Using JSON, is there any way to deserialize only keys of an object into a list? i.e
Copy code
{
  "foo": { // irrelevant },
  "bar": { // irrelevant },
} -> // [ "foo", "bar ]
Currently I got a
Copy code
data class Wrapper(
  val entries: Map<String, Irrelevant>
)
But I would like to discard/ignore the values of the map
n
if you have it as a
JsonObject
yu can treat it as a map, so you can just use
.keys
so you do not have to declare what structure the irrelevent data is, it just has to be valid json overall
🙌 1
e
Nice, thanks!
e
A bit off topic - you don't convert DTO to domain objects? So just use
wrapper.keys
after.
For me it is more clear than writing a custom json serializer
e
It's for _de_serializing in this case. The API we call returns a json object where we only care about the keys.. I don't want to introduce any data bearer to handle the object values because then I need to make assumptions on how that data will look even though I don't care about it, and that might break deserialization at some point.
e
Just checked code that @Nikky shared and it is a same idea. You parse how it is and extract data you need later.
Sorry for jumping on thread and not checking all things
n
and that's why we use a JsonObject .. the only assumption we make is that it has keys, the shape of the values are JsonElement, so.. could be any valid structure inside and can be discarded
👍 1
e
Yeah, could be just
Map<String, Any>
Ah it is already the original solution
n
and that's effectively how JsonObject is defined internally
👍🏼 1
e
so you made an computed field from it (if I'm correct)
n
you could ofc do this more complicated and write a custom deserializer and throw on serialization if you wanna discard the values.. I find that to be too messy