Skip to content
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

Don't check kind if needn't type discriminator for enum and primitive #2753

Open
SettingDust opened this issue Jul 30, 2024 · 5 comments
Open
Assignees
Labels

Comments

@SettingDust
Copy link

SettingDust commented Jul 30, 2024

What is your use-case and why do you need this feature?
Output the enum and primitive correctly.
If we add a PrimitiveKind descriptor to a subclass of polymorphic serializer. There is error #1486
If we add a wrapper for the primitive(#1252 (comment)). The result will be wrapped even if the classDiscriminatorMode = ClassDiscriminatorMode.NONE like below.

{
  "data": {
    "value": "FOO"
  }
}

Related: #1252 #1486

Describe the solution you'd like
https://github.com/Kotlin/kotlinx.serialization/blob/master/formats/json/commonMain/src/kotlinx/serialization/json/internal/Polymorphic.kt#L41
Avoid failing if needn't discriminator for checkKind. Add a condition in checkKind or encodePolymorphically

val kind = actual.descriptor.kind
if(needDiscriminator || (kind !is SerialKind.ENUM && kind !is PrimitiveKind)) checkKind(kind )
@sandwwraith
Copy link
Member

Can you post an example code and incorrect output, please?

@SettingDust
Copy link
Author

SettingDust commented Jul 30, 2024

The same error with #1486.
This is a FR for allowing enum and primitive in polymorphic if needn't the type information.
I'll append the example to body

@sandwwraith
Copy link
Member

if needn't the type information.

You always need type information when deserializing polymorphic type. Are you talking about serialization-only use case?

@SettingDust
Copy link
Author

You always need type information when deserializing polymorphic type. Are you talking about serialization-only use case?

https://github.com/Kotlin/kotlinx.serialization/blob/master/formats/json/commonMain/src/kotlinx/serialization/json/JsonConfiguration.kt#L78-L84 said it's a use case.

sandwwraith added a commit that referenced this issue Oct 10, 2024
for subclasses' polymorphic serializers if Json.classDiscriminatorMode is set to NONE.

This should simplify the code in situations where JSON is expected to be only sent and not parsed.

Fixes #2753
Fixes #1486
@sandwwraith sandwwraith self-assigned this Oct 10, 2024
sandwwraith added a commit that referenced this issue Oct 30, 2024
for subclasses' polymorphic serializers if Json.classDiscriminatorMode is set to NONE.

This should simplify the code in situations where JSON is expected to be only sent and not parsed.

Fixes #2753
Fixes #1486
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

2 participants