I can define a serializer for a class in many plac...
# serialization
a
I can define a serializer for a class in many places. On the class with
@Serializable
, on the field, for a whole file, in multiple ways with a SerializersModules, and there's also reflection. Is the order of priority defined somewhere? I'm asking because I want to see how to override a serializer that's defined on a class with
@Serializable(with = ...)
, and it's not clear in which order serializers are considered and selected.
e
SerializersModule
can only affect
@Contextual
and
@Polymorphic
serialization (additionally,
@file:UseContextualSerializers
is
@Contextual
on everything).
@Serializable(with = )
on a property overrides
@file:UseSerializers
overrides the type's own serializer.
https://kotlin.github.io/kotlinx.serialization/kotlinx-serialization-core/kotlinx.serialization/-use-serializers/index.html
Serializers from this list have higher priority than default, but lesser priority than serializers defined on the property itself, such as Serializable (with=...) or Contextual.
if by reflection you mean top-level
serializer(KType)
and similar, that doesn't have access to
SerializersModule
, so there's only one thing it can possibly do.
SerializersModule.serializer(KType)
is documented to look up from the module first with fallback: https://kotlin.github.io/kotlinx.serialization/kotlinx-serialization-core/kotlinx.serialization/serializer.html