I'm using the 1.0.0 version of kotlin serialization but I'm stuck when I try to deserialize a "flexible" array.
From the Backend API that I don't control I get back an JSON Array that holds different types of objects. How would you deserialize them using kotlin serialization?
Example
This is the API's response
[
{
"id": "test",
"person": "person",
"lastTime": "lastTime",
"expert": "pro"
},
{
"id": "test",
"person": "person",
"period": "period",
"value": 1
}
]
@Serializable
sealed class Base {
@SerialName("id")
abstract val id: String
@SerialName("person")
abstract val person: String
}
@Serializable
data class ObjectA (
@SerialName("id") override val id: String,
@SerialName("title") override val title: String,
@SerialName("lastTime") val lastTime: String,
@SerialName("expert") val expert: String
) : Base()
@Serializable
data class ObjectB (
@SerialName("id") override val id: String,
@SerialName("title") override val title: String,
@SerialName("period") val period: String,
@SerialName("value") val value: Int
) : Base()
Performing the following code result in an error
println(Json.decodeFromString<List<Base>>(json))
error Polymorphic serializer was not found for class discriminator