How can I specify a class discriminator on an inte...
# serialization
k
How can I specify a class discriminator on an interface?
Copy code
@JsonClassDiscriminator("country")
sealed interface Market {

    @Serializable @SerialName("UK")
    object UnitedKingdom : Market

    @Serializable @SerialName("US")
    enum class UnitedStates : Market {
        @SerialName("TX") Texas,
    }
}
Here the discriminator doesn’t do anything because the interface isn’t serializable (I think). I don’t want to globally specify the discriminator because it’s only applicable to this hierarchy
Right now, deserializing this fails with the following error.
Copy code
Polymorphic serializer was not found for missing class discriminator ('null')
JSON input: {"country":"UK"}
If I specify the discriminator globally in my json configuration, it works fine
d
Sounds like a bug. What happens if you remove
sealed
?
e
https://github.com/Kotlin/kotlinx.serialization/issues/1576 polymorphic serialization of sealed interfaces should be coming in Kotlin 1.6.20
k
Yes I saw that as well. Does a class need to be marked as serializable to have a class descriminator?
@Dominaezzz removing sealed does not alleviate the problem