如何使用defaultFormats将对象从json序列化到json?
我有一个这样的代码如何使用defaultFormats将对象从json序列化到json?,json,scala,spray,spray-json,Json,Scala,Spray,Spray Json,我有一个这样的代码 import akka.actor.{Props, ActorRef, Actor} import akka.util.Timeout import org.json4s.DefaultFormats import spray.routing.HttpService import spray.httpx.Json4sSupport import scala.concurrent.Await import scala.concurrent.duration._ import a
import akka.actor.{Props, ActorRef, Actor}
import akka.util.Timeout
import org.json4s.DefaultFormats
import spray.routing.HttpService
import spray.httpx.Json4sSupport
import scala.concurrent.Await
import scala.concurrent.duration._
import akka.pattern.ask
/**
* Created by mihaildoronin on 06.10.15.
*/
class ProcessesService() extends Actor with HttpService {
def actorRefFactory = context
def receive = runRoute(route)
val systemActor = context.actorOf(Props[SystemActor])
implicit val json4sFormats = DefaultFormats
implicit val timeout = Timeout(5 seconds)
val route = path("processes") {
get {
parameters('fromId.as[Int], 'count.as[Int]).as(GetProcessesCommand) { command =>
complete {
val response = Await.result(systemActor ? command, timeout.duration).asInstanceOf[CursoredResponse]
response
}
}
} ~
post {
entity(as[RegisterProcessCommand]) { command =>
complete {
val result = Await.result(systemActor ? command, timeout.duration).asInstanceOf[Long]
}
}
}
} ~
pathPrefix("processes" / IntNumber) { id =>
complete {
val process = Await.result(systemActor ? GetProcessCommand(id), timeout.duration).asInstanceOf[Process]
process
}
}
}
它给了我这样的错误
Error:(28, 11) type mismatch;
found : CursoredResponse
required: spray.httpx.marshalling.ToResponseMarshallable
response
^
但在这种情况下,类似的代码似乎也能起作用。我已经重写了JSON4SFormat并使其隐式化,我使用的是case类,我在这里缺少什么?
我是scala和spray的新手,所以我不太清楚。对于您的课程,您应该提供封送器和解封器。来自Spray Json教程:
import spray.json.DefaultJsonProtocol
import spray.httpx.unmarshalling._
import spray.httpx.marshalling._
然后让你对Json做出反应
import MyJsonProtocol._
import spray.httpx.SprayJsonSupport._
import spray.util._
object CursoredResponseProtocol extends DefaultJsonProtocol {
implicit val CursoredResponseFormat = jsonFormat3(CursoredResponse)
}
以下是:我认为您只需要将spray.httpx.Json4sSupport混合到您的类中。所以
class ProcessesService() extends Actor with HttpService with Json4sSupport {
...
}
我最初提供了一个使用spray json方法的通用答案。但在重读之后,我意识到我回答了错误的问题。我会在下面留下我的原始答案,以防有人发现它有用
我首选的方法是创建一个特性,扩展Spray的DefaultJsonProtocol和SprayJsonSupport。在这里面,我为我的所有case类创建了对RootJsonFormats的隐式引用。然后我就把这个特性混合到我定义路线的类或特性中
对我来说,这是最简单的方法来组织事情,并为我的Spray路由以及其他组件提供所需的(取消)封送功能,在这些组件中,我使用Spray client与基于HTTP的服务提供商集成,这些服务提供商生成并使用JSON
假设您有一个返回FooBar的端点。下面是我们要封送到JSON的case类:
case class FooBar(withNuts: Boolean = true, kingSize: Boolean = false)
我们定义了一个trait,其中包含自定义类的RootJsonFormat——在本例中是一个FooBar——它将能够读写JSON
import spray.json.{RootJsonFormat, DefaultJsonProtocol}
import spray.httpx.SprayJsonSupport
trait MyAppJsonProtocol extends DefaultJsonProtocol with SprayJsonSupport {
implicit val FooBarFormat: RootJsonFormat[FooBar] = jsonFormat2(FooBar)
}
然后将MyAppJsonProtocol混合到路由中
trait FooBarRoute extends HttpService with MyAppJsonProtocol {
implicit def executionContext = actorRefFactory.dispatcher
val fooRoute = {
(get & path("foobar")) {
complete(FooBar())
}
}
}
如果您有其他需要(取消)封送的类,只需为这些类中的每一个添加另一个隐式RootJsonFormat到MyAppJsonProtocol特性