akka http (un)marshall traits

元气小坏坏 提交于 2019-12-13 17:55:13

问题


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


来源:https://stackoverflow.com/questions/42188419/akka-http-unmarshall-traits

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!