Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Circe cannot find implicit encoder

Tags:

scala

circe

I am trying to encode a few classes into json strings, however no matter what I try, my classes do not seem to be able to find an implicit encoder for the case classes I am using.

Here's the smallest example I was able to pare it down to.

import io.circe._
import io.circe.generic.semiauto._
import io.circe.generic.auto._
import io.circe.syntax._

case class OneCol(value: String)

object testObject {
  def main(args: Array[String]): Unit = {
    val testVal = OneCol("someVal")
    println(testVal.asJson)
  }
}

Which gives the following compile error

Error:(30, 21) could not find implicit value for parameter encoder: io.circe.Encoder[OneCol] println(testVal.asJson)

I have tried the same thing with semi-auto encoder creation

def main(args: Array[String]): Unit = {
  implicit val enc : Encoder[OneCol] = deriveEncoder
  val testVal = OneCol("someVal")
  println(testVal.asJson)
}

Which gives the following errors

Error:(25, 42) could not find Lazy implicit value of type io.circe.generic.encoding.DerivedObjectEncoder[A] implicit val enc : Encoder[OneCol] = deriveEncoder

Error:(25, 42) not enough arguments for method deriveEncoder: (implicit encode: shapeless.Lazy[io.circe.generic.encoding.DerivedObjectEncoder[A]])io.circe.ObjectEncoder[A]. Unspecified value parameter encode. implicit val enc : Encoder[OneCol] = deriveEncoder

I am fairly sure that the entire purpose of auto and semi-auto encoder generation is to handle situations like these, so I am at a bit of a loss as to what I am doing wrong.

I am using scala 2.10.4, and circe 0.7.0 (circe-core_2.10, circe-generic_2.10 artifacts) if version information is relevant, with maven as the package manager.

Does anyone know why this is failing, and how to get it properly compiling?

Edit:

Here's the section of my POM with the macros plugin. Have tried both compiler plugins listed (both commented and non-commented), and both still give the same error.

        <plugin>
            <groupId>net.alchim31.maven</groupId>
            <artifactId>scala-maven-plugin</artifactId>
            <configuration>
                <args>
                    <!-- work-around for https://issues.scala-lang.org/browse/SI-8358 -->
                    <arg>-nobootcp</arg>
                </args>
                <recompileMode>incremental</recompileMode>
                <compilerPlugins>
                    <compilerPlugin>
                        <groupId>org.scalamacros</groupId>
                        <artifactId>paradise_2.10.4</artifactId>
                        <version>2.1.0</version>
                    </compilerPlugin>
                    <!--<compilerPlugin>-->
                        <!--<groupId>org.scala-lang.plugins</groupId>-->
                        <!--<artifactId>macro-paradise_2.10.2</artifactId>-->
                        <!--<version>2.0.0-SNAPSHOT</version>-->
                    <!--</compilerPlugin>-->
                </compilerPlugins>
            </configuration>
            <executions>
                <execution>
                    <id>scala-compile-first</id>
                    <phase>process-resources</phase>
                    <goals>
                        <goal>compile</goal>
                    </goals>
                </execution>
                <execution>
                    <id>scala-test-compile-first</id>
                    <phase>process-test-resources</phase>
                    <goals>
                        <goal>testCompile</goal>
                    </goals>
                </execution>
            </executions>
        </plugin>
like image 761
Davis Broda Avatar asked Oct 29 '22 09:10

Davis Broda


1 Answers

It turns out that circe-core_2.10 has a dependency on scala version 2.10.6, meaning that my version of scala (2.10.4) was incompatible with the library, causing the issues. Upgrading to the proper version of scala fixed this.

like image 121
Davis Broda Avatar answered Nov 15 '22 08:11

Davis Broda