Skip to content

Add annotation to omit discriminator for sealed hierarchies #65

@rallydan

Description

@rallydan

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)

Metadata

Metadata

Assignees

No one assigned

    Labels

    enhancementNew feature or request

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions