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

ClassDiscriminatorMode annotation for data classes #2883

Open
rattletat opened this issue Dec 16, 2024 · 5 comments
Open

ClassDiscriminatorMode annotation for data classes #2883

rattletat opened this issue Dec 16, 2024 · 5 comments
Labels

Comments

@rattletat
Copy link

Hi there,

similarly to JsonClassDiscriminator, I would like an annotation type to indicate the discriminator mode for data classes:

@Serializable
@JsonClassDiscriminatorMode(ClassDiscriminatorMode.ALL_JSON_OBJECTS)
abstract class Base

The motivation is that we have a polymorphic data class consumed by third-party actors but we want to ensure that the serialized classes always include the class type annotation, regardless of whether the third-party actors serialize it using the concrete or the polymorphic type.

If there is a work-around already known, please feel free to share.

@sandwwraith
Copy link
Member

Why you need annotation exactly and why the general Json setting does not satisfy your needs?

@rattletat
Copy link
Author

rattletat commented Dec 16, 2024

The use-case here is that we define message types consumed by multiple parties who should be able to create and serialize messages. These created messages (JSONs) will be processed by us again, for which we need to deserialize them and rely on the type field. We provide functions to serialize and deserialize them correctly (using the polymorphic type) for them to use, but it is easy for the third-parties to mess up here if they do it themselves:

# This will work
Json.encodeToString(someConcreteMessage as Message)
# This will fail
Json.encodeToString(someConcreteMessage)

Having an annotation directly on the data class to indicate to always include the type field would decrease the risk of some party not including the type field massively.

Is there a workaround to achieve something similar? I would suspect that one could include the serialName directly into the data class, but not sure if that would play nicely with the library.

@sandwwraith
Copy link
Member

Hm, I see. So Json will be created by 3rd party, and therefore, you don't control JsonBuilder.classDiscriminatorMode?
The problem with specifying the wrong type is well-known to us, and we are planning to eventually add IDE inspection for that.

@rattletat
Copy link
Author

Exactly. We provide methods on the Base class to do the serialization correctly i.e. someConcreteMessage.toJson(), but our clients could just ignore this serialization method and call Json.encodeToString(someConcreteMessage) because they saw it in some tutorial^^ In that case, setting ClassDiscriminatorMode.ALL_JSON_OBJECTS on the data class would still include the type discriminator in the resulting JSON.
Do you have an idea how this could be accomplished in other ways? Is there a way to specify a default Json serialization object on dataclasses directly?

@sandwwraith
Copy link
Member

Is there a way to specify a default Json serialization object on dataclasses directly?

No, we don't have this. The only way is to provide pre-configured Json instance in your library or convertor .toJson() methods as you already did.

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