import com.codahale.jerkson.Json
case class ApiResult[T](
private val content: Option[String],
private val error: Option[Throwable]
) {
def getRaw: String = this.content.getOrElse("null")
def get()(implicit m: Manifest[T]): T = {
Json.parse[T](this.content.get)
}
def either(implicit m: Manifest[T]): Either[Throwable, T] = error match {
case None => Right(get)
case Some(e) => Left(e)
}
}
现在我有了一个singleton,它有一系列方法来访问另一个服务的路由。
object PurchasesTrait extends ApiResource {
def getData(payload: String): ApiResult[ProductPurchaseResponse] =
r[ProductPurchaseResponse](POST, Url.core + "reports/productspurchased", payload)
}
}
我有两个案例课,
case类QualifyingProductSummary(upcCode:String,description:Option[String],purchase:Option[Double])
案例类ProductPurchaseResponse(qualifyingProducts:列表[合格产品],计数:Int)
purchaseTrait.getData(payload) will give me the ApiResult(json, Nnone)
{"qualifyingProductSummary":[{"upcCode":"6410077902","description":"Mini-Wheats Original","purchase":15.2},{"upcCode":"6410044886","description":"Corn Pops","purchase":13.7},{"upcCode":"041570055830","description":"Unsweetened Vanilla Beverage ","purchase":13.5},{"upcCode":"626027087802","description":"Organic Almond Milk Original","purchase":12.5}],"totalQualifyingProducts":19}
val data = purchaseTrait.getData(payload).either
当我使用ApiResult中的任何一个时,要从它中获取数据,
它无法在中解析它
因为
合格产品
我希望泛型方法也对其他嵌套的JSON执行相同的操作。