Play Framework 2 JSON 读取,反序列化一个变量

Play Framework 2 JSON Reads, deserialization of one variable

我正在使用 Play Framework 2.4,我正在尝试使用 Reads 进行基本的 JSON 反序列化,但出现错误。这是代码:

case class Config(action: String)

在某处,

implicit val configReads: Reads[Config] = (
  (__ \ "action").read[String]
  )(Config.apply _)

我认为 configReads 的格式正确,但我在 "read" 方法调用(未定义符号)时收到 IDE 错误,当我编译代码时收到以下错误:

Error:(30, 27) overloaded method value read with alternatives:
  (t: String)play.api.libs.json.Reads[String] <and>
  (implicit r: play.api.libs.json.Reads[String])play.api.libs.json.Reads[String]
 cannot be applied to (String => wings.common.json.Config)
      (__ \ "action").read[String]
                          ^

但是,如果我没有尝试反序列化一个参数,而是在构造函数中声明一个带有两个参数的 class,然后编写代码来反序列化它,它就可以工作。

有人知道怎么解决吗?

编辑:

深入挖掘 Google 我发现 this 适用于 Play 2。1.x 但我使用的是 Json 库适用于 Play 2.4.1,所以这问题应该不会发生。

你可以这样做:

implicit val configReads: Reads[Config] = (
  (__ \ "action").read[String]
  ) map Config.apply