我想在我的案例类和我的 JSON 中有不同的字段名称,因此我需要一种舒适的重命名方式,编码和解码。
有人有好的解决方案吗?
最佳答案
您可以使用 Custom key mappings via annotations .最通用的方式是JsonKey
注释来自 io.circe.generic.extras._
.文档中的示例:
import io.circe.generic.extras._, io.circe.syntax._
implicit val config: Configuration = Configuration.default
@ConfiguredJsonCodec case class Bar(@JsonKey("my-int") i: Int, s: String)
Bar(13, "Qux").asJson
// res5: io.circe.Json = JObject(object[my-int -> 13,s -> "Qux"])
这需要包
circe-generic-extras
.
关于json - 使用 circe 重命名 JSON 字段,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40306107/