<@U6647LJ30> not sure if I’m running into a compil...
# serialization
e
@sandwwraith not sure if I’m running into a compiler bug or its desired behavior but I have a somewhat complex heirarchy I’m trying to serialize with an interface at its apex. Let me see if I can do this justice but my graph looks like:
Copy code
interface Field<Value> {...}

interface InputField<InputT> : Field<InputT> {...}
interface SelectorField<ValueT> : Field<ValueT> {...}

abstract class NumberField : InputField<Number> {...}
abstract class BooleanField : SelectorField<Boolean> {...}

@Serializable
data class NumberFieldImpl(...): NumberField()

@Serializable
data class BooleanFieldImpl(...): BooleanField()
Also I have a custom _map_:
Copy code
// Need to add serializable on base class else compiler complains
@Serializable
abstract class MapBackedContainer<K, V>(val storage: MutableMap<K, V>) : MutableMap<K, V> by storage

@Serializable // Currently fails compile, message below
class FieldContainer : MapBackedContainer<String, Field<*>>

@Serializable // This one has no errors, ActionImpl is non generic and annotated with @Serializable
class ActionContainer : MapBackedContainer<String, ActionImpl>
FieldContainer
currently fails compile with the message “Serializer for element of type Any? has not been found. To use context serializer as fallback, explicitly annotate element with @Contextual”. I tried adding
@Serializable
on the intermediate abstract classes of
Field<*>
but the issue is the same. Same with adding
@Contextual
on the Field<*> generic parameter of FieldContainer.
d
Polymorphism and generics don't work automatically. You'll have to write the serializers yourself.
Field<*>
hasn't been marked as
@Serializable
too.
e
It’s an interface, the docs say that there is no need to mark it as serializable 🤔
d
True but
*
is not serializable. That's what is refered to as
Any?
s
Indeed, since
Field<Value>
does not specify upper bound,
Field<*>
is a
Field<Any?>
for serialization framework. You may try
Field<@Polymorphic Any>
instead.
e
Is this in the Field interface definition or at the use-site (in the generic type parameter of FieldContainer)?
s
on a use-site
e
Ignore this for now, I dont think
Country
is Serializable.
Thanks for the help @sandwwraith @Dominaezzz 🙏
s
Change
override var name: String = id.capitalize(Locale.ROOT)
to
override val name: String get() = ...
. It's an old bug (https://github.com/Kotlin/kotlinx.serialization/issues/133) 😞
e
Ohhhh, that is exactly the issue!
Thanks!
(Posting the message again so people dont lose context)