gpt4 book ai didi

带有案例类的抽象类的 scala circe 编码器/解码器

转载 作者:行者123 更新时间:2023-12-04 14:23:45 26 4
gpt4 key购买 nike

我想将 FieldMapping 类的集合保存为 json 字符串 -

abstract class Field {
def clazz: Class[_]

def name: String
}

case class StringField(name: String) extends Field {
override def clazz: Class[_] = classOf[String]
}

case class DateField(name: String) extends Field {
override def clazz: Class[_] = classOf[Date]
}
...等 - 这里的完整代码:
https://github.com/alexeyOnGitHub/scala-typesafe/blob/master/src/main/scala/com/example/model/Field.scala
循环代码:
import com.example.model.{DateField, Field, FieldMapping, StringField}
import io.circe.generic.semiauto.{deriveDecoder, deriveEncoder}
import io.circe.{Decoder, Encoder}

object CirceBoilerplateForConfigs {
implicit val fieldDecoder: Decoder[StringField] = deriveDecoder[StringField]
implicit val fieldEncoder: Encoder[StringField] = deriveEncoder[StringField]

implicit val dateDecoder: Decoder[DateField] = deriveDecoder[DateField]
implicit val dateEncoder: Encoder[DateField] = deriveEncoder[DateField]

implicit val fooDecoder: Decoder[FieldMapping] = deriveDecoder[FieldMapping]
implicit val fooEncoder: Encoder[FieldMapping] = deriveEncoder[FieldMapping]
}

Error:(14, 65) could not find Lazy implicit value of typeio.circe.generic.decoding.DerivedDecoder[com.example.model.FieldMapping]implicit val fooDecoder: Decoder[FieldMapping] =deriveDecoder[FieldMapping] Error:(14, 65)

not enough arguments for method deriveDecoder: (implicit decode:shapeless.Lazy[io.circe.generic.decoding.DerivedDecoder[com.example.model.FieldMapping]])io.circe.Decoder[com.example.model.FieldMapping].Unspecified value parameter decode. implicit val fooDecoder:Decoder[FieldMapping] = deriveDecoder[FieldMapping] Error:(15, 65)

could not find Lazy implicit value of typeio.circe.generic.encoding.DerivedObjectEncoder[com.example.model.FieldMapping]implicit val fooEncoder: Encoder[FieldMapping] =deriveEncoder[FieldMapping] Error:(15, 65)

not enough arguments formethod deriveEncoder: (implicit encode:shapeless.Lazy[io.circe.generic.encoding.DerivedObjectEncoder[com.example.model.FieldMapping]])io.circe.ObjectEncoder[com.example.model.FieldMapping].Unspecified value parameter encode. implicit val fooEncoder:Encoder[FieldMapping] = deriveEncoder[FieldMapping]

最佳答案

Field应该是一个密封特征(对于抽象类或非密封特征,这将不起作用)。

以下代码编译:

import java.util.Date

sealed trait Field {
def clazz: Class[_]

def name: String
}

case class StringField(name: String) extends Field {
override def clazz: Class[_] = classOf[String]
}

case class DateField(name: String) extends Field {
override def clazz: Class[_] = classOf[Date]
}

case class FieldMapping(fieldInConnector1: Option[Field],
fieldInConnector2: Option[Field],
selected: Boolean,
defaultValue: String)

import io.circe.generic.semiauto.{deriveDecoder, deriveEncoder}
import io.circe.{Decoder, Encoder}
object CirceBoilerplateForConfigs {
implicit val stringDecoder: Decoder[StringField] = deriveDecoder[StringField]
implicit val stringEncoder: Encoder[StringField] = deriveEncoder[StringField]

implicit val dateDecoder: Decoder[DateField] = deriveDecoder[DateField]
implicit val dateEncoder: Encoder[DateField] = deriveEncoder[DateField]

implicit val fieldDecoder: Decoder[Field] = deriveDecoder[Field]
implicit val fieldEncoder: Encoder[Field] = deriveEncoder[Field]

implicit val fooDecoder: Decoder[FieldMapping] = deriveDecoder[FieldMapping]
implicit val fooEncoder: Encoder[FieldMapping] = deriveEncoder[FieldMapping]
}

关于带有案例类的抽象类的 scala circe 编码器/解码器,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50307115/

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