Play writers additional property

34 views Asked by At

Is it possible to write additional property unavailable in case class constructor?

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

by extending by Entity case class have also id property

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

How can I add this property?

1

There are 1 answers

0
serejja On BEST ANSWER

You can define your writes like this:

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
  }
}