播放:JSON class extends case class 时不考虑写入

Play: JSON Writes is not respected when class extends case class

我有以下 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)} 

}

序列化 B 时的结果仅包括其父级 class A 的状态,如下所示:

{
  id:"some value",
  name: "some other value"
}

ReadsWrites 的配置应该是什么,以便子 class 中的配置得到尊重,所有包含的键值对都被适当地序列化.我期望的结果如下:

{
  id:"some value",
  name: "some other value",
  other: 4,
  another: ["a","b"]
}

我不想对键值对进行花哨的自定义序列化。如果编译器没有抱怨父 class 缺少读写隐式,我只会在 subclass B 中包含读写。

像这样使用 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 {}

并且一切顺利...老实说,Play 使用 JSON 的方法仍然很原始 - 例如与 Spring 相比。