Scala编译错误 - 无法找到类型的证据参数的隐含值

时间:2017-05-06 07:07:33

标签: scala spray-json

我正在使用spray json序列化来处理以下案例类

case class ActivationMessage(override val transid: TransactionId,
                             action: FullyQualifiedEntityName,
                             revision: DocRevision,
                             user: Identity,
                             activationId: ActivationId,
                             activationNamespace: EntityPath,
                             content: Option[JsObject],
                             cause: Option[ActivationId] = None,
                             traceMetadata: Option[SpanMetadata] = None
                            ) extends Message {

  def meta = JsObject("meta" -> {
    cause map {
      c => JsObject(c.toJsObject.fields ++  activationId.toJsObject.fields)
    } getOrElse {
      activationId.toJsObject
    }
  })

  override def serialize = ActivationMessage.serdes.write(this).compactPrint

  override def toString = {
    val value = (content getOrElse JsObject()).compactPrint
    s"$action?message=$value"
  }

  def causedBySequence: Boolean = cause.isDefined
}

object ActivationMessage extends DefaultJsonProtocol {
  def parse(msg: String) = Try(serdes.read(msg.parseJson))
  private implicit val fqnSerdes = FullyQualifiedEntityName.serdes
  implicit val serdes = jsonFormat9(ActivationMessage.apply)
}

我收到编译错误说 - 无法找到ActivationMessage.JF[Option[com.github.levkhomich.akka.tracing.SpanMetadata]]

类型的证据参数的隐含值

如果我从构造函数中删除最后一个参数并使用jsonFormat8,那么每个东西编译都很好。如何在没有编译问题的情况下添加这个额外的参数?

1 个答案:

答案 0 :(得分:2)

编译器找不到JsonFormat的隐式SpanMetadata。尝试:

...
object ActivationMessage extends DefaultJsonProtocol {
  ...
  private implicit val spanMetaSerdes = jsonFormat4(SpanMetadata.apply)
  implicit val serdes = jsonFormat9(ActivationMessage.apply)
}