如何准备(播放框架,scala)Json写了一个对象列表的选项,例如
case class Person(name: String, age: Int, relations: Option[List[Person]])
我尝试了以下写作
implicit val personWrites: Writes[Person] = (
(__ \ "name").write[String] and
(__ \ "age").write[Int] and
(__ \ "relations").lazyWrite[Option[Writes.traversableWrites[Person](personWrites)]])(unlift(Person.unapply))
它不起作用,有人可以用正确的方法帮助我创建这些Json写入
答案 0 :(得分:8)
你非常接近:
implicit val personWrites: Writes[Person] = (
(__ \ "name").write[String] and
(__ \ "age").write[Int] and
(__ \ "relations").lazyWriteNullable(Writes.traversableWrites(personWrites)))(unlift(Person.unapply))