【发布时间】:2015-08-25 16:10:04
【问题描述】:
考虑一个可以返回两个json作为响应的http服务:
成功
{
"yourField":"value"
}
失败
{
"errorCode": 3
}
为了处理这些 json,我需要创建 2 个案例类 case class RespSucc(yourField:String) 和
case class RespFail(errorCode:Int).
现在我不得不这样做:
//unmarshal is spray.httpx.ResponseTransformation#unmarshal
if (response.entity.asString.contains("errorCode")) {
unmarshal[RespSucc].apply(response)
}
else {
unmarshal[RespFail].apply(response)
}
是否有无需任何if 即可自动解析这些类的api?例如。 unmarshaller 可以查看 json 字段并选择合适的案例类吗?
【问题讨论】:
标签: json scala marshalling unmarshalling spray