spray / akka http json编组案例类作为值

时间:2016-01-17 16:25:11

标签: json scala spray akka-http

在spray / akka http中,我可以像这样编组/解组一个案例类:

case class Latitude(value:Double)
object Latitude extends DefaultJsonProtocol with SprayJsonSupport {
  implicit val LatitudeFormat = jsonFormat1(Latitude.apply)
}

但是,这会将Latitude(42)封送到对象{value:42}。我宁愿把它编组到一个JsNumber 42。为此,我做了以下事情:

case class Latitude(value:Double)
object Latitude extends DefaultJsonProtocol with SprayJsonSupport {
  implicit object LatitudeFormat extends RootJsonFormat[Latitude] {
    def write(lat: Latitude) = lat.value.toJson
    def read(value: JsValue) = ??? //too much code with decent error handling, but working
  }
}

但是,我不想为每个"简单值案例类#34;做这件事。 我的目标是例如创建一个函数(或者可能是一个宏),它完全像喷雾jsonFormat1一样工作,除了它不是写/读对象而是简单的值,这取决于我使用它的案例类。

不幸的是,似乎没有任何方法可以扩展或组合从jsonFormat1函数返回的jsonRootFormat对象。该函数本身似乎使用已弃用的东西(如ClassManifest),因此我不确定是否要根据需要复制和调整它。在这种情况下,为JsValue函数创建/创建这样一个简单的案例类,我最好的选择是什么?

0 个答案:

没有答案