jsonscalagenericsderivedcirce

Circe deriveDecoder/deriveEncoder could not find Lazy implicit value of type io.circe.generic.encoding.DerivedAsObjectEncoder


I am trying to generate encoders and decoders for two case classes:

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]
}

I keep getting errors like this one, but for both cases: could not find Lazy implicit value of type io.circe.generic.encoding.DerivedAsObjectEncoder[eventbus.eventBusCases.ValuationResponse]

I decided to also try and derive encoders and decoders for the custom classes inside these ones, such as "Valuation", but I just get the same error on those ones.

I am using Circe 0.12.3 and Scala 2.12.8 and these are my Circe related Scala dependencies:

  "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"

Solution

  • So, the way I found to make this work was to implement Encoders and Decoders to both ValuationRequest and ValuationResponse, as well as to all custom types contained in them.

    For ValuationRequest and ValuationResponse, I basically added this bit to the same file containing both case classes:

    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 _)
    }