You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
@@ -1260,6 +1261,53 @@ No class discriminator is added in the JSON output:
1260
1261
1261
1262
<!--- TEST -->
1262
1263
1264
+
### Extending the behavior of the plugin generated serializer
1265
+
In some cases, it may be necessary to add additional serialization logic on top of the plugin generated logic.
1266
+
For example, to add a preliminary modification of JSON elements or to add processing of unknown values of enums.
1267
+
1268
+
In this case, you can mark the serializable class with the [`@KeepGeneratedSerializer`][KeepGeneratedSerializer] annotation and get the generated serializer using the `generatedSerializer()` function.
1269
+
1270
+
Here is an example of the simultaneous use of [JsonTransformingSerializer] and polymorphism.
1271
+
In this example, we use `transformDeserialize` function to rename `basic-name` key into `name` so it matches the `abstract val name` property from the `Project` supertype.
* <aname='simultaneous-use-of-plugin-generated-and-custom-serializers'></a>[Simultaneous use of plugin-generated and custom serializers](serializers.md#simultaneous-use-of-plugin-generated-and-custom-serializers)
* <aname='extending-the-behavior-of-the-plugin-generated-serializer'></a>[Extending the behavior of the plugin generated serializer](json.md#extending-the-behavior-of-the-plugin-generated-serializer)
140
142
* <aname='under-the-hood-experimental'></a>[Under the hood (experimental)](json.md#under-the-hood-experimental)
*[Contextual serialization and generic classes](#contextual-serialization-and-generic-classes)
@@ -810,7 +811,7 @@ fun main() {
810
811
811
812
<!---TEST-->
812
813
813
-
### Specifying serializers for a file
814
+
### Specifying serializers for a file
814
815
815
816
A serializer for a specific type, like `Date`, can be specified for a whole source code file with the file-level
816
817
[UseSerializers] annotation at the beginning of the file.
@@ -975,6 +976,58 @@ features that a serializer implementation would like to take advantage of.
975
976
976
977
This chapter proceeds with a generic approach to tweaking the serialization strategy based on the context.
977
978
979
+
## Simultaneous use of plugin-generated and custom serializers
980
+
In some cases it may be useful to have a serialization plugin continue to generate a serializer even if a custom one is used for the class.
981
+
982
+
The most common examples are: using a plugin-generated serializer for fallback strategy, accessing type structure via [descriptor][KSerializer.descriptor] of plugin-generated serializer, use default serialization behavior in descendants that do not use custom serializers.
983
+
984
+
In order for the plugin to continue generating the serializer, you must specify the `@KeepGeneratedSerializer` annotation in the type declaration.
985
+
In this case, the serializer will be accessible using the `.generatedSerializer()` function on the class's companionobject.
986
+
987
+
Annotation `@KeepGeneratedSerializer` isnot allowed on classes involved in polymorphic serialization: interfaces, sealed classes, abstract classes, classes marked by [Polymorphic].
0 commit comments