1

我根据这个例子从 SprayJsonSupport 传递到 argonaut 。经过一些代码修改:

object ElevationJsonProtocol extends DefaultJsonProtocol {
  implicit val locationCodec: CodecJson[Elevation] = casecodec2(Elevation, Elevation.unapply)("location", "elevation")
  implicit val elevationCodec: CodecJson[Location] = casecodec2(Location, Location.unapply)("lat", "lng")
  implicit def googleApiResultCodec: CodecJson[GoogleApiResult] = casecodec2(GoogleApiResult, GoogleApiResult.unapply)("status", "results")
}

我收到了这个错误

错误:(41、42)方法解组的参数不足:(隐式证据$1:spray.httpx.unmarshalling.FromResponseUnmarshaller[GoogleApiResult])spray.http.HttpResponse => GoogleApiResult。未指定值参数evidence$1。val pipeline = sendReceive ~> unmarshal[GoogleApiResult] ^

我看一下 unmarshall 方法:

 def unmarshal[T](implicit evidence$1 : spray.httpx.unmarshalling.FromResponseUnmarshaller[T]) : scala.Function1[spray.http.HttpResponse, T] 

如何添加隐式参数?为什么我在 sprayJsonSupport 上没有出现这样的错误?

孔代码:

import spray.httpx.unmarshalling.FromResponseUnmarshaller

import scala.util.{Success, Failure}
import scala.concurrent.duration._
import akka.actor.ActorSystem
import akka.pattern.ask
import akka.event.Logging
import akka.io.IO
import spray.json.{JsonFormat, DefaultJsonProtocol}
import spray.can.Http
import spray.httpx.SprayJsonSupport
import spray.client.pipelining._
import spray.util._
import argonaut._, Argonaut._

case class Elevation(location: Location, elevation: Double)
case class Location(lat: Double, lng: Double)
case class GoogleApiResult(status: String, results: List[Elevation])

object ElevationJsonProtocol extends DefaultJsonProtocol {
  implicit val locationCodec: CodecJson[Elevation] = casecodec2(Elevation, Elevation.unapply)("location", "elevation")
  implicit val elevationCodec: CodecJson[Location] = casecodec2(Location, Location.unapply)("lat", "lng")
  implicit def googleApiResultCodec: CodecJson[GoogleApiResult] = casecodec2(GoogleApiResult, GoogleApiResult.unapply)("status", "results")
}

object Main extends App {
  // we need an ActorSystem to host our application in
  implicit val system = ActorSystem("simple-spray-client")
  import system.dispatcher // execution context for futures below
  val log = Logging(system, getClass)

  log.info("Requesting the elevation of Mt. Everest from Googles Elevation API...")

  import ElevationJsonProtocol._

  val pipeline = sendReceive ~> unmarshal[GoogleApiResult]

  val responseFuture = pipeline (
    Get("http://maps.googleapis.com/maps/api/elevation/json?locations=27.988056,86.925278&sensor=false")
    )

  responseFuture onComplete {
    case Success(GoogleApiResult(_, Elevation(_, elevation) :: _)) =>
      log.info("The elevation of Mt. Everest is: {} m", elevation)
      shutdown()

    case Success(somethingUnexpected) =>
      log.warning("The Google API call was successful but returned something unexpected: '{}'.", somethingUnexpected)
      shutdown()

    case Failure(error) =>
      log.error(error, "Couldn't get elevation")
      shutdown()
  }

  def shutdown(): Unit = {
    IO(Http).ask(Http.CloseAll)(1.second).await
    system.shutdown()
  }
}
4

1 回答 1

1

我并没有真正使用 argonaut,我使用 play json with spray。但乍一看,似乎需要为您的隐式编解码器引入 argonaut 支持 trait/import 以转换为 spray 的解组器(play json 需要类似的东西)。

https://github.com/dwhjames/argonaut-spray

这个库似乎是你想要的。您的隐式和导入看起来不错,拉入库应该可以解决您的问题。

于 2016-02-20T13:50:11.557 回答