-
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
Conversation
…morphic base class Fixes #546
core/commonTest/src/kotlinx/serialization/SerialDescriptorAnnotationsTest.kt
Outdated
Show resolved
Hide resolved
objectInstance: T, | ||
classAnnotations: Array<Annotation> | ||
) : this(serialName, objectInstance) { | ||
(this.descriptor as SerialDescriptorImpl).annotations = classAnnotations.asList() |
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:
- Mutable (to some reasonable extent) serialDescriptorImpl — current solution. I can add
buildSerialDescriptorInternal
function to avoid downcast to be sure this won't break. - Store the builder in property, call
.build
on every access topublic val descriptor
— I don't like copying every time - Store descriptor in private mutable property, rebuild it from scratch (because we do not have a copying mechanism) every time. Seems reasonable, although a bit boilerplaite-ish. However, I do not see any strong objections to this solution, so I can rewrite everything to it
formats/json/commonTest/src/kotlinx/serialization/features/JsonClassDiscriminatorTest.kt
Show resolved
Hide resolved
formats/json/commonMain/src/kotlinx/serialization/json/internal/Polymorphic.kt
Show resolved
Hide resolved
Superseded by #1608 as we changed the design |
As of now, requires a special branch of the Kotlin compiler to work (will likely be merged in 1.5.20)