找不到io.circe.generic.decoding.DerivedDecoder类型的Lazy隐式值[Staff]

时间:2019-05-24 13:10:09

标签: json scala circe

我对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())

1 个答案:

答案 0 :(得分:3)

如注释中所指定,您的依赖项与博客文章中的依赖项不匹配,正如@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>