使用Spray序列化为JSON时,更好地解决了implicits和排序问题?

时间:2014-03-25 22:47:11

标签: json scala spray implicits

我使用Spray JSON库将我们的case类序列化为JSON。问题是我们有一些相互递归的定义。我正在从这个序列化密封特征的例子开始: http://www.cakesolutions.net/teamblogs/2012/11/30/spray-json-and-adts/

这是一个有效的简单示例。请注意C的定义:

import spray.json._
import DefaultJsonProtocol._

sealed trait BaseTrait
sealed trait SpecializedTrait extends BaseTrait

case class A(argA: BaseTrait, foo: Int) extends SpecializedTrait
case class B(argB: BaseTrait, foo: Int) extends SpecializedTrait

case class C(foo: Int) extends BaseTrait

object BaseTrait {
  implicit val cJsonFormat = jsonFormat1(C)

  implicit object BaseTraitJsonFormat extends RootJsonFormat[BaseTrait] {
    override def write(obj: BaseTrait): JsValue = throw new IllegalStateException("Not Implemented")
    override def read(v: JsValue): BaseTrait = throw new IllegalStateException("Not Implemented")
  }
}

object SpecializedTrait {
  implicit val aJsonFormat = jsonFormat2(A)
  implicit val bJsonFormat = jsonFormat2(B)

  implicit object SpecializedTraitJsonFormat extends RootJsonFormat[SpecializedTrait] {
    override def write(obj: SpecializedTrait): JsValue = throw new IllegalStateException("Not Implemented")
    override def read(v: JsValue): SpecializedTrait = throw new IllegalStateException("Not Implemented")
  }
}

当您更改" C"的定义时原始的相互递归定义,。

...
case class C(argC: SpecializedTrait, foo: Int) extends BaseTrait

object BaseTrait {
  implicit val cJsonFormat = jsonFormat2(C)

...

显然,你不能说不,我不能有相互递归的数据结构。由于隐式对象的解析规则,编译器似乎陷入困境。

有解决方法吗?我通过将对象更改为匿名类声明来解决它,即

import spray.json._
import DefaultJsonProtocol._

sealed trait BaseTrait
sealed trait SpecializedTrait extends BaseTrait

case class A(argA: BaseTrait, foo: Int) extends SpecializedTrait
case class B(argB: BaseTrait, foo: Int) extends SpecializedTrait

case class C(argC: SpecializedTrait, foo: Int) extends BaseTrait

object BaseTrait {
  implicit val cJsonFormat : RootJsonFormat[C] = jsonFormat2(C)

  implicit val baseTraitFormat : RootJsonFormat[BaseTrait] = new RootJsonFormat[BaseTrait] {
    def write(obj: BaseTrait): JsValue = throw new IllegalStateException("Not Implemented")
    def read(v: JsValue): BaseTrait = throw new IllegalStateException("Not Implemented")
  }
}

object SpecializedTrait {
  implicit val aJsonFormat : RootJsonFormat[A] = jsonFormat2(A)
  implicit val bJsonFormat : RootJsonFormat[B] = jsonFormat2(B)

  implicit val specializedTraitFormat : RootJsonFormat[SpecializedTrait] = new RootJsonFormat[SpecializedTrait] {
    override def write(obj: SpecializedTrait): JsValue = throw new IllegalStateException("Not Implemented")
    override def read(v: JsValue): SpecializedTrait = throw new IllegalStateException("Not Implemented")
  }
}

最后一个片段有效,请注意"隐含对象"到"隐含的val"以及随后的匿名课程。

1 个答案:

答案 0 :(得分:0)

按顺序定义implicits并将它们放入一个对象中。

import spray.json._
import DefaultJsonProtocol._

sealed trait BaseTrait
sealed trait SpecializedTrait extends BaseTrait

case class A(argA: BaseTrait, foo: Int) extends SpecializedTrait
case class B(argB: BaseTrait, foo: Int) extends SpecializedTrait
case class C(argC: SpecializedTrait, foo: Int) extends BaseTrait

object SomeProtocol {
  implicit object BaseTraitJsonFormat extends RootJsonFormat[BaseTrait] {
    override def write(obj: BaseTrait): JsValue = throw new IllegalStateException("Not Implemented")
    override def read(v: JsValue): BaseTrait = throw new IllegalStateException("Not Implemented")
  }
  implicit object SpecializedTraitJsonFormat extends RootJsonFormat[SpecializedTrait] {
    override def write(obj: SpecializedTrait): JsValue = throw new IllegalStateException("Not Implemented")
    override def read(v: JsValue): SpecializedTrait = throw new IllegalStateException("Not Implemented")
  }
  implicit val aJsonFormat = jsonFormat2(A)
  implicit val bJsonFormat = jsonFormat2(B)
  implicit val cJsonFormat = jsonFormat2(C)
}
相关问题