I have the following hierarchy of classes
case class A(id: String,name:String)
object A {
implicit val reads:Reads[A] = Json.reads[A]
implicit val writes:Writes[A] = Json.writes[A]
}
class B(id:String,name:String, val other:Int,val another:Vector[String]) extends A(id,name)
object B {
implicit val reads:Reads[B] = Json.reads[B]
implicit val writes:Writes[B] = Json.writes[B]
def apply(id: String,name:String, other:Int, another:Vector[String]) = new B(id,name,other,another)
def unapply(b: B):Option[(String,String)] = Some {(b.id,b.name,b.other,b.another)}
}
the result when serialising B only includes the state of its parent class A
and is the following:
{
id:"some value",
name: "some other value"
}
what should be the configuration of the Reads
and Writes
in order for the ones in the subclass to be respected and all the included key value pairs to be appropriately serialised. The result I expect is the following:
{
id:"some value",
name: "some other value",
other: 4,
another: ["a","b"]
}
I want no fancy custom serialisation of the key value pairs. I would only include the reads and writes in the subclass B if only the compiler did not complain that the parent class is missing read and write implicits.
Just use this library like this
sealed trait A
object A {
implicit val oformat: OFormat[A] = julienrf.json.derived.oformat(adapter = NameAdapter.snakeCase)
}
case class B(x: String, y: String) extends A {}
and everything works smoothly... To be honest Play's approach on working with JSONs is still quite primitive - compared to Spring for example.