[英]spray-json recursive json issue - could not find implicit value for evidence parameter
Hello I am bit struggling with parsing a json with spray-json libary whith recursive data structure. 您好,我有点想用spray-json libary递归数据结构解析json。
I have following case class structure and Protocol defined 我有以下案例类结构和协议定义
import spray.json.DefaultJsonProtocol
import spray.json._
case class OfferAnalysisReport(BUG: DleTN, DOM: DleTN) extends AnalyticalReport
case class DleTN(doc_count: Int, result: AggsDefinition)
case class BucketDefinition(key: String, doc_count: Int, result: Option[AggsDefinition])
case class AggsDefinition(buckets: List[BucketDefinition])
object OfferAnalysisReportProtocol extends DefaultJsonProtocol {
implicit val aggsDefFormat = lazyFormat(jsonFormat(AggsDefinition,"buckets"))
implicit val bucketDefinitionFormat = lazyFormat(jsonFormat(BucketDefinition,"key","doc_count","result"))
implicit val dleTypNemovitostisFormat = jsonFormat2(DleTypuNemovitosti)
implicit val OfferAnalysisReportFormat = jsonFormat2(OfferAnalysisReport)
}
In the test I am importing: 在测试中,我要导入:
import spray.json._
import OfferAnalysisReportProtocol._
But I am still getting 但我仍然
Error: couldn't find implicit value for evidence parameter of type BuckedDefinition.
Am I missing something important here? 我在这里错过了重要的事情吗? Could someone give me a hint?
有人可以给我一个提示吗?
You will have to wrap your format constructor with lazyFormat as you have done, but you also have to supply an explicit type annotation like this: 完成后,您将不得不用lazyFormat包装格式构造函数,但是还必须提供一个显式类型注释,如下所示:
implicit val fooFormat: JsonFormat[Foo] = lazyFormat(jsonFormat(Foo, "i", "foo"))
Source: https://github.com/spray/spray-json#jsonformats-for-recursive-types 来源: https : //github.com/spray/spray-json#jsonformats-for-recursive-types
@spydons solution did not work for me. @spydons解决方案不适用于我。 You can handle recursive classes by writing a custom serializer as well.
您还可以通过编写自定义序列化程序来处理递归类。 Here's an example for writing:
这是一个编写示例:
object Test extends App{
import spray.json._
case class Foo(i: Int, foo: Foo)
implicit object FooJsonFormat extends RootJsonWriter[Foo] {
override def write(c: Foo) : JsValue =
if (c.foo != null) { JsObject("i" -> JsNumber(c.i),"foo" -> write(c.foo)) }
else { JsObject("i" -> JsNumber(c.i)) }
}
println(Foo(20,Foo(10,null)).toJson) //prints {"foo":{"i":10},"i":20}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.