【问题标题】:Encoding/Decode shapeless records with circe用 circe 编码/解码无形记录
【发布时间】:2017-08-21 14:02:32
【问题描述】:

将 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]                                              ^

我是否在这里遗漏了一些导入,或者这些编码器/解码器在 circe 中不再可用?

【问题讨论】:

    标签: json scala circe


    【解决方案1】:

    在 circe 0.6.0 版本中,Shapeless 的 hlist、记录等实例已移至单独的 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 中不可用的功能,例如联产品的实例。

    【讨论】:

    • 非常感谢。在任何地方都找不到该拆分。
    猜你喜欢
    • 2016-06-23
    • 2017-06-12
    • 2021-04-24
    • 2019-03-13
    • 2017-11-27
    • 2019-01-26
    • 2017-05-24
    • 1970-01-01
    • 2019-01-02
    相关资源
    最近更新 更多