-
Notifications
You must be signed in to change notification settings - Fork 668
Added documentation for @KeepGeneratedSerializer feature #2669
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
Changes from all commits
8f0e036
d3962bc
9255092
e76cb45
9723c2a
6bf9b4e
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -37,6 +37,7 @@ In this chapter, we'll walk through features of [JSON](https://www.json.org/json | |
| * [Array unwrapping](#array-unwrapping) | ||
| * [Manipulating default values](#manipulating-default-values) | ||
| * [Content-based polymorphic deserialization](#content-based-polymorphic-deserialization) | ||
| * [Extending the behavior of the plugin generated serializer](#extending-the-behavior-of-the-plugin-generated-serializer) | ||
| * [Under the hood (experimental)](#under-the-hood-experimental) | ||
| * [Maintaining custom JSON attributes](#maintaining-custom-json-attributes) | ||
|
|
||
|
|
@@ -1260,6 +1261,53 @@ No class discriminator is added in the JSON output: | |
|
|
||
| <!--- TEST --> | ||
|
|
||
| ### Extending the behavior of the plugin generated serializer | ||
| In some cases, it may be necessary to add additional serialization logic on top of the plugin generated logic. | ||
| For example, to add a preliminary modification of JSON elements or to add processing of unknown values of enums. | ||
|
|
||
| In this case, you can mark the serializable class with the [`@KeepGeneratedSerializer`][KeepGeneratedSerializer] annotation and get the generated serializer using the `generatedSerializer()` function. | ||
|
|
||
| Here is an example of the simultaneous use of [JsonTransformingSerializer] and polymorphism. | ||
| 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. | ||
| ```kotlin | ||
| @Serializable | ||
| sealed class Project { | ||
| abstract val name: String | ||
| } | ||
|
|
||
| @KeepGeneratedSerializer | ||
| @Serializable(with = BasicProjectSerializer::class) | ||
| @SerialName("basic") | ||
| data class BasicProject(override val name: String): Project() | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Also, do you test your sample automatically? If not, can you add this code to your tests in #2758? I will also create an issue with a small reproducer.
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
yes
Thanks! |
||
|
|
||
| object BasicProjectSerializer : JsonTransformingSerializer<BasicProject>(BasicProject.generatedSerializer()) { | ||
| override fun transformDeserialize(element: JsonElement): JsonElement { | ||
| val jsonObject = element.jsonObject | ||
| return if ("basic-name" in jsonObject) { | ||
| val nameElement = jsonObject["basic-name"] ?: throw IllegalStateException() | ||
| JsonObject(mapOf("name" to nameElement)) | ||
| } else { | ||
| jsonObject | ||
| } | ||
| } | ||
| } | ||
|
|
||
|
|
||
| fun main() { | ||
| val project = Json.decodeFromString<Project>("""{"type":"basic","basic-name":"example"}""") | ||
| println(project) | ||
| } | ||
| ``` | ||
|
|
||
| > You can get the full code [here](../guide/example/example-json-29.kt). | ||
|
|
||
| `BasicProject` will be printed to the output: | ||
|
|
||
| ```text | ||
| BasicProject(name=example) | ||
| ``` | ||
| <!--- TEST --> | ||
|
|
||
| ### Under the hood (experimental) | ||
|
|
||
| Although abstract serializers mentioned above can cover most of the cases, it is possible to implement similar machinery | ||
|
|
@@ -1345,7 +1393,7 @@ fun main() { | |
| } | ||
| ``` | ||
|
|
||
| > You can get the full code [here](../guide/example/example-json-29.kt). | ||
| > You can get the full code [here](../guide/example/example-json-30.kt). | ||
|
|
||
| This gives you fine-grained control on the representation of the `Response` class in the JSON output: | ||
|
|
||
|
|
@@ -1410,7 +1458,7 @@ fun main() { | |
| } | ||
| ``` | ||
|
|
||
| > You can get the full code [here](../guide/example/example-json-30.kt). | ||
| > You can get the full code [here](../guide/example/example-json-31.kt). | ||
|
|
||
| ```text | ||
| UnknownProject(name=example, details={"type":"unknown","maintainer":"Unknown","license":"Apache 2.0"}) | ||
|
|
@@ -1440,6 +1488,7 @@ The next chapter covers [Alternative and custom formats (experimental)](formats. | |
| [InheritableSerialInfo]: https://kotlinlang.org/api/kotlinx.serialization/kotlinx-serialization-core/kotlinx.serialization/-inheritable-serial-info/index.html | ||
| [KSerializer]: https://kotlinlang.org/api/kotlinx.serialization/kotlinx-serialization-core/kotlinx.serialization/-k-serializer/index.html | ||
| [Serializable]: https://kotlinlang.org/api/kotlinx.serialization/kotlinx-serialization-core/kotlinx.serialization/-serializable/index.html | ||
| [KeepGeneratedSerializer]: https://kotlinlang.org/api/kotlinx.serialization/kotlinx-serialization-core/kotlinx.serialization/-keep-generated-serializer/index.html | ||
|
|
||
| <!--- INDEX kotlinx-serialization-core/kotlinx.serialization.encoding --> | ||
|
|
||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I don't understand this sentence:
@KeepGeneratedSerializeris not allowed with any inheritance, but your sample code uses a class with inherits from an interface. (see other comment)Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It means that you cannot specify the
@KeepGeneratedSerializerannotation on the interfaces, sealed/abstract classes, but it's ok to add this annotation add to the heirs