是否可以在类构造函数中写入不可用的其他属性?
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))
如何添加此属性?
您可以像这样定义写入:
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
}
}