scala - Play Framework Json Object mapping partial objects -
another play framework 2.1 question documentation techie me wrap head around.
if have scala case class object represents something, server:
case class server(name: string, ip: string, operatingsystem: enums.operatingsystem) implicit val serverreads = ((__ \ "name").read[string] , (__ \ "ip").read[string] , (__ \ "os").read[enums.operatingsystem])(server.apply _) implicit val serverwrite = ((__ \ "name").write[string] , (__ \ "ip").write[string] , (__ \ "os").write[enums.operatingsystem])(unlift(server.unapply))
i create json reads , writes , can process whole object, fine.
but possible map partial objects?
for example, if had server wasn't active may not have ip, know change option[string] , map none, isn't perfect example, if wanted simplify json model without changing underlying case class, can map values class fields, whilst leaving others @ default?
thanks
tom
you create custom apply methond i.e. simplaapply
. create object simpleserver
matching json structure. when working case classes can define instance default data , copy instance while overwriting new data i.e. i.copy(prop1=42)
.
Comments
Post a Comment