akka http (un)marshall traits

2019-07-23 03:40发布

问题:

Let's assume the following Code:

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
}

Now I want to define a webservice where one can retrieve the Actions. How can I Marshall the Action as it's just the trait and no specific Type?

I have found this https://github.com/spray/spray-json#providing-jsonformats-for-other-types in the Docs. Is there any simpler way to achieve this than using this approach mixed with pattern matching?

回答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")
  }
}

Or if you only care about converting Actions to json, then simply:

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
  }
})