I encountered a problem when receiving data from the backend that the resulting field (for example, "somethingField") could be either a String or an Object with two Int lists. How can you parse such data correctly? I'm using kotlinx.serialization .
@Serializable
data class SomeObject(
...
val somethingField: String or SometingObject
...
)
@Serializable
data class SomethingObject(
val list1: List<Int>,
val list2: List<Int>,
)
You can achieve this by using sealed classes. When the real type is one of the few available types, sealed classes are helpful in modeling constrained class hierarchies.
You can then use it in your fragment or activity like this
This way, you can handle different cases based on the actual type of "somethingField".