在播放框架递归JSON阅读器中关闭“前向引用”警告

时间:2015-05-09 19:44:42

标签: scala playframework

在Play框架(Scala)Web应用程序上,我解析了一些获得递归类型的JSON。为了实现这一点,我使用了读者的惰性引用,就像在play's official documentation上推荐的那样(向下滚动到“递归类型”)。

这有效,但我收到警告:

[info] Compiling 1 Scala source to /path-to-project/target/scala-2.11/classes...
[warn] /path-to-project/app/controllers/JSONFormats.scala:116: Reference to uninitialized value layerTreeNodeFormat
[warn]   val folderNodeFormat = Format(folderNodeReads, Json.writes[FolderNode])
[warn]                                                             ^
[warn] one warning found

有什么方法可以关掉这个警告吗?我看过Scala的@unchecked,但我不确定如何(如果有的话)可以在这里应用。

谢谢!

编辑:以下是代码的相关部分。 JSON解析器解析异构的地图图层树。一些节点(“层”)是叶子,而其他节点(“文件夹”)可以包含层和其他文件夹(因此递归)。在Scala方面,有一个抽象基类LayerTreeNode和两个具体案例类:LayerNodeFolderNode

object ProjectJSONFormats {

  // omitted code...

  /** Turns the tuple parsed from JSON into a FolderNode. */
  def tupleToFolder( id:String, jsType:String, name:String, children:Seq[LayerTreeNode] ) = FolderNode(id, name, children)

  val folderNodeReads: Reads[FolderNode] = (
    (JsPath \ "id").read[String] and
      (JsPath \ "type").read[String] and
      (JsPath \ "title").readNullable[String].map( _.getOrElse("") ) and
      (JsPath \ "children").lazyReadNullable(Reads.seq[LayerTreeNode](layerTreeNodeReads)).map( _.getOrElse(Seq[LayerTreeNode]()))
    )( tupleToFolder _)

  val folderNodeFormat = Format(folderNodeReads, Json.writes[FolderNode])

  implicit val layerNodeFormat = Json.format[LayerNode]

  val layerTreeNodeReads: Reads[LayerTreeNode] = new Reads[LayerTreeNode] {
    override def reads(json: JsValue): JsResult[LayerTreeNode] = {
      if ( (json\"type").as[String] == "folder" ) {
        folderNodeFormat.reads(json)
      } else {
        layerNodeFormat.reads(json)
      }
    }
  }
  val layerTreeNodeWrites: Writes[LayerTreeNode] = new Writes[LayerTreeNode] {
    override def writes(o: LayerTreeNode): JsValue = o match {
      case f:FolderNode => folderNodeFormat.writes(f)
      case l:LayerNode  => layerNodeFormat.writes(l)
    }
  }
  implicit val layerTreeNodeFormat:Format[LayerTreeNode] = Format( layerTreeNodeReads, layerTreeNodeWrites )

}

1 个答案:

答案 0 :(得分:2)

编译器完全证明了警告,所以不要试图忽略它。你没有分享你的案例类结构,所以我必须做出一些猜测,但无论如何问题都会出现。 Reads可能是安全的,但Writes不是。

sealed abstract class LayerTreeNode(id: String, name: String)
case class FolderNode(id: String, name: String, children: Seq[LayerTreeNode]) extends LayerTreeNode(id, name)
case class LayerNode(id: String, name: String) extends LayerTreeNode(id, name)

val folder = FolderNode("ABC", "Parent", Seq(LayerNode("DEF", "Child")))

import ProjectJSONFormats._

scala> Json.toJson(folder)
java.lang.NullPointerException
  at play.api.libs.json.Json$.toJson(Json.scala:108)
  at play.api.libs.json.DefaultWrites$$anon$3$$anonfun$writes$2.apply(Writes.sc
  ... 43 elided

发生什么事了?正如编译器警告我们的那样,Json.writes[FolderNode]需要隐式Writes[LayerNode]Format[LayerNode]。但layerNodeFormat在<{em> Json.writes[FolderNode]调用之后定义了,这意味着我们可以看到它,但它未初始化。现在writes的{​​{1}}方法让NPE等待在最糟糕的时刻出现。

修复很简单,只需让你的folderNodeFormat懒惰。即:

Writes

它有效:

lazy val folderNodeFormat = Format(folderNodeReads, Json.writes[FolderNode])

lazy val layerTreeNodeWrites: Writes[LayerTreeNode] = ...

一般问题是初始化顺序,如果你不密切关注,肯定会咬你。我在this answer写了更多关于它的文章。这同样适用于scala> Json.toJson(folder) res10: play.api.libs.json.JsValue = {"id":"ABC","name":"Parent","children":[{"id":"DEF","name":"Child"}]} Reads

另外,使用Writes也不安全。如果(json \ "type").as[String] == "folder")实际上不是"type",则会抛出异常。

String

最好使用val js = Json.parse("""{ "id": "ABC", "name": "parent", "type": 1, "children": [ {"id": "DEF", "name": "child", "type": "leaf"} ] }""") scala> js.validate[LayerTreeNode] play.api.libs.json.JsResultException: JsResultException(errors:List((,List(ValidationError(error.expected.jsstring,WrappedArray()))))) ... 43 elided // We probably don't want this to happen! validate

flatMap

现在,如果我们 val layerTreeNodeReads: Reads[LayerTreeNode] = new Reads[LayerTreeNode] { override def reads(json: JsValue): JsResult[LayerTreeNode] = { (json \ "type").validate[String] flatMap { case "folder" => folderNodeFormat.reads(json) case _ => layerNodeFormat.reads(json) } } } 我们得到validate[LayerTreeNode]而不是抛出异常,那么最糟糕的事情可能会发生。

JsError