2014-02-09 57 views
3

我我得到了一個錯誤:噴霧JSON解析和NullPointerException異常

spray.json.ProductFormats$class.productElement2Field NullPointerException 

這裏是我的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 

我想這可能是因爲在類的重載構造RequestHolder。

UPD: JSON的內容是:

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

回答

1

我發現在官方噴霧郵件列表的解決方案,該解決方案是使用選項[MyClass的=無構造函數,而不是替代的構造方法nulls,例如,我的班級定義如下:

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

現在解析工作正常!