[英]Play Framework - migration from version 2.3.x to 2.4.1: how to determine whether a JSON value is JsUndefined
I'm trying to migrate my Play application from version 2.3.8 to 2.4.1... but I'm still facing some issues. 我正在尝试将我的Play应用程序从2.3.8版本迁移到2.4.1 ...,但仍然遇到一些问题。
I use play-json-zipper
, which always returns a JsValue
... and thus the following statement doesn't work with Play 2.4.1: 我使用
play-json-zipper
,它始终返回JsValue
...,因此以下语句不适用于Play 2.4.1:
import play.api.libs.json._
import play.api.libs.json.extensions._
val json = JsObject(...)
val v = json.get(__ \ 'key) match { // json.get returns a JsValue
case _: JsUndefined => Seq[JsValue]()
case js: JsValue => js.as[JsArray].value
}
In Play 2.4.1 JsUndefined
doesn't inherit from JsValue
(they both inherit from JsReadable
). 在Play 2.4.1中,
JsUndefined
不会从JsValue
继承(它们都从JsReadable
继承)。 My code is quite huge... and removing play-json-zipper
would imply a considerable effort. 我的代码非常庞大...删除
play-json-zipper
意味着需要付出很大的努力。 Is there a workaround to determine whether the JsValue
returned by json.get(__ \\ 'key)
is undefined? 有一种解决方法,以确定是否
JsValue
返回由json.get(__ \\ 'key)
未定义?
That's an interesting one. 真有趣。 The commit says "JsUndefined should not be a JsValue".
提交显示 “ JsUndefined不应为JsValue”。 That's fair enough, I think it makes better sense for it to use the Some/None approach instead.
这很公平,我认为使用Some / None方法更有意义。
If you want to do exactly what you wrote there, can use something like this: 如果您想完全按照那里的样子做,可以使用如下代码:
val v: Seq[JsValue] = (json \ "key").toOption.map {
case JsArray(els) => els
case _ => Seq[JsValue]()
} getOrElse(Seq[JsValue]())
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.