5

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>
Davis Broda
  • 4,102
  • 5
  • 23
  • 37
  • I'm not at a computer at the moment, but on 2.10 you will need the Macro Paradise compiler plugin for generic derivation to work. The instructions are in the docs (I can provide a link later). – Travis Brown Mar 31 '17 at 14:49
  • @TravisBrown While it was initially not present, copying it in from another project did not fix the error. However I have added the relevent section of the pom here, as I have no idea if I have configured it correctly or not. – Davis Broda Mar 31 '17 at 15:08

1 Answers1

3

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.

Davis Broda
  • 4,102
  • 5
  • 23
  • 37
  • I'd suggest going to 2.12 for a new project, or 2.11 if your dependencies allow it. – Reactormonk Mar 31 '17 at 15:19
  • @Reactormonk Looked into upgrading to 2.11, but unfortunately some of our dependencies are not fully supported in scala 2.11, so I'm locked into this for the time being. Will upgrade as soon as the dependencies get 2.11 support worked out. – Davis Broda Mar 31 '17 at 15:52
  • 2
    That sounds weird. Are you sure these libraries aren't abandoned by now? – Reactormonk Mar 31 '17 at 16:08