-
Notifications
You must be signed in to change notification settings - Fork 624
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
@JsonClassDiscriminator and other serial info annotations in sealed, polymorphic and object serializables #1515
Closed
Closed
Changes from 3 commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
08137e2
Test for @SerialInfo with default value
sandwwraith 9d965ba
Add annotations array parameter for sealed, polymorphic, and object s…
sandwwraith 4af5547
Introduce @JsonClassDiscriminator to configure discriminator per poly…
sandwwraith d8906b7
~make adjustments to tests
sandwwraith File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
71 changes: 71 additions & 0 deletions
71
formats/json/commonMain/src/kotlinx/serialization/json/JsonAnnotations.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,71 @@ | ||
/* | ||
* Copyright 2017-2021 JetBrains s.r.o. Use of this source code is governed by the Apache 2.0 license. | ||
*/ | ||
|
||
package kotlinx.serialization.json | ||
|
||
import kotlinx.serialization.* | ||
import kotlinx.serialization.descriptors.* | ||
import kotlinx.serialization.encoding.* | ||
import kotlinx.serialization.json.internal.* | ||
import kotlin.native.concurrent.* | ||
|
||
/** | ||
* Indicates that the field can be represented in JSON | ||
* with multiple possible alternative names. | ||
* [Json] format recognizes this annotation and is able to decode | ||
* the data using any of the alternative names. | ||
* | ||
* Unlike [SerialName] annotation, does not affect JSON encoding in any way. | ||
* | ||
* Example of usage: | ||
* ``` | ||
* @Serializable | ||
* data class Project(@JsonNames("title") val name: String) | ||
* | ||
* val project = Json.decodeFromString<Project>("""{"name":"kotlinx.serialization"}""") | ||
* println(project) | ||
* val oldProject = Json.decodeFromString<Project>("""{"title":"kotlinx.coroutines"}""") | ||
* println(oldProject) | ||
* ``` | ||
* | ||
* This annotation has lesser priority than [SerialName]. | ||
* | ||
* @see JsonBuilder.useAlternativeNames | ||
*/ | ||
@SerialInfo | ||
@Target(AnnotationTarget.PROPERTY) | ||
@ExperimentalSerializationApi | ||
public annotation class JsonNames(vararg val names: String) | ||
|
||
/** | ||
* Specifies key for class discriminator value used during polymorphic serialization in [Json]. | ||
* Provided key is used only for an annotated class, to configure global class discriminator, use [JsonBuilder.classDiscriminator] | ||
* property. | ||
* | ||
* It is possible to define different class discriminators for different parts of class hierarchy. | ||
* Pay attention to the fact that class discriminator, same as polymorphic serializer's base class, is | ||
* determined statically. | ||
* | ||
* Example: | ||
* ``` | ||
* @Serializable | ||
* @JsonTypeDiscriminator("class") | ||
* abstract class Base | ||
* | ||
* @Serializable | ||
* @JsonTypeDiscriminator("error_class") | ||
* abstract class ErrorClass: Base() | ||
* | ||
* @Serializable | ||
* class Message(val object: Base, val error: ErrorClass?) | ||
* | ||
* val message = Json.decodeFromString<Message>("""{"object": {"class":"my.app.BaseMessage", "message": "not found"}, "error": {"error_class":"my.app.GenericError", "error_code": 404}}""") | ||
* ``` | ||
* | ||
* @see JsonBuilder.classDiscriminator | ||
*/ | ||
@SerialInfo | ||
@Target(AnnotationTarget.CLASS) | ||
@ExperimentalSerializationApi | ||
public annotation class JsonClassDiscriminator(val discriminator: String) |
39 changes: 0 additions & 39 deletions
39
formats/json/commonMain/src/kotlinx/serialization/json/JsonNames.kt
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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 have the impression that this is not the best idea to do
SerialDescriptorImpl
almost mutable because of one issue.Also, explicit conversion to
SerialDescriptorImpl
looks dangerous.Maybe solve the problem where it appears - in serializer? Create private var property and rewrite it in the secondary constructor or other alternatives.
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.
The problem is, the secondary constructor is called after all
init
blocks and properties initializers. I considered the following alternatives:buildSerialDescriptorInternal
function to avoid downcast to be sure this won't break..build
on every access topublic val descriptor
— I don't like copying every time