JsValue序列化失败

时间:2016-06-10 13:50:01

标签: json scala shapeless spray-json

在我的应用中,我使用https://github.com/fommil/spray-json-shapeless库进行序列化,但我对此类有疑问:

case class FooResult(var id: Option[String], jobId: String, completedAt: DateTime, result: JsValue) extends JobResult

implicit val JobResultFormat: RootJsonFormat[JobResult] = {
  import shapeless._
  cachedImplicit
}

如果没有JsValue类型的结果字段,一切正常,但有了它,我有以下异常:

[error] /home/mgosk/projects/spark-service/spark-rest-api/src/main/scala/com/xxx/http/services/SparkService.scala:134: ambiguous implicit values:
[error]  both object JsValueFormat in trait AdditionalFormats of type com.xxx.spark.json.ServiceJsonProtocol.JsValueFormat.type
[error]  and method familyFormat in trait LowPriorityFamilyFormats of type [T, Repr](implicit gen: shapeless.LabelledGeneric.Aux[T,Repr], implicit sg: shapeless.Cached[shapeless.Strict[com.xxx.spark.json.ServiceJsonProtocol.WrappedRootJsonFormat[T,Repr]]], implicit tpe: shapeless.Typeable[T])spray.json.RootJsonFormat[T]
[error]  match expected type spray.json.JsonFormat[spray.json.JsValue]

任何建议如何处理它。

1 个答案:

答案 0 :(得分:1)

我通过在重建之前将结果更改为JsObject并清理项目来解决了我的问题。

case class FooResult(var id: Option[String], jobId: String, completedAt: DateTime, result: JsObject) extends JobResult