Open alphaho opened 9 months ago
/bounty $100
/attempt #1056
with your implementation plan/claim #1056
in the PR body to claim the bountyThank you for contributing to zio/zio-json!
Add a bounty • Share on socials
Attempt | Started (GMT+0) | Solution |
---|---|---|
🟢 @987Nabil | #1112 | |
🟢 @pablf | #1113 |
💡 @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.
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, thedogCodec
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 emittype
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: 1) we can support
@jsonDiscriminator
in case class (with the risk that the case class's jsonDiscriminator value different from the parent's jsonDiscriminator value), or 2) we can support reading the parent sealed trait's@jsonDiscriminator
when we deriving case class's codec