作者热门文章
- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我正试图将我的头环绕在 Circe 上。
所以,这是我得到的模型:
object Gender extends Enumeration {
type Gender = Value
val Male, Female, Unisex, Unknown = Value
}
case class Product(id: String, gender: Gender.Value)
val product = Product(id = "1234", gender = Gender.Female)
object JsonProtocol {
implicit val productDecoder: Decoder[Product] = deriveDecoder
implicit val productEncoder: Encoder[Product] = deriveEncoder
}
Error:(52, 49) could not find Lazy implicit value of type io.circe.generic.decoding.DerivedDecoder[A]
implicit val productDecoder: Decoder[Product] = deriveDecoder
^
最佳答案
尝试使用以下方法为枚举定义自己的编码器和解码器:
Decoder.enumDecoder[E <: Enumeration](enum: E)
Encoder.enumEncoder[E <: Enumeration](enum: E)
object JsonProtocol {
implicit val genderDecoder: Decoder[Gender.Value] = Decoder.enumDecoder(Gender)
implicit val genderEncoder: Encoder[Gender.Value] = Encoder.enumEncoder(Gender)
implicit val productDecoder: Decoder[Product] = deriveDecoder
implicit val productEncoder: Encoder[Product] = deriveEncoder
}
sealed trait
的层次结构。 s 和
case classes
据我所知。您看到该错误的原因是
Product
的派生编解码器将隐含地要求编码器/解码器用于每个参数的类型。
String
的编码器/解码器是 Circe 的标准部分,但您可能需要为自己的枚举创建一个。
关于json - Circe 和 Scala 的枚举类型,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42068680/
我是一名优秀的程序员,十分优秀!