gpt4 book ai didi

json - Circe DeriveDecoder/deriveEncoder 找不到 io.circe.generic.encoding.DerivedAsObjectEncoder 类型的惰性隐式值

转载 作者:行者123 更新时间:2023-12-04 17:30:40 25 4
gpt4 key购买 nike

我正在尝试为两个案例类生成编码器和解码器:

object EventBusCases {
case class ValuationRequest(function: RequestValue = ALL_DAY_VALS, interval: RequestValue = IntraDayIntervals.MIN_5)
implicit val requestDecoder: Decoder[ValuationRequest] = deriveDecoder[ValuationRequest]
implicit val requestEncoder: Encoder[ValuationRequest] = deriveEncoder[ValuationRequest]

case class ValuationResponse(values: List[Valuation], function: RequestValue)
implicit val responseDecoder: Decoder[ValuationResponse] = deriveDecoder[ValuationResponse]
implicit val responseEncoder: Encoder[ValuationResponse] = deriveEncoder[ValuationResponse]
}

我不断收到这样的错误,但对于这两种情况: could not find Lazy implicit value of type io.circe.generic.encoding.DerivedAsObjectEncoder[eventbus.eventBusCases.ValuationResponse]
我决定还尝试为这些类中的自定义类派生编码器和解码器,例如“估值”,但我在这些类上遇到了相同的错误。

我正在使用 Circe 0.12.3Scala 2.12.8这些是我与 Circe 相关的 Scala 依赖项:
  "com.beachape" %% "enumeratum" % "1.5.14",
"com.beachape" %% "enumeratum-circe" % "1.5.22",
"io.circe" %% "circe-core" % "0.12.3",
"io.circe" %% "circe-generic" % "0.12.3",
"io.circe" %% "circe-parser" % "0.12.3"

最佳答案

因此,我发现使这项工作的方法是对 ValuationRequest 和 ValuationResponse 以及其中包含的所有自定义类型实现编码器和解码器。

对于 ValuationRequest 和 ValuationResponse,我基本上将这个位添加到包含两个 case 类的同一个文件中:

object derivation {
implicit val encodeResponse: Encoder[ValuationResponse] = Encoder.instance {
case response @ ValuationResponse(_,_) => response.asJson
}
implicit val decodeResponse: Decoder[ValuationResponse] =
List[Decoder[ValuationResponse]](
Decoder[ValuationResponse].widen
).reduceLeft(_ or _)

implicit val encodeRequest: Encoder[ValuationRequest] = Encoder.instance {
case response @ ValuationRequest(_,_) => response.asJson
}
implicit val decodeRequest: Decoder[ValuationRequest] =
List[Decoder[ValuationRequest]](
Decoder[ValuationRequest].widen
).reduceLeft(_ or _)
}

关于json - Circe DeriveDecoder/deriveEncoder 找不到 io.circe.generic.encoding.DerivedAsObjectEncoder 类型的惰性隐式值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60043219/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com