gpt4 book ai didi

scala - Circe 找不到隐式编码器

转载 作者:行者123 更新时间:2023-12-04 13:17:41 26 4
gpt4 key购买 nike

我正在尝试将一些类编码为 json 字符串,但是无论我尝试什么,我的类似乎都无法为我正在使用的案例类找到隐式编码器。

这是我能够精简到的最小示例。

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

这给出了以下编译错误

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



我已经尝试过与半自动编码器创建相同的事情
def main(args: Array[String]): Unit = {
implicit val enc : Encoder[OneCol] = deriveEncoder
val testVal = OneCol("someVal")
println(testVal.asJson)
}

这给出了以下错误

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



我相当确定自动和半自动编码器生成的全部目的是处理这些情况,所以我对我做错了什么感到有点茫然。

如果版本信息相关,我使用 scala 2.10.4 和 circe 0.7.0(circe-core_2.10,circe-generic_2.10 工件),maven 作为包管理器。

有谁知道为什么会失败,以及如何正确编译?

编辑:

这是带有宏插件的 POM 部分。已经尝试了列出的两个编译器插件(已注释和未注释),但仍然出现相同的错误。
        <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>

最佳答案

事实证明,circe-core_2.10 依赖于 Scala 版本 2.10.6,这意味着我的 Scala 版本(2.10.4)与库不兼容,从而导致了问题。升级到正确版本的 Scala 解决了这个问题。

关于scala - Circe 找不到隐式编码器,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43143430/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com