jean
05/31/2022, 2:02 PMPolymorphic serializer was not found for class discriminator 'batteryPercentage', JSON input: {"name":"batteryPercentage","value":70}
@JsonClassDiscriminator("name")
interface DeviceAttribute
@Serializable
data class Device(
val id: String,
val name: String,
val icon: String,
val iconOptions: List<String>,
val attributes: List<DeviceAttribute> = emptyList(),
val profiles: List<ActiveProfileMode>?,
)
@Serializable
@SerialName("batteryPercentage")
data class BatteryPercentage(val value: Int) : DeviceAttribute
What am I missing here?Emil Kantis
05/31/2022, 2:13 PMrocketraman
05/31/2022, 2:14 PMSerializersModule
. See https://github.com/Kotlin/kotlinx.serialization/blob/master/docs/polymorphism.md#open-polymorphism.
Alternatively, you could use a sealed class
as your base instead, see https://github.com/Kotlin/kotlinx.serialization/blob/master/docs/polymorphism.md#sealed-classes. sealed interface
support is a bit flaky right now.jean
06/01/2022, 6:19 AMSerializersModule
was only necessary if one needs a default serialization behavior. Thanks for the help!ephemient
06/01/2022, 9:23 PM@Serializable
sealed interface DeviceAttribute
should work in the Kotlin 1.6.20+, I see https://github.com/Kotlin/kotlinx.serialization/issues/1869 is still an issue but if you're using it as a property in another serializable class it should be fine