Json spray and NullPointerException

I have an error:

spray.json.ProductFormats$class.productElement2Field NullPointerException

Here is my code for json deserialization:

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 assume this may be due to overloaded constructors in the RequestHolder class.

UPD: json content:

{"requestStatus":{"status":0,"message":""},"loginInfo":{"userid":0,"email":"123","password":"123","rememberme":false}}
+3
source share
1 answer

I found the solution in the official mailing list, the solution uses the [MyClass] = None option in the constructor, instead overriding the constructors with zeros, for example, the definition of my class looks like this:

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

and now the parsing works fine!

+1
source

All Articles