简体   繁体   中英

Play Framework 2 JSON Reads, deserialization of one variable

I'm using Play Framework 2.4 and I'm trying to do a basic JSON deserialization with Reads but I get an error. Here is the code:

case class Config(action: String)

and somewhere,

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

I think the configReads is correctly formed but I get an IDE error on the "read" method call (symbol not defined), when I compile the code I get the following error:

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]
                          ^

but, if instead of trying to deserialize ONE argument I declare a class with TWO arguments in the constructor and I write the code to deserialize it, it works.

Does anybody know how to solve this?

Edit:

Digging in the depth of Google I found this for Play 2.1.x but I'm using the Json library for Play 2.4.1 so this problem should not be happening.

You can do like this:

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

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