- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我已经编写了这段代码来使用 circe 读写 josn
import io.circe._, io.circe.generic.auto._, io.circe.parser._, io.circe.syntax._
case class Foo(i: Int)
val f = Foo(10)
val json = f.asJson.toString
val t1 = decode[Foo](json)
这很好用。但是如果我创建一个普通的类 Bar
class Bar { var i : Int = 0 }
decode[Bar](json)
现在我得到错误
could not find implicit value for evidence parameter of type io.circe.Decoder[$sess.cmd25.Bar]
那么是否可以使用普通类并使用 Circe 从 json 中解码它们?
最佳答案
通过 io.circe.generic.auto._
,您正在使用 Circe 的自动泛型派生,它由 Shapeless 的 LabelledGeneric
类型类支持。 LabelledGeneric
仅适用于元组和案例类等产品类型。这就是您看到此错误的原因,因为 Circe 的自动模式无法自动为您的普通类派生 Decoder 实例。你可以做的是手动 implement您类(class)的解码器(请参阅自定义编码器/解码器部分)。
关于scala - 使用 Circe 解码 Normal Class(不是 case class),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43374285/
我正在尝试为菊石 REPL 创建一个 predef.sc 文件。这是我写的 val fs2Version = "2.2.2" val circeVersion = "0.13.0" // fs2 in
我正在尝试为两个案例类生成编码器和解码器: object EventBusCases { case class ValuationRequest(function: RequestValue =
我已经为 JSON 表示定义了几个 case 类,但我不确定我是否做得正确,因为有很多嵌套的 case 类。 spec、meta 等实体属于 JSONObject 类型以及自定义对象本身。 这是我定义
为什么我得到错误 could not find Lazy implicit value of type io.circe.generic.decoding.DerivedDecoder[A$A6.th
我正在尝试将一些类编码为 json 字符串,但是无论我尝试什么,我的类似乎都无法为我正在使用的案例类找到隐式编码器。 这是我能够精简到的最小示例。 import io.circe._ import i
我以前见过类似的问题,但没有一个有效。我认为他们会问一些不同的问题,所以我在这里问。 我在一个文件中有这样的东西: sealed trait Thing case class SomeThing()
我的目标是将 JSON 转换为以下模型: case class Container(typeId: Int, timestamp: Long, content: Content) sealed tra
我想用 Circe 解码以下 ADT: sealed trait PaymentType object PaymentType extends EnumEncoder[PaymentType] {
我想将案例类的 Array[Byte] 字段编码为 Base64 字符串。出于某种原因,Circe 没有使用默认编解码器选择我的编解码器,而是将字节数组转换为 json 整数数组。 我应该怎么做才能修
我正在尝试使用 scala json 库 Circe,将它包装在一个简单的 trait 中以提供与 json 的转换,我有以下内容: import io.circe.generic.auto._ im
我试图将“5m”或“5s”或“5ms”形式的字符串解码为 FiniteDuration 类型的对象,分别为 5.minutes、5.seconds、5.milliseconds。 我正在尝试为涉及 F
当字段可以具有不同的原始值类型时,我在解析 json 时遇到问题。例如,我可以得到 json: { "name" : "john", "age" : 31 } 或者它可以是这种形式: {
我有一些 json,其中包含一些字段,这些字段被压缩为 bson-ish 格式,如 {"foo.bar" : "bash"} .我想将其转换为以下表示 {"foo" : { "bar" : "bash
我正在尝试实现类似的东西 object Claims { import shapeless._ import shapeless.labelled.FieldType import io.
我正在尝试使用 Circe 对对象列表进行编码,看起来类似于: val test = Seq(MyObject("hello", None, 1, 2, None) 我正在尝试使用 Circe 解析它
我的问题有点棘手。我有一个看起来像这样的案例类 case class Foo( id: String, name: String, field1: Boolean, f
我有以下案例类: final case class Camel(firstName: String, lastName: String, waterPerDay: Int) 和circe配置: obj
有没有办法将单个 None 字段序列化为“null”? 例如: // When None, I'd like to serialize only f2 to `null` case class Exa
我是 Scala 新手,正在使用 circe 来建模和序列化一些 API 响应。我发现自己使用以下样板 sealed trait SomeTrait object SomeTrait { im
可以有一个如下所示的类: case class Amount(value: Int) case class Data(insurance: Option[Amount], itemPrice: Amo
我是一名优秀的程序员,十分优秀!