jsonscalanullpointerexceptionspray-json

Spray json parsing and NullPointerException


i'm got an error:

spray.json.ProductFormats$class.productElement2Field NullPointerException

Here is my code for json deserialisation:

object DomainJsonProtocol extends DefaultJsonProtocol {
implicit val loginInfoFormat = jsonFormat(LoginInfo, "userid", "email", "password", "rememberme")
implicit val requestStatusFormat = jsonFormat(RequestStatus, "status", "message")
implicit val requestHolderFormat = jsonFormat(RequestHolder, "requestStatus", "loginInfo")
}

case class RequestHolder(requestStatus : RequestStatus, loginInfo: LoginInfo) {
  def this(requestStatus : RequestStatus) = this(requestStatus, null)
}
case class LoginInfo(userid: Int, email: String, password: String, rememberme: Boolean)
case class RequestStatus(status : Int, message: String)

val requestHolder = content.asJson.convertTo[RequestHolder] //The error is hereHere is 

I guess it could be because of the overloaded constructors in the class RequestHolder.

upd: The json content is:

{"requestStatus":{"status":0,"message":""},"loginInfo":{"userid":0,"email":"123","password":"123","rememberme":false}}

Solution

  • I'm found solution in the official spray mailing list, the solution is use Option[MyClass] = None in the constructor, instead the override constructors with nulls, for instance, my class definition look like:

    case class RequestHolder(requestStatus : RequestStatus,  loginInfo: Option[LoginInfo] = None)
    

    and now parsing works fine!