How to decode a specific enum/sealed trait subtype with discriminator #1112
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
After thinking about the suggestions made in the issue, I think they are not desirable for default behaviour.
@jsonDiscriminator
can not only lead to inconsistencies, but is unintuitive and very strange when applied to a case class that is not part of a sealed hierarchy.DeriveJsonCodec.gen[Dog]
asks very explicitly for aDog
codec, but in my opinion theDog
codec should not be aware of it being a subtype of a sealed trait here at all. This is not only more consistent, but I would argue this change would be a breaking one. A codec that might be currently derived and works well, would suddenly fail, if this is implemented and released. And wanting a codec that treatsDog
just as a case class is also reasonable.Over all, I don't see any of these solutions as a good one. The idea is somehow to fit the derivation to some personal needs. And they don't see common to me. I decided instead to add docs how to create a custom
Decoder
that fails when the wrong structure is found.Another option could be to implement #689 and define the decoder by hand.
fixes #1056
/claim #1056