为什么不使用circe将我的区分字段添加到编码的case对象中?

时间:2018-11-28 13:56:16

标签: json scala circe

我有以下代码,希望能打印出要编码的对象的类型,但只打印出一个空对象:

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的编码?

1 个答案:

答案 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"}
}

需要注意的是:

    (据我所知)
  1. Configurationio.circe.generic.extras.semiauto的,而不是io.circe.generic.auto
  2. 您提供了Encoder,并且没有让它(半)自动派生。而配置是用于(半)自动派生的。