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

Exclude class discriminator #1487

Closed
P1NG2WIN opened this issue May 15, 2021 · 8 comments
Closed

Exclude class discriminator #1487

P1NG2WIN opened this issue May 15, 2021 · 8 comments
Labels

Comments

@P1NG2WIN
Copy link

Right now in Json { } we cannot disable write classDiscriminator. In source code i found that class discriminator can be ignored if we change writePolymorphic boolean inside encodePolymorphically in StreamingJsonEncoder. Can i somehow change it without fully fork your library and change all layers? Maybe somehow with extension

@Dominaezzz
Copy link
Contributor

You can skip the writing of the classDiscriminator by using the serializer of the child class instead of the base class.

@P1NG2WIN
Copy link
Author

You can skip the writing of the classDiscriminator by using the serializer of the child class instead of the base class.

Can you provide example?

@sandwwraith
Copy link
Member

val data = ChildClass("...")
Json.encodeToString<ParentClass>(data) // discriminator written
Json.encodeToString<ChildClass>(data) // discriminator omitted

@P1NG2WIN
Copy link
Author

In my case, this will not suit

@sandwwraith
Copy link
Member

What's your use case for excluding the discriminator? You won't be able to decode polymorphic data without it.

@Whathecode
Copy link
Contributor

Whathecode commented May 19, 2021

What's your use case for excluding the discriminator?

Probably the other posted issue: #1486 (comment)

In that specific use case, using JsonContentPolymorphicSerializer could work, but as I comment there, would still not be recommendable.

@P1NG2WIN
Copy link
Author

P1NG2WIN commented May 19, 2021

@sandwwraith I don't need to decode this json. It goes to the server. The server does not accept unknown fields. Right now I forked the library and added a new field to the builder, so now I have two Json instances, one for the application that writes the class discriminator and the other for the api, which skips it when serializing.

@sandwwraith
Copy link
Member

I'll close this because #1247 already mentions this use-case — feel free to continue the discussion there

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

4 participants