I have defined the following classes : ```interface DeviceAttribute<T> { val name: String ...
j
I have defined the following classes :
Copy code
interface DeviceAttribute<T> {
    val name: String
    val value: T
}

@SharedImmutable
internal val module = SerializersModule {
    polymorphic(baseClass = DeviceAttribute::class, baseSerializer = null) {
        subclass(AlarmEntryDevice::class)
        default { Unknown.serializer() }
    }
}

@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>?,
)
But I get this error
Serializer has not been found for type 'Any?'. To use context serializer as fallback, explicitly annotate type or property with @Contextual
Adding the annotation doesn’t change anything though. What should I do?
d
You should remove the generics from your interface since you don't seem to need it.
j
it’s used in other parts of my app, I just wrote the relevant classes to the issue here
d
Well, you can't quite mix generics and polymorphism like you've done there.
How many possible values of
T
are there? Maybe you can hard code them? Like
StringDeviceAttribute
j
It could be any primitive type but I can do without it, it would have just been a bit nicer code. thanks for the help 🙂
d
It's just not possible for
kotlinx.serialization
to know which value of
T
to use when deserialising.
Np
j
I understand that, I though maybe I needed to have some more configuration in the
SerializersModule
to fix it
e
https://github.com/Kotlin/kotlinx.serialization/issues/607 kotlinx.serialization doesn't handle this well currently