Ivan Colovic
11/09/2020, 10:32 AMclass TreeNode(
val value: MyData
) {
val children = listOf<TreeNode>()
}
I’m trying to have the value
field named dynamically based on its type (many classes inherit from MyData
). Something like:
{
"data_one":{
...
},
"children":[
{
"data_two":{
...
},
"children":[
{
"data_three":{
...
},
"children":[
]
},
{
"data_one":{
...
},
"children":[
]
}
]
}
]
}
How do I define the SerialDescriptor
element that represents the collection of its own type? Right now, I have:
override val descriptor: SerialDescriptor = buildClassSerialDescriptor("TreeNode") {
element<DataOne>("data_one")
element<DataTwo>("data_two")
element<DataThree>("data_three")
element("children", listSerialDescriptor<TreeNode>()) // this crashes because I'm trying to use the descriptor I'm currently creating
}
When I change the listSerialDescriptor<TreeNode>()
to for example listSerialDescriptor<String>()
it encodes it the way I want it to, but I'm getting errors when decoding.
How do I approach this? Still new to the library I hope I was clear enough 🙇♂️ Thanks!Nikky
11/09/2020, 3:17 PMclassDiscriminator
Nikky
11/09/2020, 3:29 PMNikky
11/09/2020, 4:09 PMNikky
11/09/2020, 4:20 PMIvan Colovic
11/09/2020, 8:45 PM