圆圈类型字段未显示

circe type field not showing

当使用 circe 编码为 Json 时,我们确实希望 type 字段显示例如

scala> val fooJson = foo.asJson
fooJson: io.circe.Json =
{
  "this_is_a_string" : "abc",
  "another_field" : 123,
  "type" : "Foo"
}

这取自之前提到的可以像这样配置编码的发行说明:

implicit val customConfig: Configuration = 
Configuration.default.withSnakeCaseKeys.withDefaults.withDiscriminator("type")

关于 circe here 的其他信息也表明,如果没有任何配置,您应该在编码 json.

中获得一些 class 类型信息

我错过了什么吗?如何显示 class 类型?

2017 年 3 月 30 日更新:跟进 OP 的评论

我能够完成这项工作,如链接 release notes 中所示。

准备步骤1:添加附加依赖到build.sbt

libraryDependencies += "io.circe" %% "circe-generic-extras" % "0.7.0"

准备步骤 2:设置虚拟密封特征层次结构

import io.circe.{ Decoder, Encoder }
import io.circe.parser._, io.circe.syntax._
import io.circe.generic.extras.Configuration
import io.circe.generic.extras.auto._
import io.circe.generic.{ semiauto => boring } // <- This is the default generic derivation behaviour
import io.circe.generic.extras.{ semiauto => fancy } // <- This is the new generic derivation behaviour

implicit val customConfig: Configuration = Configuration.default.withDefaults.withDiscriminator("type")

sealed trait Stuff
case class Foo(thisIsAString: String, anotherField: Int = 13) extends Stuff
case class Bar(thisIsAString: String, anotherField: Int = 13) extends Stuff

object Foo {
  implicit val decodeBar: Decoder[Bar] = fancy.deriveDecoder
  implicit val encodeBar: Encoder[Bar] = fancy.deriveEncoder
}

object Bar {
  implicit val decodeBar: Decoder[Bar] = boring.deriveDecoder
  implicit val encodeBar: Encoder[Bar] = boring.deriveEncoder
}

使用这个的实际代码:

val foo: Stuff = Foo("abc", 123)
val bar: Stuff = Bar("xyz", 987)

val fooString = foo.asJson.noSpaces
// fooString: String = {"thisIsAString":"abc","anotherField":123,"type":"Foo"}

val barString = bar.asJson.noSpaces
// barString: String = {"thisIsAString":"xyz","anotherField":987,"type":"Bar"}

val bar2 = for{
  json <- parse(barString)
  bar2 <- json.as[Stuff]
} yield bar2
// bar2: scala.util.Either[io.circe.Error,Stuff] = Right(Bar(xyz,987))

val foo2 = for{
  json <- parse(fooString)
  foo2 <- json.as[Stuff]
} yield foo2
// foo2: scala.util.Either[io.circe.Error,Stuff] = Right(Foo(abc,123))

因此,只要您导入额外的依赖项(Configuration 的来源),它看起来就可以工作。

最后,作为旁注,Circe's DESIGN.md和实践之间似乎确实存在一些脱节,对此我其实很高兴。


原回答: 我不确定这是否应该受设计支持。

摘自Circe's DESIGN.md

Implicit scope should not be used for configuration. Lots of people have asked for a way to configure generic codec derivation to use e.g. a type field as the discriminator for sealed trait hierarchies, or to use snake case for member names. argonaut-shapeless supports this quite straightforwardly with a JsonCoproductCodec type that the user can provide implicitly.

I don't want to criticize this approach—it's entirely idiomatic Scala, and it often works well in practice—but I personally don't like using implicit values for configuration, and I'd like to avoid it in circe until I am 100% convinced that there's no alternative way to provide this functionality.

What this means concretely: You'll probably never see an implicit argument that isn't a type class instance—i.e. that isn't a type constructor applied to a type in your model—in circe, and configuration of generic codec derivation is going to be relatively limited (compared to e.g. argonaut-shapeless) until we find a nice way to do this kind of thing with type tags or something similar.

特别是,customConfig: Configuration 似乎正是最后一段所指的参数类型(例如 不是 class 实例类型的隐式参数)

我相信 @travis-brown 或任何其他 Circe 的主要贡献者可以对此提供更多的说明,以防万一确实有办法做到这一点 - 我会很高兴知道它! :)