Play 2.3 implicit json conversion causes null pointer exception

后端 未结 2 1119
孤城傲影
孤城傲影 2021-01-01 22:20

I\'m trying to parse json into my case class DealFormMap

case class DealFormMap(limit: Option[Int], filter: Option[DealFormFilterMap])
case clas         


        
相关标签:
2条回答
  • 2021-01-01 22:50

    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

    0 讨论(0)
  • 2021-01-01 22:52

    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`
    
    0 讨论(0)
提交回复
热议问题