starsep
10/28/2020, 12:06 PMFooResponse
is sealed class, I don't control response and therefore cannot add type discriminator as described in https://github.com/Kotlin/kotlinx.serialization/blob/master/docs/polymorphism.md#sealed-classes
At the moment I use this ugly code, is there a better way?
val jsonData = jsonSerializer.parseToJsonElement(responseData)
try {
Json.decodeFromJsonElement<FooResponse.SuccessData>(jsonData)
} catch (e: SerializationException) {
try {
Json.decodeFromJsonElement<FooResponse.AnotherCase>(jsonData)
} catch (e: SerializationException) {
Json.decodeFromJsonElement<FooResponse.ErrorData>(jsonData)
}
}
diesieben07
10/28/2020, 12:23 PMKSerializer
, which first deserializes to JsonElement
, then checks the structure and based on the structure decides which class to decode into.sandwwraith
10/28/2020, 12:40 PMedenman
10/28/2020, 6:23 PMoneof
values: https://github.com/edenman/kmpPlayground/blob/edenman/kupdates/shared/src/commonMain/kotlin/chat/quill/data/OneOfSerializer.kt#L30