-
Notifications
You must be signed in to change notification settings - Fork 147
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
[Feature Request] support jsonDiscriminator in case class #1056
Labels
Comments
/bounty $100 |
💎 $100 bounty • ZIOSteps to solve:
Thank you for contributing to zio/zio-json! Add a bounty • Share on socials
|
💡 @987Nabil submitted a pull request that claims the bounty. You can visit your bounty board to reward. |
💡 @pablf submitted a pull request that claims the bounty. You can visit your bounty board to reward. |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
With a sealed trait like below, the
anmailCodec
would emitatype
property in the encoded json string as expected like:{"type": "dog", "name": "Snoopy"}
.But sometimes in the client code, I want to use the more specific
Dog
type instead ofAnimal
. That requires me to define thedogCodec
as above inDog
's companion object.However, the
dogCodec
would not emit atype
property in the encoded json, and get us something like{"name": "Snoopy"}
instead. And it would not honor thetype
property during decoding, accepting invalid input with unexpectedtype
.For example, below test would fail as it parses the json successfully:
One workaround I have is to define extra sealed traits for the case classes like the
Dog
type.For example, to emit
type
property forDog
, we can:However, this workaround is quite verbose, especially if we need multiple case class codecs to emit the discriminator.
It would be better if:
@jsonDiscriminator
in case class (with the risk that the case class's jsonDiscriminator value different from the parent's jsonDiscriminator value), or@jsonDiscriminator
when we deriving case class's codecThe text was updated successfully, but these errors were encountered: