【发布时间】:2017-11-11 13:04:06
【问题描述】:
我在使用默认参数和使用 Play Json Read 时遇到问题。 这是我的代码:
case class Test(action: String, storeResult: Option[Boolean] = Some(true), returndata: Option[Boolean] = Some(true))
val json =
"""
{"action": "Test"}"""
implicit val testReads: Reads[Test] =
(
(JsPath \\ "action").read[String](minLength[String](1)) and
(JsPath \\ "store_result").readNullable[Boolean] and
(JsPath \\ "returndata").readNullable[Boolean]
) (Test.apply _)
val js = Json.parse(json)
js.validate[Test] match {
case JsSuccess(a, _) => println(a)
case JsError(errors) =>
println("Here")
println(errors)
}
我希望最后得到的是
Test("Test", Some(true), Some(true))
但我得到了:
Test("Test",None,None)
为什么会这样?如果我没有在 json 中提供参数,为什么它没有默认值?如何实现我想要的?
【问题讨论】:
标签: json scala playframework playframework-2.0