玩ws如何以类型安全的方式映射响应

时间:2016-05-08 07:39:28

标签: json scala serialization playframework

如何以类型安全的方式映射play-ws异步Web请求的JSON响应?

private def webRequest(): Map[String, Any] = {

    case class QuantileResult(val_05: Double, val_25: Double, val_50: Double, val_75: Double, val_90: Double)

    object QuantileResult {
      implicit val quantileResultFormat = Json.format[QuantileResult]
    }

    implicit val quantileReads = Json.reads[QuantileResult]

    val payload = Json.obj(
      "json" -> Json.parse("""{"type":1,"x":[1,2,3,4,5,6,7,8,9,10],"probs":[0.05,0.25,0.75,0.95]}"""),
      "windowWidth" -> JsNumber(11)
    )

    wsClient.url("http://public.opencpu.org/ocpu/library/stats/R/quantile/json")
      .withHeaders("Content-Type" -> "application/json")
      .post(payload)
      .map { wsResponse =>
        if (!(200 to 299).contains(wsResponse.status)) {
          sys.error(s"Received unexpected status, open-cpu error ${wsResponse.status} : ${wsResponse.body}")
        }
        println(s"OK, received ${wsResponse.body}")
        wsResponse.json.validate[QuantileResult]
        match {
          case JsSuccess(arrOut: QuantileResult, _) => QuantileResult //TODO how to perform mapping here to Map[String, Any]
          case e: JsError => JsError.toFlatForm(e) // TODO empty array does not work here otherwise future[object] does not conform to expected return type of Map[String, Any]
        }
      }
  }
  1. 如何在此处执行映射到Map [String,Any]
  2. 空地图在这里不起作用,否则未来[对象]不符合Map的预期返回类型[String,Any]
  3. 什么有效

     wsClient.url("url").withHeaders("Content-Type" -> "application/json").post(payload).map { wsResponse => result = wsResponse.body }
    
            val json = Json.parse(result)
            val mapper = new ObjectMapper() with ScalaObjectMapper
            mapper.registerModule(DefaultScalaModule)
            val returnValue = mapper.readValue[Map[String, Any]](result)
    

    但这似乎非常笨重,并不是异步。

1 个答案:

答案 0 :(得分:1)

解决方案是将方法签名更改为适当的未来Future[Seq[Map[String, Any]]]并通过Await.result(someMethod), 20.seconds)解析

映射执行类似

wsResponse.json.validate[Seq[OutlierResult]] match {
          case JsSuccess(result, _) => result.map(outlierRes => Map("period" -> outlierRes.period, "amount" -> outlierRes.amount, "outlier" -> outlierRes.outlier))
          case JsError(error) => throw new OutlierParseException(error.toString())
        }

即使这可以改进而不抛出异常