Sum types are sealed trait
s, sealed class
es and
non-singleton enum
s.
Since they have one or more subtypes, we need to disambiguate between them somehow.
The default in Tupson is to use the @type
property.
Its value is the simple type name of class or enum case.
This makes JSON independent of scala/java package and it is more readable.
Example:
enum Color derives JsonRW:
case Hex(num: String)
case Yellow
val color = Color.Hex("FFF")
println(color.toJson)
// { "@type":"Hex", "num":"FFF" }
You can use some other key by annotating the sum type with @discriminator
:
@discriminator("myOtherKey")
enum Color derives JsonRW ...
println(color.toJson)
// { "myOtherKey":"Hex", "num":"FFF" }