Play 2.3 implicit json conversion causes null pointer exception

后端 未结 2 1130
孤城傲影
孤城傲影 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

提交回复
热议问题