kevin.cianfarini
12/18/2022, 7:50 PMsealed interface Entity {
val commonProperty: Int
class New(override val commonProperty: Int) : Entity
class Persisted(
val id: Long,
override val commonProperty: Int
) : Entity
}
Given the following json
[
{ "commonProperty": 0 },
{ "id": 1, "commonProperty": 0 }
]
I would like that to deserialize to the following in Kotlin
[New(commonProperty = 0), Persisted(id = 1, commonProperty = 0)]
kevin.cianfarini
12/18/2022, 7:51 PMclass Entity(val id: Long?, val commonProperty: Int)
but that would require either passing in null
explcitily in JSON, or disabling Json.explicitNulls
globally which I'd like to avoid.zt
12/18/2022, 8:36 PMid
Kotlinx serialization would then deserialize to that value if it's missing in the inputAdam S
12/19/2022, 11:33 AMJsonContentPolymorphicSerializer
that can check if a JSON property is present or missing, and then select the serializer you’d like to use
https://github.com/Kotlin/kotlinx.serialization/blob/v1.4.1/docs/json.md#content-based-polymorphic-deserializationkevin.cianfarini
12/19/2022, 3:37 PM