简体   繁体   English

Circe无法找到隐式编码器

[英]Circe cannot find implicit encoder

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. 我试图将几个类编码为json字符串,但无论我尝试什么,我的类似乎都找不到我正在使用的case类的隐式编码器。

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) 错误:(30,21)找不到参数编码器的隐含值: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 错误:(25,42)找不到类型为io.circe.generic.encoding的延迟隐式值.DerivedObjectEncoder [A]隐式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]. 错误:(25,42)方法deriveEncoder没有足够的参数:(隐式编码:shapeless.Lazy [io.circe.generic.encoding.DerivedObjectEncoder [A]])io.circe.ObjectEncoder [A]。 Unspecified value parameter encode. 未指定的值参数编码。 implicit val enc : Encoder[OneCol] = deriveEncoder 隐式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. 如果版本信息相关,我使用scala 2.10.4和circe 0.7.0(circe-core_2.10,circe-generic_2.10工件),maven作为包管理器。

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. 这是我的POM中带有宏插件的部分。 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>

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. 事实证明,circe-core_2.10依赖于scala版本2.10.6,这意味着我的scala版本(2.10.4)与库不兼容,导致问题。 Upgrading to the proper version of scala fixed this. 升级到适当版本的scala修复了这个问题。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 找不到参数编码器的隐式值:io.circe.Encoder[com.sweetsoft.SapHealth] - could not find implicit value for parameter encoder: io.circe.Encoder[com.sweetsoft.SapHealth] Circe:将隐式编码器移动到通用 Class - Circe: Moving an Implicit Encoder into A Generic Class scala could not find implicit value for parameter encoder: io.circe.Encoder[scala.collection.immutable.Map[String,java.io.Serializable]] ).asJson) - scala could not find implicit value for parameter encoder: io.circe.Encoder[scala.collection.immutable.Map[String,java.io.Serializable]] ).asJson) io.circe.Encoder [scala.collection.immutable.Map [Int,Any]]类型的离散隐式展开 - diverging implicit expansion for type io.circe.Encoder[scala.collection.immutable.Map[Int,Any]] Circe DeriveDecoder/deriveEncoder 找不到 io.circe.generic.encoding.DerivedAsObjectEncoder 类型的惰性隐式值 - Circe deriveDecoder/deriveEncoder could not find Lazy implicit value of type io.circe.generic.encoding.DerivedAsObjectEncoder Scala Quill长时间找不到隐式编码器 - Scala Quill can't find implicit encoder for long 找不到io.circe.generic.decoding.DerivedDecoder类型的Lazy隐式值[Staff] - could not find Lazy implicit value of type io.circe.generic.decoding.DerivedDecoder[Staff] 错误:找不到参数 d 的隐式值:io.circe.Decoder[List[TableInfo]] - error: could not find implicit value for parameter d: io.circe.Decoder[List[TableInfo]] 如何为抽象 class 编写 circe 解码器/编码器 - how to write circe decoder/encoder for abstract class 斯卡拉 Circe。 编码器类型 任意 - Scala Circe. Encoder type Any
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM