简体   繁体   中英

How to Decode a Generic Case Class with semiautomatic in Circe

I have the following case class:

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

It works with auto derivation.

But I could not solve it to have a semiautomatic derivation.

Here is my Test case:

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

This gives me:

Error:(16, 50) No method evidence$1 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]]

Which version of circe are you using? In 0.12.3 I have to use import io.circe.generic.semiauto._ and the following works for me:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM