我正在使用 Spray JSON 库将我们的案例类序列化为 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”的更改以及随后的匿名类。