我有以下代码,希望能打印出要编码的对象的类型,但只打印出一个空对象:
import cats.syntax.functor._
import io.circe.generic.auto._
import io.circe.generic.extras.Configuration
import io.circe.syntax._
import io.circe.{Decoder, Encoder}
object Main extends App {
implicit val customConfig: Configuration =
Configuration.default.withDefaults.withDiscriminator("type")
sealed trait Foo
final case object Bar extends Foo
final case object Boo extends Foo
implicit val encodeEvent: Encoder[Foo] = Encoder.instance {
case Bar => Bar.asJson
case Boo => Boo.asJson
}
implicit val decodeEvent: Decoder[Foo] =
List[Decoder[Foo]](
Decoder[Bar.type].widen,
Decoder[Boo.type].widen,
).reduceLeft(_ or _)
val bar = Bar
println((bar: Foo).asJson.noSpaces) // {}
}
为什么不将Configuration
应用于我的Foo
的编码?
答案 0 :(得分:2)
以下方法有效:
import cats.syntax.functor._
import io.circe.generic.extras.semiauto._
import io.circe.generic.extras.Configuration
import io.circe.syntax._
import io.circe.{Decoder, Encoder}
object Main extends App {
implicit val customConfig: Configuration =
Configuration.default.withDefaults.withDiscriminator("type")
sealed trait Foo
final case object Bar extends Foo
final case object Boo extends Foo
implicit val eBar: Encoder[Bar.type] = deriveEncoder[Bar.type]
implicit val eBoo: Encoder[Boo.type] = deriveEncoder[Boo.type]
implicit val dBar: Decoder[Bar.type] = deriveDecoder[Bar.type]
implicit val dBoo: Decoder[Boo.type] = deriveDecoder[Boo.type]
implicit val encodeEvent: Encoder[Foo] = deriveEncoder[Foo]
implicit val decodeEvent: Decoder[Foo] =
List[Decoder[Foo]](
Decoder[Bar.type].widen,
Decoder[Boo.type].widen,
).reduceLeft(_ or _)
val foo: Foo = Boo: Foo
println(foo.asJson.noSpaces) // {"type":"Boo"}
}
需要注意的是:
Configuration
是io.circe.generic.extras.semiauto
的,而不是io.circe.generic.auto
Encoder
,并且没有让它(半)自动派生。而配置是用于(半)自动派生的。