您可以通过注释使用自定义键映射。最通用的方法是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"])
Run Code Online (Sandbox Code Playgroud)
这需要包circe-generic-extras。
您可以使用mapJson编码器上的函数从通用编码器派生编码器并重新映射您的字段名称。
您可以使用prepareDecoder 上的函数来转换传递给通用 Decoder 的 JSON。
您也可以从头开始编写这两个解决方案,但这可能是大量的样板文件,这些解决方案都应该最多只有几行。