Type IDs
When encoding Abstract Data Types (ADTs), i.e. top types of sealed
type hierarchies like Animal
in this example:
sourcesealed trait Animal
case class Dog(age: Int, name: String) extends Animal
case class Cat(weight: Double, color: String, home: String) extends Animal
case class Fish(color: String) extends Animal
case object Yeti extends Animal
the encoder needs to somehow transfer the information to the decoder, which of the defined sub-types to decode into.
In the context of the example ADT above, when Json.decode(encoding).to[Animal]
is called the decoder needs to know, whether the encoding holds a Dog
, a Cat
or any of the other (well-defined) sub-types of Animal
.
This is done through a “Type ID”, which is a String
or number that uniquely identifies each ADT sup-type.
By default borer will use a sub-type’s short class name as a (textual) type ID, i.e. "Dog"
would be the type id of Dog
instances and "Cat"
would be the same for the Cat
type.
If you want to customize this you can use the @key
annotation to do so. Check out the @key
sources here for more info on this.