Is it possible to deserialize a JSON structure so that portions of that structure are collected into a nested child object?
So given this JSON structure
{
"root_field1": "This field will be in root",
"root_field2": "This field will be in root",
"child_field1": "This field will be in a child object",
"child_field2": 123
}
Is it possible to use a JSONTransformSerializer
(or some other way) to deserialize the above json into:
@Serializable
data class Root(
@SerialName("root_field1")
val field1: String,
@SerialName("root_field2")
val field2: String,
val child: Child
)
@Serializable
data class Child(
@SerialName("child_field1")
val field1: String,
@SerialName("child_field2")
val field2: Int
)
I've attempted to use a JsonTransformingSerializer
on the Root
, however that simply causes an exception due to the child
element not being found.
I also attempted to set the child
to @Transient
in hopes that would allow me to circumvent the issue, however the JsonTransformingSerializer
still requires a KSerializer
for its underlying class as an input and so that did not work.