【发布时间】:2017-03-10 21:02:44
【问题描述】:
我无法理解 JsPath 的示例代码并阅读文档
https://www.playframework.com/documentation/2.2.1/ScalaJsonCombinators
import play.api.libs.json._
import play.api.libs.functional.syntax._
问题 1 - 我们创建了一个自定义阅读器。 Reads 应该能够读取由 String、Float 和 List 组成的数据结构。但在下面的示例中,我们将其传递给 Json! Json 是如何转换为(String、Fload 和 List)的?
问题 2 - 我们使用 JsPath \"key1" 但我们在哪里传递了 JSON?
val customReads: Reads[(String, Float, List[String])] =
(JsPath \ "key1").read[String](email keepAnd minLength(5)) and
(JsPath \ "key2").read[Float](min(45)) and
(JsPath \ "key3").read[List[String]]
tupled
import play.api.libs.json.Json
val js = Json.obj(
"key1" -> "alpha",
"key2" -> 123.345F,
"key3" -> Json.arr("alpha", "beta")
)
res5: JsSuccess(("alpha", 123.345F, List("alpha", "beta")))
scala> customReads.reads(js)
customReads.reads(js).fold(
invalid = { errors => ... },
valid = { res =>
val (s, f, l): (String, Float, List[String]) = res
...
}
)
【问题讨论】:
标签: json playframework