Scala,Spray - 解组到列表 [Int]
Scala, Spray - Unmarshalling to List[Int]
我正在尝试从 JSON api 获取整数列表 [1,2,3,...]
,但我发现转换为通用类型无效,Spray 文档仅显示解组到特定目的。任何想法如何让它工作/如何 google 得到我的答案?
API 端点:https://hacker-news.firebaseio.com/v0/newstories.json
import java.io.IOException
import akka.actor.ActorSystem
import akka.http.scaladsl.Http
import akka.http.scaladsl.unmarshalling.Unmarshal
import akka.stream.ActorMaterializer
import akka.stream.scaladsl.{Flow, Sink, Source}
import akka.http.scaladsl.model.{HttpResponse, HttpRequest}
import com.typesafe.config.ConfigFactory
import akka.http.scaladsl.model.StatusCodes._
import spray.json.DefaultJsonProtocol._
import spray.json._
import akka.http.scaladsl.client.RequestBuilding
import scala.concurrent.Future
object NewsDaemon extends App{
val config = ConfigFactory.load()
implicit val system = ActorSystem()
implicit val executor = system.dispatcher
implicit val materializer = ActorMaterializer()
case class HNResultList(items: List[Int])
val hnConnectionFlow: Flow[HttpRequest, HttpResponse, Any] =
Http().outgoingConnectionTls(config.getString("services.hnApiHost"))
def hnApiRequest (request: HttpRequest) : Future[HttpResponse] = Source.single(request).via(hnConnectionFlow).runWith(Sink.head)
hnApiRequest(RequestBuilding.Get("/v0/topstories.json?print=pretty")).flatMap { response =>
response.status match {
case OK => Unmarshal(response.entity).to[List[Int]].map(println)
case _ => Unmarshal(response.entity).to[String].flatMap { entity =>
val error = s"HN API request failed with status code ${response.status} and entity $entity"
Future.failed(new IOException(error))
}
}
}
}
试试 akka.http.scaladsl.marshallers.sprayjson.SprayJsonSupport
:
object NewsDaemon extends App with SprayJsonSupport {
...
}
spray-json 库支持基本类型和 collections 'out-of-box'.
您可以在此处找到实现:
- 基本类型:
spray.json.BasicFormats
- collections:
spray.json.CollectionFormats
因此您不必为此类型编写自己的 JsonFormat (JsonReader/JsonWriter)。
有关如何将 spray-json 与 akka-http 集成的详细说明,您可以在此处找到:http://doc.akka.io/docs/akka-stream-and-http-experimental/2.0.3/scala/http/common/json-support.html
我正在尝试从 JSON api 获取整数列表 [1,2,3,...]
,但我发现转换为通用类型无效,Spray 文档仅显示解组到特定目的。任何想法如何让它工作/如何 google 得到我的答案?
API 端点:https://hacker-news.firebaseio.com/v0/newstories.json
import java.io.IOException
import akka.actor.ActorSystem
import akka.http.scaladsl.Http
import akka.http.scaladsl.unmarshalling.Unmarshal
import akka.stream.ActorMaterializer
import akka.stream.scaladsl.{Flow, Sink, Source}
import akka.http.scaladsl.model.{HttpResponse, HttpRequest}
import com.typesafe.config.ConfigFactory
import akka.http.scaladsl.model.StatusCodes._
import spray.json.DefaultJsonProtocol._
import spray.json._
import akka.http.scaladsl.client.RequestBuilding
import scala.concurrent.Future
object NewsDaemon extends App{
val config = ConfigFactory.load()
implicit val system = ActorSystem()
implicit val executor = system.dispatcher
implicit val materializer = ActorMaterializer()
case class HNResultList(items: List[Int])
val hnConnectionFlow: Flow[HttpRequest, HttpResponse, Any] =
Http().outgoingConnectionTls(config.getString("services.hnApiHost"))
def hnApiRequest (request: HttpRequest) : Future[HttpResponse] = Source.single(request).via(hnConnectionFlow).runWith(Sink.head)
hnApiRequest(RequestBuilding.Get("/v0/topstories.json?print=pretty")).flatMap { response =>
response.status match {
case OK => Unmarshal(response.entity).to[List[Int]].map(println)
case _ => Unmarshal(response.entity).to[String].flatMap { entity =>
val error = s"HN API request failed with status code ${response.status} and entity $entity"
Future.failed(new IOException(error))
}
}
}
}
试试 akka.http.scaladsl.marshallers.sprayjson.SprayJsonSupport
:
object NewsDaemon extends App with SprayJsonSupport {
...
}
spray-json 库支持基本类型和 collections 'out-of-box'.
您可以在此处找到实现:
- 基本类型:
spray.json.BasicFormats
- collections:
spray.json.CollectionFormats
因此您不必为此类型编写自己的 JsonFormat (JsonReader/JsonWriter)。
有关如何将 spray-json 与 akka-http 集成的详细说明,您可以在此处找到:http://doc.akka.io/docs/akka-stream-and-http-experimental/2.0.3/scala/http/common/json-support.html