在递归选项列表上播放延迟写入

时间:2013-11-23 21:31:54

标签: json scala playframework playframework-2.0

如何准备(播放框架,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写入

1 个答案:

答案 0 :(得分:8)

你非常接近:

implicit val personWrites: Writes[Person] = (
  (__ \ "name").write[String] and
  (__ \ "age").write[Int] and
  (__ \ "relations").lazyWriteNullable(Writes.traversableWrites(personWrites)))(unlift(Person.unapply))