scala - Defining Writes[...] for a Case Class w/ Tuple -



scala - Defining Writes[...] for a Case Class w/ Tuple -

given range class:

case class range(things: (string, string))

i tried implement writes[range] play json docs illustration on creature.

implicit val rangewrites: writes[range] = (jspath \ "things").write( (jspath \ "string").write[string] , (jspath \ "string").write[string] tupled )(unlift(range.unapply))

however, got:

[error] jsontest.scala 19 not find implicit value parameter app: play.api.libs.functional. applicative[play.api.libs.json.owrites] [error] (jspath \ "items").write(

how can fill in (jspath \ "items").write(? in docs' example, there doesn't appear type required.

the issue here similar 1 shows in this question: can't utilize applicative syntax on plain old writes (or owrites)—you need builder.

the solution similar here, instead of map need contramap, since writes contravariant functor, not functor:

implicit val rangewrites: writes[range] = (jspath \ "things").write( (jspath \ "string1").write[string] , (jspath \ "string2").write[string] tupled ).contramap(unlift(range.unapply))

also note i've changed keys string1 , string2—if utilize same key you're not going expect.

scala playframework-2.0

Comments

Popular posts from this blog

php - Android app custom user registration and login with cookie using facebook sdk -

django - Access session in user model .save() -

php - .htaccess Multiple Rewrite Rules / Prioritizing -