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

Add annotation to omit discriminator for sealed hierarchies #65

Open
rallydan opened this issue Mar 26, 2021 · 0 comments
Open

Add annotation to omit discriminator for sealed hierarchies #65

rallydan opened this issue Mar 26, 2021 · 0 comments
Labels
enhancement New feature or request

Comments

@rallydan
Copy link

rallydan commented Mar 26, 2021

While the default mechanism of including a discriminator allows for easy parsing, sometimes there is a need to match a specific JSON schema that doesn't include a single field that can be used for this purpose.

We should add an annotation that can be used on the trait that indicates that no discriminator will be present, and still allow generating a From on the trait, and both From and To on the leaf classes. Only macroTo on the base class(es) should fail. Users can then either hand-code a To, or omit it if it is not needed.

@nodiscriminator
sealed trait Fruit

case class Apple(seeds: Int) extends Fruit

implicit val fruitPickler: From[Fruit] = macroFrom
implicit val applePickler: FromTo[Apple] = macroFromTo

val red: Fruit = Apple(15)
val json = FromScala(red).transform(ToJson.string) // """{"seeds":15}"""
val reread = FromJson(json).transform(ToScala[Apple]) // Apple(15)
@htmldoug htmldoug added the enhancement New feature or request label Jun 28, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

No branches or pull requests

2 participants