【问题标题】:could not find Lazy implicit value of type io.circe.generic.decoding.DerivedDecoder[Staff]找不到 io.circe.generic.decoding.DerivedDecoder[Staff] 类型的惰性隐式值
【发布时间】:2019-05-24 22:36:20
【问题描述】:

我对 scala 非常陌生(这周刚学会)并尝试使用此示例解析 json

https://medium.com/@djoepramono/how-to-parse-json-in-scala-c024cb44f66b

我添加了代码的第一部分

import io.circe.parser
import io.circe.generic.semiauto.deriveDecoder

case class Staff(name: String)
object SimpleDecoder {
  def main(args: Array[String]): Unit = {
    val input =
      """
        {
          "name": "John Doe"
        }
      """.stripMargin

    implicit val staffDecoder = deriveDecoder[Staff]
    val decodeResult = parser.decode[Staff](input)
    decodeResult match {
      case Right(staff) => println(staff.name)
      case Left(error) => println(error.getMessage())
    }
  }
}

这是我的 pom.xml

<dependency>
    <groupId>io.circe</groupId>
    <artifactId>circe-core_2.11</artifactId>
    <version>0.3.0</version>
</dependency>


<dependency>
    <groupId>io.circe</groupId>
    <artifactId>circe-generic_2.12</artifactId>
    <version>0.12.0-M1</version>
</dependency>


<dependency>
    <groupId>io.circe</groupId>
    <artifactId>circe-parser_2.12</artifactId>
    <version>0.12.0-M1</version>
</dependency>

当我运行它时,我得到以下错误

Error:(14, 46) could not find Lazy implicit value of type io.circe.generic.decoding.DerivedDecoder[Staff]
    implicit val staffDecoder = deriveDecoder[Staff]
Error:(14, 46) not enough arguments for method deriveDecoder: (implicit decode: shapeless.Lazy[io.circe.generic.decoding.DerivedDecoder[Staff]])io.circe.Decoder[Staff].
Unspecified value parameter decode.
    implicit val staffDecoder = deriveDecoder[Staff]
Error:(15, 44) could not find implicit value for parameter d: io.circe.Decoder[Staff]
    val decodeResult = parser.decode[Staff](input)
Error:(15, 44) not enough arguments for method decode: (implicit d: io.circe.Decoder[Staff])cats.data.Xor[io.circe.Error,Staff].
Unspecified value parameter d.
    val decodeResult = parser.decode[Staff](input)
Error:(17, 42) value name is not a member of Any
      case Right(staff) => println(staff.name)
Error:(18, 41) value getMessage is not a member of Any
      case Left(error) => println(error.getMessage())

【问题讨论】:

  • 你在依赖中没有使用相同版本的scala,帖子上的博客也使用了circe的0.7.0版本,而你使用0.3.00.12.0-M1...跨度>
  • 我尝试使用 0.7.0,但我收到一个错误,指出此版本不存在,因此我搜索了解析器、泛型和核心的最新版本

标签: json scala circe


【解决方案1】:

如评论中所述,您的依赖项与博文中的不匹配,正如@Luis 指出的那样,0.7.0 是旧版本,因此最好使用0.11.1

pom.xml

<dependency>
    <groupId>io.circe</groupId>
    <artifactId>circe-core_2.12</artifactId>
    <version>0.11.1</version>
</dependency>


<dependency>
    <groupId>io.circe</groupId>
    <artifactId>circe-generic_2.12</artifactId>
    <version>0.11.1</version>
</dependency>


<dependency>
    <groupId>io.circe</groupId>
    <artifactId>circe-parser_2.12</artifactId>
    <version>0.11.1</version>
</dependency>

【讨论】:

  • @RMagen 虽然这行得通... Circe 已经是0.11.1 版本,0.7.0 版本已经有两年多了! - 我认为将其用于较新的项目不是一个好主意。
  • @LuisMiguelMejíaSuárez 公平点!我已经相应地更新了答案
猜你喜欢
  • 2020-05-19
  • 1970-01-01
  • 1970-01-01
  • 2017-03-26
  • 1970-01-01
  • 2015-12-18
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多