喷Json: 找不到Json编写器或Json格式类型class
Spray Json: Cannot find JsonWriter or JsonFormat type class for
我有 class 我放置 http 请求的地方。这是我需要 json.
的地方
package com.webtrekk.cometd
import com.webtrekk.json._
import spray.json._
import scala.concurrent.Future
import scala.concurrent.duration._
import akka.util.Timeout
import akka.pattern.ask
import akka.io.IO
import spray.can.Http
import scala.util.{ Success, Failure }
import akka.actor.ActorSystem
import spray.http._
import HttpMethods._
import spray.http.HttpHeaders._
import spray.http.ContentTypes._
import spray.http.MediaTypes._
import scala.concurrent.Future
class Handshake(var url:String, var token:String){
val handshake = com.webtrekk.json.Handshake
implicit val system: ActorSystem = ActorSystem("salesforce")
implicit val timeout: Timeout = Timeout(15.seconds)
def execute() {
val contentType = new ContentType(MediaTypes.`application/json`, Option(HttpCharsets.`UTF-8`))
val httpHeader = List(RawHeader("Authorization", "Bearer "+token))
val httpEntity = HttpEntity(
contentType,
handshake.toJson)
val responseFuture: Future[HttpResponse] =
(IO(Http) ? HttpRequest(
method = POST,
uri = url,
entity = httpEntity,
headers = httpHeader)).mapTo[HttpResponse]
}
}
这是握手 Json 协议
package com.webtrekk.json
import spray.json._
import DefaultJsonProtocol._
class Handshake(
val channel: String = "/meta/handshake",
val id: Int = 1,
val supportedConnectionTypes: Vector[String] = Vector("long-polling"),
val version: String = "1.0",
val minimumVersion: String = "1.0") {
var successful: Boolean = false
var clientId:String = ""
}
class HandshakeProtocol extends DefaultJsonProtocol {
implicit object HandshakeJsonFormat extends JsonFormat[Handshake] {
def write(h: Handshake): JsValue =
JsObject(
"channel" -> JsString(h.channel),
"id" -> JsNumber(h.id),
"supportedConnectionTypes" -> JsArray(h.supportedConnectionTypes.map(value => JsString(value))),
"version" -> JsString(h.version),
"minimumVersion" -> JsString(h.minimumVersion)
)
def read(value: JsValue): Handshake =
value.asJsObject.getFields(
"channel",
"id",
"supportedConnectionTypes",
"version",
"minimumVersion",
"successful",
"cliendId"
) match {
case Seq(
JsString(channel),
JsNumber(id),
JsArray(supportedConnectionTypes),
JsString(version),
JsString(minimumVersion),
JsBoolean(successful),
JsString(clientId)
) => {
var handshake = new Handshake(
channel,
id.toInt,
supportedConnectionTypes.map(value => value.toString),
version,
minimumVersion)
handshake.successful = successful
handshake.clientId = clientId
handshake
}
case _ => throw new DeserializationException("JSON Error: Handshake Json Structure isn't correct!")
}
}
}
我还扩展了RootJson格式。但这也行不通。
我不知道为什么 toJson 方法不起作用。在执行时我得到错误:找不到 JsonWriter 或 JsonFormat type class for Handshake.this.handshake.type
我使用 Scala 2.11.7
我添加了
implicit val handshakeFormat = HandshakeJsonFormat
握手 class。这行得通。这样,HandhshakeJsonFormat 就在握手范围内。感谢tryx
我有 class 我放置 http 请求的地方。这是我需要 json.
的地方package com.webtrekk.cometd
import com.webtrekk.json._
import spray.json._
import scala.concurrent.Future
import scala.concurrent.duration._
import akka.util.Timeout
import akka.pattern.ask
import akka.io.IO
import spray.can.Http
import scala.util.{ Success, Failure }
import akka.actor.ActorSystem
import spray.http._
import HttpMethods._
import spray.http.HttpHeaders._
import spray.http.ContentTypes._
import spray.http.MediaTypes._
import scala.concurrent.Future
class Handshake(var url:String, var token:String){
val handshake = com.webtrekk.json.Handshake
implicit val system: ActorSystem = ActorSystem("salesforce")
implicit val timeout: Timeout = Timeout(15.seconds)
def execute() {
val contentType = new ContentType(MediaTypes.`application/json`, Option(HttpCharsets.`UTF-8`))
val httpHeader = List(RawHeader("Authorization", "Bearer "+token))
val httpEntity = HttpEntity(
contentType,
handshake.toJson)
val responseFuture: Future[HttpResponse] =
(IO(Http) ? HttpRequest(
method = POST,
uri = url,
entity = httpEntity,
headers = httpHeader)).mapTo[HttpResponse]
}
}
这是握手 Json 协议
package com.webtrekk.json
import spray.json._
import DefaultJsonProtocol._
class Handshake(
val channel: String = "/meta/handshake",
val id: Int = 1,
val supportedConnectionTypes: Vector[String] = Vector("long-polling"),
val version: String = "1.0",
val minimumVersion: String = "1.0") {
var successful: Boolean = false
var clientId:String = ""
}
class HandshakeProtocol extends DefaultJsonProtocol {
implicit object HandshakeJsonFormat extends JsonFormat[Handshake] {
def write(h: Handshake): JsValue =
JsObject(
"channel" -> JsString(h.channel),
"id" -> JsNumber(h.id),
"supportedConnectionTypes" -> JsArray(h.supportedConnectionTypes.map(value => JsString(value))),
"version" -> JsString(h.version),
"minimumVersion" -> JsString(h.minimumVersion)
)
def read(value: JsValue): Handshake =
value.asJsObject.getFields(
"channel",
"id",
"supportedConnectionTypes",
"version",
"minimumVersion",
"successful",
"cliendId"
) match {
case Seq(
JsString(channel),
JsNumber(id),
JsArray(supportedConnectionTypes),
JsString(version),
JsString(minimumVersion),
JsBoolean(successful),
JsString(clientId)
) => {
var handshake = new Handshake(
channel,
id.toInt,
supportedConnectionTypes.map(value => value.toString),
version,
minimumVersion)
handshake.successful = successful
handshake.clientId = clientId
handshake
}
case _ => throw new DeserializationException("JSON Error: Handshake Json Structure isn't correct!")
}
}
}
我还扩展了RootJson格式。但这也行不通。 我不知道为什么 toJson 方法不起作用。在执行时我得到错误:找不到 JsonWriter 或 JsonFormat type class for Handshake.this.handshake.type
我使用 Scala 2.11.7
我添加了
implicit val handshakeFormat = HandshakeJsonFormat
握手 class。这行得通。这样,HandhshakeJsonFormat 就在握手范围内。感谢tryx