Spray Json:找不到

时间:2016-04-03 13:32:52

标签: json scala spray spray-json

我有我的课,我放置了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!")
        }
  }
}

我还扩展了RootJsonFormat。但这也行不通。 我不知道为什么toJson方法不起作用。在执行时我得到错误:找不到Handshake.this.handshake.type

的JsonWriter或JsonFormat类型类

我使用Scala 2.11.7

1 个答案:

答案 0 :(得分:1)

我添加了

func doDismiss(_ sender: UIButton) {
    // Use presentingViewController twice to go back two levels and call
    // dismissViewController to dismiss both viewControllers.
    self.presentingViewController?.presentingViewController?.dismiss(animated: true, completion: nil)
}

到握手班。这有效。有了这个,HandhshakeJsonFormat就在握手范围内。感谢tryx