扮演作家的额外财产

时间:2014-02-27 13:08:38

标签: scala playframework-2.0 playframework-2.2 writers

是否可以在case类构造函数中编写其他不可用的属性?

case class Task(var name:String, var counter:Int) extends Entity

通过实体案例类扩展也有id属性

implicit val task = (
    (__ \ 'name).write[String] and
    (__ \ 'counter).write[Int] and
    (__ \ 'id).write[String] <==== ???
)(unlift(Task.unapply))

如何添加此属性?

1 个答案:

答案 0 :(得分:1)

您可以像这样定义您的写作:

implicit val writer = new Writes[Task] {
  def writes(t: Task): JsValue = {
    Json.obj("name" -> t.name,
      "counter" -> t.counter,
      "id" -> t.getIdFromSomewhere) //here's the thing you want
  }
}