I have a field that I want to deserialize into an instance of a sealed subclass based on a value on that Json object.
[
{
"id": 1L,
"some_array": [],
"my_thing": {
"type": "sealed_subclass_one",
"other_thing": {
"thing": "thing is a string"
}
}
}, {
"id": 2L,
"some_array": [],
"my_thing": {
"type": "sealed_subclass_two",
"other_thing": {
"other_thing": "other_thing is a string too"
}
}
},
]
Response model:
@Serializable
data class MyResponse(
@SerialName("id")
val id: Long
@SerialName("some_array")
val someArray: Array<Something>
@SerialName("my_thing")
val myThing: MySealedClassResponse
)
MySealedClass
@Serializable
sealed class MySealedClassResponse : Parcelable {
@Serializable
@SerialName("type")
data class SealedSubclassOne(
val thing: String
) : MySealedClassResponse()
@Serializable
@SerialName("type")
data class SealedSubclassTwo(
val otherThing: String
) : MySealedClassResponse()
}
As it stands, I'm getting a serialization exception because the serializer doesn't know what to do :
kotlinx.serialization.SerializationException: sealed_subclass_one is not registered for polymorphic serialization in the scope of class com.myapp.MyResponse
Is there an easy way to register the values of type
so that deserialization can happen without a custom serializer?