I have the following data classes to parse JSON. I can parse it easily with the decodeFromString
method. However, the Info
classes could contain the List<Int>
type from time to time along with the Int
type so that both are included in a single JSON. How can I handle this variation in serialization?
@Serializable
data class Node (@SerialName("nodeContent") val nodeContent: List<Info>)
@Serializable
data class Info (@SerialName("info") val info: Int)
p.s. The closest question to mine is this one: Kotlinx Serialization, avoid crashes on other datatype. I wonder if there are other ways?
EDIT: An example is given below.
"nodeContent": [
{
"info": {
"name": "1",
},
},
{
"info": [
{
"name": "1"
},
{
"name": "2"
},
],
},
{
"info": {
"name": "2",
},
}
]