scala play 2.1.1 json functional syntax mapping data to a different format -
i'm trying write custom json serializer using functional syntax , can't seem find right way solve particular problem. have couple of joda datetime objects want write in specific format ui consuming it. can show me i've gone wrong?
here case class dealing @ moment, nothing special going on.
case class banner( id: int = 0, ownerid: int = 0, licenseeid: option[int] = none, statuscolor: option[string] = none, content: option[string] = none, displaystart: datetime = new datetime(), displayend: datetime = new datetime(), created: datetime = new datetime(), updated: datetime = new datetime() )
these imports serializer object.
import play.api.libs.json._ import play.api.libs.functional.syntax._ import org.joda.time.datetime import org.joda.time.format.datetimeformat
first off, joda datetime converts long fine implicitly macro expander works great if that's wanted.
object myserializers { implicit val writesbanner = json.writes[banner] }
what need convert displaystart , displayend objects particular string format rather long value. tried do.
object myserializers { val userdateformatter = datetimeformat.forpattern("mm/dd/yyyy hh:mm a") implicit val writesbanner = ( (__ \ "id").write[int] , (__ \ "ownerid").write[int] , (__ \ "licenseeid").write[int] , (__ \ "statuscolor").writenullable[string] , (__ \ "content").writenullable[string] , (__ \ "displaystart").write[datetime].inmap[string](dt => userdateformatter.print(dt)) , (__ \ "displayend").write[datetime].inmap[string](dt => userdateformatter.print(dt)) , (__ \ "created").write[datetime] , (__ \ "updated").write[datetime] )(unlift(banner.unapply)) }
but compiler not happy that, don't seem understand correct way use inmap function.
could not find implicit value parameter fu: play.api.libs.functional.invariantfunctor[play.api.libs.json.owrites] [error] (__ \ "displaystart").write[datetime].inmap[string](dt => userdateformatter.print(dt)) , [error] ^
any advice appreciated.
i managed 1 figured out, using wrong type of functor map operations , had types working backwards. here working reads/writes implementations in nicer functional syntax.
implicit val writesbanner = ( (__ \ "id").write[int] , (__ \ "ownerid").write[int] , (__ \ "licenseeid").writenullable[int] , (__ \ "statuscolor").writenullable[string] , (__ \ "content").writenullable[string] , (__ \ "displaystart").write[string].contramap[datetime](dt => userdateformatter.print(dt)) , (__ \ "displayend").write[string].contramap[datetime](dt => userdateformatter.print(dt)) , (__ \ "created").write[datetime] , (__ \ "updated").write[datetime] )(unlift(banner.unapply)) implicit val readsbanner = ( (__ \ "id").read[int] , (__ \ "ownerid").read[int] , (__ \ "licenseeid").readnullable[int] , (__ \ "statuscolor").readnullable[string] , (__ \ "content").readnullable[string] , (__ \ "displaystart").read[string].fmap[datetime](dt => datetime.parse(dt, userdateformatter)) , (__ \ "displayend").read[string].fmap[datetime](dt => datetime.parse(dt, userdateformatter)) , (__ \ "created").read[datetime] , (__ \ "updated").read[datetime] )(banner)
Comments
Post a Comment