Ivan Đorđević
02/12/2021, 12:42 PM@Flatten
annotation (or a custom serializer that works for any @Serializable
class), that would flatten nested classes into a flat JSON structure?
Example from stack overflow (https://stackoverflow.com/questions/64556699/serialize-kotlin-nested-classes-to-flat-json)
An Example:
@Serializable
data class Address(val street: String, val postalCode: String)
@Serializable
data class Customer(val id: String, val name: String, val address: Address)
The default behaviour upon serialization is:
{
"id": "123ABC",
"name": "Joe"
"address": {
"street": "my street",
"postalCode": "123456"
}
}
What i want is:
{
"id": "123ABC",
"name": "Joe"
"street": "my street",
"postalCode": "123456"
}
The accepted answer does work, but it requires basically a per-type custom implementation which is a lot of boilerplate. Is there a way to do this in a general manner, so that it works for any nested serializable class?