Question

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?

Was it helpful?

Solution

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
  }
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top