简体   繁体   English

Play Framework-从版本2.3.x迁移到2.4.1:如何确定JSON值是否为JsUndefined

[英]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. 我正在尝试将我的Pl​​ay应用程序从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.

相关问题 Play框架:从版本2.3.x迁移到2.4.1的问题 - Play Framework: Problems migrating from version 2.3.x to 2.4.1 播放2.3.x框架:如何拒绝所有非application / json请求 - play 2.3.x framework: How to decline all non application/json requests 如何在Play framework 2.3.x(Scala)中将case类转换为JSON? - How to convert case class to JSON in Play framework 2.3.x (Scala)? 播放2.3.x Scala - 如何在视图上显示json数据? - Play 2.3.x Scala - How to display json data on view? 在Play Framework 2.3.x(Scala)中为案例类定义交叉属性Json验证器 - Defining cross-attribute Json Validators for a case class in Play Framework 2.3.x (Scala) Scala,Play Framework - “JsUndefined(“VALUE1”不是对象)” - 有没有更好的方法将 json 反序列化为 Map[String, String]? - Scala, Play Framework - "JsUndefined("VALUE1" is not an object)" - is there a better way to deserialise json into Map[String, String]? 在grails 2.3.x中使用RestAPI JSON - Consuming a RestAPI JSON in grails 2.3.x Play 2.3.x中对Java8 ZonedDateTime的隐式json读写? - Implicit json Writes and Reads for Java8 ZonedDateTime in Play 2.3.x? Play Framework 2.4.1:如何从JsArray中删除元素 - Play Framework 2.4.1: How to remove an element from a JsArray Playframework 2.3.x:Json映射错误“此行有多个标记:找不到未应用的功能” - Playframework 2.3.x: Json mapping error “Multiple markers at this line: No unapply function found”
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM