简体   繁体   English

如何在Play + Scala中为自定义对象定义JSON writer?

[英]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 ? 您能否演示如何在Scala和Play Framework 2.1-RC2中为类定义json writer?

The documentation is quite confusing (and will be significant obstacle for wider adoption of Scala/Play by web-developer community). 该文档非常混乱(并且将成为Web开发人员社区广泛采用Scala / Play的重大障碍)。 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. 但是如果我将其放在object定义中,它也是不可编译的。 Play gives me following error 播放给我以下错误

not found: value __

Here is my Task.scala 这是我的Task.scala

http://pastebin.com/hG8F6Gqk http://pastebin.com/hG8F6Gqk

Well, turned out the problem was obvious. 好吧,原来问题很明显。 I forgot to import play.api.libs.functional.syntax._ . 我忘了导入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: _ ASCII字符#95:_

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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