Hello, I have a JSON where the keys are the ids. ...
# serialization
h
Hello, I have a JSON where the keys are the ids.
Copy code
{
  "0": {"name": "John"},
  "1": {"name": "Jane"},
  "2": {"name": "Pete"},
  ...
}
I'm super new to the Serialization library, and I can't find which will be the best approach to deserialize it and have the id as part of the object? Something like this:
Person (id: Int, name: String)
Thank you🙂
p
It would be a Map<String, PersonDto> where the PersonDto has only the name.
Then you map that to a List<Person>.
You can also go with a custom serializer but implementing it like this will be way simpler and easier to maintain while also providing encapsulation
1
h
Awesome. I will try it. Thank you @Paul Woitaschek 🙏
Can it be that this does not work because the array of objects is not an array but an object?
Copy code
{
  "0": {"name": "John"},
  "1": {"name": "Jane"},
  "2": {"name": "Pete"},
  ...
}
and not
Copy code
[
  "0": {"name": "John"},
  "1": {"name": "Jane"},
  "2": {"name": "Pete"},
  ...
]
I'm receiving this error:
Error kotlinx.serialization.json.internal.JsonDecodingException: Expected start of the array '[', but had 'EOF' instead
p
Use MapSerializer(String.serializer(), PersonDto.serializer))
🙏 1