播放框架:JSON 阅读单属性案例 class
Play framework: JSON Reads for a single-attribute case class
我正在尝试为包含单个属性的案例 class 创建 JSON 隐式读取,但我收到错误 "Reads[Nothing] doesn't conform to expected type"。这是代码:
import play.api.libs.functional.syntax._
import play.api.libs.json.Reads._
import play.api.libs.json.{JsPath, Json, Reads}
case class Feedback(message: String)
object Feedback {
implicit val reads: Reads[Feedback] = (
(JsPath \ "message").read[String](maxLength[String](2000))
)(Feedback.apply _)
}
为什么这不起作用?如果我将额外的属性添加到案例 class 和多个 .read
调用与 and
连接它工作......
Json 组合器不适用于单字段情况 class.
您可以执行以下操作:
import play.api.libs.json.Reads._
import play.api.libs.json.{__, Reads}
case class Feedback(message: String)
object Feedback {
implicit val reads: Reads[Feedback] = (__ \ "message")
.read[String](maxLength[String](2000)).map {message => Feedback(message)}
}
这是因为当前宏实现的限制。您可以在这里阅读更多相关信息:Pacal is the writer of this API
我正在尝试为包含单个属性的案例 class 创建 JSON 隐式读取,但我收到错误 "Reads[Nothing] doesn't conform to expected type"。这是代码:
import play.api.libs.functional.syntax._
import play.api.libs.json.Reads._
import play.api.libs.json.{JsPath, Json, Reads}
case class Feedback(message: String)
object Feedback {
implicit val reads: Reads[Feedback] = (
(JsPath \ "message").read[String](maxLength[String](2000))
)(Feedback.apply _)
}
为什么这不起作用?如果我将额外的属性添加到案例 class 和多个 .read
调用与 and
连接它工作......
Json 组合器不适用于单字段情况 class.
您可以执行以下操作:
import play.api.libs.json.Reads._
import play.api.libs.json.{__, Reads}
case class Feedback(message: String)
object Feedback {
implicit val reads: Reads[Feedback] = (__ \ "message")
.read[String](maxLength[String](2000)).map {message => Feedback(message)}
}
这是因为当前宏实现的限制。您可以在这里阅读更多相关信息:Pacal is the writer of this API