akka http(联合国)马歇尔特征

时间:2017-02-12 13:55:39

标签: scala akka-http spray-json

我们假设以下代码:

sealed trait Action {
    def run(): Boolean
}

case class SimpleAction(parameter: String) extends Actions {
 // some impl
}

case class ExtendedAction(parameter1: String, parameter2: String) extends Actions {
 // some impl
}

现在我想定义一个可以检索Actions的webservice。我怎样才能对行动负责,因为它只是特质而没有具体的类型?

我在文档中找到了这个https://github.com/spray/spray-json#providing-jsonformats-for-other-types。有没有比使用混合模式匹配的方法更简单的方法来实现这一点?

1 个答案:

答案 0 :(得分:1)

import spray.json._
import DefaultJsonProtocol._

implicit val simpleActionFormat = jsonFormat1(SimpleAction)
implicit val extendedActionFormat = jsonFormat2(ExtendedAction)
implicit val actionFormat1 = new JsonFormat[Action] {
  override def write(obj: Action): JsValue = obj match {
    case a: SimpleAction => JsObject("type" -> "simple".toJson, "value" -> a.toJson)
    case b: ExtendedAction => JsObject("type" -> "extended".toJson, "value" -> b.toJson)
  }

  override def read(json: JsValue): Action = json.asJsObject.getFields("type", "value") match {
    case Seq(JsString("simple"), js) => js.convertTo[SimpleAction]
    case Seq(JsString("extended"), js) => js.convertTo[ExtendedAction]
    case _ => throw new RuntimeException(s"Invalid json format: $json")
  }
}

或者,如果您只关心将Actions转换为json,那么只需:

implicit val simpleActionFormat = jsonFormat1(SimpleAction)
implicit val extendedActionFormat = jsonFormat2(ExtendedAction)
implicit val actionFormat = lift(new JsonWriter[Action] {
  override def write(obj: Action): JsValue = obj match {
    case a: SimpleAction => a.toJson
    case b: ExtendedAction => b.toJson
  }
})