由于 22 个字段的限制,我不得不将一个大型案例类拆分为较小的类。我怎样才能扁平化Writes
这个大类?
import play.api.libs.json._
import play.api.libs.functional.syntax._
case class B(x: Option[Int], y: Option[Int])
object B {
implicit val format: (Reads[B], Writes[B]) => Format[B] = Format[B]
}
case class C(z: Option[Int], w: Option[Int])
object C {
implicit val format: (Reads[C], Writes[C]) => Format[C] = Format[C]
}
case class A(b: B, c: C)
object A {
implicit val reads: Reads[A] =
(Reads.of[B] and Reads.of[C]) (A.apply _)
implicit val writes: Writes[A] = ???
/*
val a = A(B(1, 2), C(3, 4)
Json.toJson(a) should be
{
"x": 1
"y": 2
"z": 3
"w": 4
}
*/
}