Suppose I have multiple types of ids defined as follows.
sealed trait Id {
val value: String
}
case class IdA(value: String) extends Id
case class IdB(value: String) extends Id
case class IdC(value: String) extends Id
These classes should be decoded from and encoded to the following JSON.
{
"id: "some-id"
}
How can I define some generic decoder/encoder for the id,
case class A(id: IdA, name: String, count: Int)
case class B(id: IdB, name: String, count: Int)
case class C(id: IdC, name: String, count: Int)
so that the classes above can be decoded from and encoded to the JSON below?
{
"id" : "some-id",
"name": "some-name",
"count": 2
}
if possible, I want the id field to be flexible for both decoder and encoder that is id maybe "id-x" in one case and "id-y" in another.