I hit a route of my microservice. I have common API to talk to any microservice and using jerkson JSON library.
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)
}
}
Now I have a singleton which has the list of methods to hit routes of another service.
object PurchasesTrait extends ApiResource {
def getData(payload: String): ApiResult[ProductPurchaseResponse] =
r[ProductPurchaseResponse](POST, Url.core + "reports/productspurchased", payload)
}
}
I have two case class,
case class QualifyingProductSummary(upcCode: String, description: Option[String], purchase: Option[Double]) case class ProductPurchaseResponse(qualifyingProducts:List[QualifyingProducts], count: Int)
purchaseTrait.getData(payload) will give me the ApiResult(json, Nnone)
json:
{"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
To get the data out of ApiResult when I use either on it,
It is not able to parse it in ProductPurchaseResponse because of QualifyingProducts. So how can I achieve it using the same library? I want the generic method to do the same for other nested JSON as well.
Thanks in advance.