Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Play 2.1 Json serialization for traits?

I have this:

package models

import play.api.libs.json._
import play.api.libs.functional.syntax._

object ModelWrites {
    implicit val tmoWrites= Json.writes[TestModelObject]
    implicit val ihWrites = Json.writes[IntHolder]
}

case class TestModelObject(s1:String, s2:String)

case class IntHolder(i1:Int, i2:Int)

trait HasInts {
    val ints: List[IntHolder]
}

When I do this:

scala> val tmo = new TestModelObject("hello", "world") with HasInts {
  val ints = List(IntHolder(1,2), IntHolder(3,4))
}

scala> Json.toJson(tmo)
res0: play.api.libs.json.JsValue = {"s1":"hello","s2":"world"}

how can I implicity serialize the mixed-in val 'ints'? Like:

scala> val someInts = List(IntHolder(8,9), IntHolder(10,11))
someInts: List[models.IntHolder] = List(IntHolder(8,9), IntHolder(10,11))

scala> Json.toJson(someInts)
res1: play.api.libs.json.JsValue = [{"i1":8,"i2":9},{"i1":10,"i2":11}]

Note: if I try: implicit val hasIntsWrites = Json.writes[HasInts] I (expectedly?) get:

[error] Models.scala:10: No unapply function found
[error]     implicit val hasIntsWrites = Json.writes[HasInts]
[error]                                             ^
like image 764
Lester Burnham Avatar asked Jan 03 '13 19:01

Lester Burnham


1 Answers

You're not going to be able to use the experimental "Inception" feature (Json.writes[...]) directly here, since that only works on case classes. You can, however, build on the Writes instances that Inception can provide to accomplish what you want with only a very little boilerplate.

Note that I'm ignoring the question of whether mixing in a trait when instantiating a case class like this is a good idea—it probably isn't—but the approach I give here will work in the more general case as well.

First for the classes and imports (no changes here):

case class TestModelObject(s1: String, s2: String)
case class IntHolder(i1: Int, i2: Int)
trait HasInts { val ints: List[IntHolder] }

import play.api.libs.json._
import play.api.libs.functional.syntax._

Now we need to put all our lower-priority instances into a trait to make sure that the compiler will pick the right one, since TestModelObject with HasInts is a subtype of both TestModelObject and HasInts:

trait LowPriorityWritesInstances {
  implicit val tmoWrites = Json.writes[TestModelObject]
  implicit val ihWrites = Json.writes[IntHolder]

  implicit object hiWrites extends OWrites[HasInts] {
    def writes(hi: HasInts) = Json.obj("ints" -> hi.ints)
  }
}

And now the main event:

object WritesInstances extends LowPriorityWritesInstances {
  implicit val tmowhiWrites = new Writes[TestModelObject with HasInts] {
    def writes(o: TestModelObject with HasInts) =
      tmoWrites.writes(o) ++ implicitly[OWrites[HasInts]].writes(o)
  }
}

And we're done:

scala> import WritesInstances._
import WritesInstances._

scala> val tmo = new TestModelObject("hello", "world") with HasInts {
     |   val ints = List(IntHolder(1, 2), IntHolder(3, 4))
     | }

scala> println(Json.toJson(tmo))
{"s1":"hello","s2":"world","ints":[{"i1":1,"i2":2},{"i1":3,"i2":4}]}

As desired.

like image 57
Travis Brown Avatar answered Nov 18 '22 02:11

Travis Brown