the experimental "inception" feature in play 2.1 (json.format[...]) works case classes (see here). how can write custom format implicit trait. have following construct:
sealed trait plan { def id: string def name: string def apps: int def users: int def testruns: int def price: int def prio: int }
and following case classes extending trait plan.
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
now need write custom implicit format val in plan companion object. tried:
object plan { implicit val planformats = ( (__ \ "id").format[string] , (__ \ "name").format[string] , (__ \ "apps").format[int] , (__ \ "users").format[int] , (__ \ "testruns").format[int] , (__ \ "price").format[int] , (__ \ "prio").format[int] )(plan.apply, unlift(plan.unapply)) }
however, trait hasn't apply or unapply method. correct way provide implicit val json serialization in play 2.1?
you have provide own function creates new instance given values.
essentially companion object trait acts factory.
object plan { def apply(id: string, name: string, ...) = id match { case "pro" => new pro(id, name, ...) ... } def unapply(p: person): option[(string, string, ...)] = ... }
Comments
Post a Comment