如何在 Circe 中使用半自动解码通用案例 Class

How to Decode a Generic Case Class with semiautomatic in Circe

我有以下情况class:

case class QueryResult[T: Decoder](data: T)

它适用于自动派生。

但是半自动推导我无法解决

这是我的测试用例:

  //import io.circe.generic.auto._ // with this it works
  import io.circe.derivation._

  case class Name(name: String)
  case class QueryResult[T: Decoder](data: T)

  implicit val nameDer = deriveDecoder[Name]
  implicit def result[T: Decoder] = deriveDecoder[QueryResult[T]] // this does not work

这给了我:

Error:(16, 50) No method evidence in pme123.graphql.client.QueryResult[T] (this is probably because a constructor parameter isn't a val)
  implicit def result[T: Decoder] = deriveDecoder[QueryResult[T]]

您使用的是哪个版本的 circe?在 0.12.3 中,我必须使用 import io.circe.generic.semiauto._ 并且以下对我有用:

case class Name(name: String)
case class QueryResult[T: Decoder](data: T)

implicit val nameDer = deriveDecoder[Name]
implicit def result[T: Decoder] = deriveDecoder[QueryResult[T]]

val json = """{"data": {"name": "foo"}}"""
decode[QueryResult[Name]](json)  // Right(QueryResult(Name(foo)))