Is there a way to distinguish between Successful
and Failed
responses based on the value of ok
field in a response JSON?
@Serializable
sealed class Response {
@Serializable
data class Successful(
@SerialName("ok")
val ok: Boolean,
@SerialName("payload")
val payload: Payload
) : Response()
@Serializable
data class Failed(
@SerialName("ok")
val ok: Boolean,
@SerialName("description")
val description: String
) : Response()
}
So, for {"ok":true, "payload":…}
I want to get Successful
class, and for {"ok":false, "description":…}
— Failed
.
I know that there is similar question — Deserializing into sealed subclass based on value of field — but it uses type
field, and I don't have any type discriminators in the JSON (the meaning of ok
is not type discrimination (though it can be used that way with some hacks, probably))