Search code examples
jsonscalacirce

could not find Lazy implicit value of type io.circe.generic.decoding.DerivedDecoder[Staff]


I am very new to scala (just learned this week ) and trying to parse json using this example

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

i add the first part of the code

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

this is my 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>

and when i run this i get the following error

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

Solution

  • As specified in the comment, your dependencies don't match the one in the blog post, as @Luis pointed out, 0.7.0 is an old version so it's better to use 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>