I'm encountering a runtime error when using the circe and cats libraries together in my Scala code. The error message I receive is java.lang.NoClassDefFoundError: cats/FlatMapArityFunctions. I would appreciate any insights into this issue.
Here is the relevant code snippet:
sealed trait SuperJob {
def name: String
}
object SuperJob {
implicit val encodeJob: Encoder[SuperJob] = Encoder.instance {
case job @ FirstSuperJob(_) => job.asJson
case job @ SecondSuperJob(_) => job.asJson
}
implicit val decodeJob: Decoder[SuperJob] = List[Decoder[SuperJob]](
Decoder[FirstSuperJob].widen,
Decoder[SecondSuperJob].widen
).reduceLeft(_ or _)
}
case class FirstSuperJob(name: String) extends SuperJob
object FirstSuperJob {
implicit def decoder: Decoder[FirstSuperJob] = deriveDecoder
implicit def encoder: Encoder[FirstSuperJob] = deriveEncoder
}
case class SecondSuperJob(name: String) extends SuperJob
object SecondSuperJob {
implicit def decoder: Decoder[SecondSuperJob] = deriveDecoder
implicit def encoder: Encoder[SecondSuperJob] = deriveEncoder
}
object example extends App {
val firstJob = FirstSuperJob("firstjob")
val secondJob = SecondSuperJob("secondjob")
println(firstJob.name)
println(secondJob.name)
val firstJson = FirstSuperJob.encoder(firstJob)
println(firstJson)
val secondJson = SecondSuperJob.encoder(secondJob)
println(secondJson)
// Run time error below!!!!
try {
val firstDecodeResult = firstJson.as[FirstSuperJob]
println(firstDecodeResult)
} catch {
case ex: Throwable => println(s"caught exception: ${ex}")
}
}
I have the following dependency in my build.sbt:
When I run the code, I get the following output:
firstjob
secondjob
{
"name" : "firstjob"
}
{
"name" : "secondjob"
}
caught exception: java.lang.NoClassDefFoundError: cats/FlatMapArityFunctions
I am expecting this instead:
firstjob
secondjob
{
"name" : "firstjob"
}
{
"name" : "secondjob"
}
Right(FirstSuperJob(firstjob))
I have following dependencies in my build.sbt
val catsVersion = "2.2.0"
val circeVersion = "0.13.0"
"io.circe" %% "circe-core" % circeVersion,
"io.circe" %% "circe-generic" % circeVersion,
"io.circe" %% "circe-parser" % circeVersion,
"io.circe" %% "circe-literal" % circeVersion,
"org.typelevel" %% "cats-core" % catsVersion,
"org.typelevel" %% "cats-effect" % catsVersion
I have tried searching for a solution but haven't been able to find one that resolves this issue. Any help would be greatly appreciated. Thank you!