Thursday, 15 September 2011

scala - Serialize Case Object Extending Trait -



scala - Serialize Case Object Extending Trait -

given trait conjunction and , or case object sub-types:

trait conjunction case object , extends conjunction case object or extends conjunction

using play 2 json, tried write next writes[conjunction]:

implicit object conjunctionwrites extends writes[conjunction] { implicit val orwrites: writes[or] = json.writes[or] implicit val andwrites: writes[and] = json.writes[and] def writes(c: conjunction) = c match { case a@and => json.tojson(a)(andwrites) case o@or => json.tojson(o)(orwrites) } }

but got bunch of not found: type and/or errors.

how can serialize these case objects?

when create case object, create value name, not type. and , or don't exist types. if want refer type of case object, utilize .type, e.g. and.type.

however, json.writes macro works on case classes, not case objects. you'll have write own definition:

implicit object conjunctionwrites extends writes[conjunction] { def writes(c: conjunction) = c match { case , => json.tojson("and") case or => json.tojson("or") } }

scala serialization playframework-2.0

No comments:

Post a Comment