Play 2.3 implicit json conversion causes null pointer exception

牧云@^-^@ 提交于 2019-11-30 11:46:49

The problem is the initialization order. dealFormMapReads depends on the implicit dealFormFilterMapReads, which isn't defined until after. It will compile because the implicit is found, even though it hasn't been initialized, so dealFormMapReads is read as null, which eventually causes the NPE.

Lazily loading will fix it:

implicit val dealFormMapReads: Reads[DealFormMap] = (
     (JsPath \ "limit").readNullable[Int] and
     (JsPath \ "filter").lazyReadNullable[DealFormFilterMap](dealFormFilterMapReads)
)(DealFormMap)

Or you could just swap the order in which the Reads are defined.


The NPE thrown here is similar to this example:

case class A(i: Int)

object Test {
     val test = a.toString
     val a = A(1)
}

// Compiles up to here

Test.test // throws NPE, because `a` was not initialized before `test`

The problem is the order as mentioned by LimbSoup. But it's worth noting that with using Json macros you can achieve the same result with

import play.api.libs.json._

implicit val dealFormFilterMapReads: Reads[DealFormFilterMap] = Json.reads[DealFormFilterMap]

implicit val dealFormMapReads: Reads[DealFormMap] = Json.reads[DealFormMap]

Note that by using this macros, if you change the order

implicit val dealFormMapReads: Reads[DealFormMap] = Json.reads[DealFormMap]

implicit val dealFormFilterMapReads: Reads[DealFormFilterMap] = Json.reads[DealFormFilterMap]   

you will get the following helpful warning:

 Reference to uninitialized value dealFormFilterMapReads
[warn] implicit val dealFormMapReads: Reads[DealFormMap] = Json.reads[DealFormMap]
[warn]                                                                 ^
[warn] one warning found

which you can fix (again as mentioned by LimpSoup) by making dealFormFilterMapReads lazy or reordering which makes more sense in this case.

Disclaimer

Using Json macro inception in play needs Scala 2.10 and is supported on Play versions after 2.1.0

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!