是否可以在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))
如何添加此属性?
答案 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
}
}