使用circe编码/解码无形记录

时间:2017-03-28 13:11:06

标签: json scala circe

将circe从0.4.1升级到0.7.0打破了以下代码:

import shapeless._
import syntax.singleton._
import io.circe.generic.auto._

.run[Record.`'transaction_id -> Int`.T](transport)

def run[A](transport: Json => Future[Json])(implicit decoder: Decoder[A], exec: ExecutionContext): Future[A]

出现以下错误:

 could not find implicit value for parameter decoder: io.circe.Decoder[shapeless.::[Int with shapeless.labelled.KeyTag[Symbol with shapeless.tag.Tagged[String("transaction_id")],Int],shapeless.HNil]]
[error]       .run[Record.`'transaction_id -> Int`.T](transport)
[error]                                              ^

我在这里缺少一些导入,或者这些编码器/解码器不再可用了吗?

1 个答案:

答案 0 :(得分:1)

在Shape 0.6.0版本中,Shapeless的hlists,记录等实例被移动到一个单独的circe-shapes模块中。如果将此模块添加到构建中,则以下内容应该可以正常工作:

import io.circe.jawn.decode, io.circe.shapes._
import shapeless._, record.Record, syntax.singleton._

val doc = """{ "transaction_id": 1 }"""

val res = decode[Record.`'transaction_id -> Int`.T](doc)

移动这些实例的动机是0.6中引入的改进的泛型推导意味着它们不再是必需的,并且当它们不需要时将它们保持在隐式范围之外是更清晰并且可能支持更快的编译时间。新的circe-shapes模块还包括circe-generic中没有的功能,例如副产品的实例。

相关问题