请考虑:
import io.circe.generic.auto._, io.circe.syntax._
sealed trait Data
case class Failed(foo: String, bar: String) extends Data
case class Success(foo1:String, bar1:String) extends Data
case class Task(Foo:String, Data: Data)
val something = Task("test", Failed("1", "2"))
println(something.asJson)
这将输出:
val something: Task = Task(test,Failed(1,2))
{"Foo" : "test", "Data" : {"Failed" : {"foo" : "1","bar" : "2"}}}
但我真正想要的,是它能够输出:
{"Foo" : "test", "Data" : {"foo" : "1", "bar" : "2"}}
实际上,我只想删除“失败”块,但保留该块中的所有内容。
组建信息:
val scalaVer = "2.13.8"
lazy val circeJsonSchemaVersion = "0.2.0"
lazy val circeVersion = "0.14.3"
lazy val circeOpticsVersion = "0.14.1"
"io.circe" %% "circe-json-schema" % circeJsonSchemaVersion,
"io.circe" %% "circe-core" % circeVersion,
"io.circe" %% "circe-generic" % circeVersion,
"io.circe" %% "circe-parser" % circeVersion,
"io.circe" %% "circe-literal" % circeVersion,
"io.circe" %% "circe-generic-extras" % circeVersion,
"io.circe" %% "circe-optics" % circeOpticsVersion,
我试过使用@JsonCodec,但无法让它工作。我看了自定义的编解码器,但似乎它可能是一个巨大的兔子洞。
编辑:修复了输出中的错误复制/粘贴
1条答案
按热度按时间erhoui1w1#
更通用的解决方案建议
但这似乎已经过时了。
您可以尝试自己手动导出无标签联产品的编码器(即
Failed :+: Success :+: CNil
形式的联产品,而不是FieldType[Symbol @@ "Failed", Failed] :+: FieldType[Symbol @@ "Success", Success] :+: CNil
)这适用于
semiauto
和与
auto