Note that there are some explanatory texts on larger screens.

plurals
  1. POPlay 2.1 Json serialization of traits
    text
    copied!<p>The experimental "Inception" feature in Play 2.1 (Json.format[...]) only works for case classes (see <a href="https://stackoverflow.com/questions/14145432/play-2-1-json-serialization-for-traits">here</a>). How can I write my custom format implicit for a trait. I have the following construct:</p> <pre><code>sealed trait Plan { def id: String def name: String def apps: Int def users: Int def testruns: Int def price: Int def prio: Int } </code></pre> <p>And the following case classes which are extending the trait Plan.</p> <pre><code>case class Start( id: String = "start", name: String = "Start", apps: Int = 1, users: Int = 1, testruns: Int = 10, price: Int = 99, prio: Int = 30) extends Plan case class Pro( id: String = "pro", name: String = "Pro", apps: Int = 2, users: Int = 5, testruns: Int = 25, price: Int = 299, prio: Int = 20) extends Plan case class Premium( id: String = "premium", name: String = "Premium", apps: Int = -1, users: Int = -1, testruns: Int = -1, price: Int = 799, prio: Int = 10) extends Plan </code></pre> <p>Now I need to write my custom implicit format val in the Plan companion object. I tried:</p> <pre><code>object Plan { implicit val planFormats = ( (__ \ "id").format[String] and (__ \ "name").format[String] and (__ \ "apps").format[Int] and (__ \ "users").format[Int] and (__ \ "testruns").format[Int] and (__ \ "price").format[Int] and (__ \ "prio").format[Int] )(Plan.apply, unlift(Plan.unapply)) } </code></pre> <p>However, a trait hasn't an apply or unapply method. What is the correct way to provide an implicit val for the json serialization in Play 2.1? </p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload