简体   繁体   中英

How do I define json writer for custom object in Play + Scala?

Could you please demostrate how I can define json writer for my class in Scala and Play Framework 2.1-RC2 ?

The documentation is quite confusing (and will be significant obstacle for wider adoption of Scala/Play by web-developer community). For example not only it's unclear where exactly following code (taken from here ) should be located

implicit val taskWrites = (
  (__ \ "id").write[String] ~
  (__ \ "label").write[Boolean] ~
)(unlift(Task.unapply))

but it's also not compilable if I put it inside object definition. Play gives me following error

not found: value __

Here is my Task.scala

http://pastebin.com/hG8F6Gqk

Well, turned out the problem was obvious. I forgot to import play.api.libs.functional.syntax._ .

import play.api.libs.json._
import play.api.libs.functional.syntax._

The underscore you're using isn't a standard underscore.

Your underscore: __

ASCII Character #95: _

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM