-
Notifications
You must be signed in to change notification settings - Fork 636
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Customizing serialization (without @Contextual
)
#2279
Comments
kotlinx.serialization uses a compiler plugin, which means that actual serializer for type gets compiled into the generated serialization code. There is no type mapping for it on runtime, as KClass is never retrieved. That's why To ease specifying serializers, I can recommend you one of the techniques from here: https://github.com/Kotlin/kotlinx.serialization/blob/master/docs/serializers.md#specifying-serializers-for-a-file |
The Also, the compiler plugin could simply treat all non-primitive types as if they had Alternatively, what about having a simple interface like this interface SerializationMapper {
fun serialize(value: Any?, defaultSerialization: SerializationMapper, fieldAnnotations: List<Any>): Any?
} The default behavior would be contained in the class ZonedDateTimeMapper(val someFormatSpec: IsoFormatSpec) : SerializationMapper {
fun serialize(value: Any?, defaultSerialization: SerializationMapper, fieldAnnotations: List<Any>): Any? =
if (value is ZonedDateTime)
value.formatToIsoString(someFormatSpec)
else
defaultSerialization.serialize(value, defaultSerialization, fieldAnnotations)
} Though, just having a default |
This goes against design principles: given that classes are processed at compile time, we can also report errors about missing serializers at compile time and in the IDE, which is great and important for us. By auto-inserting |
Then how about introducing module-wide serializer mappings and also adding Contextual for types which define a serializer (e.g. so kotlinx-datetime's Instant can be customized without extra annotations)? I'd rather take a small performance hit than deal with bugs due to missing Contextual. But module wide rules could even solve the performance issue. |
Still can't find a way to make the plugin —in enums— use the explicit serializer if it exist, and the default serializer if not without using my use-case is I have a database enum class ExampleEnum(
override val dtoName: String,
override val persistenceName: String
) : EnumDtoName, EnumPersistenceName {
FIRST(dtoName = "first_on_mobile", persistenceName = "first_on_database"),
SECOND(dtoName = "second_on_mobile", persistenceName = "second_on_database"),
THIRD(dtoName = "third_on_mobile", persistenceName = "third_on_database")
} Any update on this? :') |
@shalaga44 There are 2 ways that this behaviour can be achieved:
|
Related discussion: #507 |
What is your use-case and why do you need this feature?
We have lots of modules which have two or three different ZonedDateTime encodings because different backends have different format requirements. How can we avoid plastering the whole code with
@Contextual
and getting bugs where we forget to add the annotation? (esp. if ZonedDateTime has a default serializer)Another use-case is that we have individual encrypted fields and we'd like to e.g. set an
@Encrypted
annotation on them so that the value is pre/post-processed for (de-)serialization.Describe the solution you'd like
We'd like to have one central place to set the serializer per
Json
instance. There already isserializersModule
, but it's not clear to us why we additionally have to set@Contextual
everywhere in the code (or per file). This feels completely unnecessary and just makes the code more error-prone. Is it possible to have something likeserializersModule
without@Contextual
somehow?Ideally, we'd like to be able to inject some mapper function which gets the type and information about the field (so we can lookup potential annotations) and then it can override the default (de-)serialization behavior either fully or pre/post-process the data before/after the default serialization behavior.
The text was updated successfully, but these errors were encountered: