从蛇格钥匙圈解析json

时间:2019-04-26 11:52:30

标签: scala circe

我有以下案例类:

final case class Camel(firstName: String, lastName: String, waterPerDay: Int)

和圆圈配置:

object CirceImplicits {

  import io.circe.syntax._
  import io.circe.generic.semiauto._
  import io.circe.{Encoder, Decoder, Json}
  import io.circe.generic.extras.Configuration

  implicit val customConfig: Configuration =
    Configuration.default.withSnakeCaseMemberNames.withDefaults
  implicit lazy val camelEncoder: Encoder[Camel] = deriveEncoder
  implicit lazy val camelDecoder: Decoder[Camel] = deriveDecoder
}

可以,对此进行测试:

val camel = Camel(firstName = "Camelbek", lastName = "Camelov", waterPerDay = 30)

private val camelJ = Json.obj(
    "firstName" -> Json.fromString("Camelbek"),
    "lastName" -> Json.fromString("Camelov"),
    "waterPerDay" -> Json.fromInt(30)
)

"Decoder" must "decode camel types" in {
    camelJ.as[Camel] shouldBe Right(camel)
}

但是此测试未通过:

val camel = Camel(firstName = "Camelbek", lastName = "Camelov", waterPerDay = 30)

private val camelJ = Json.obj(
    "first_name" -> Json.fromString("Camelbek"),
    "last_name" -> Json.fromString("Camelov"),
    "water_per_day" -> Json.fromInt(30)
)

"Decoder" must "decode camel types" in {
    camelJ.as[Camel] shouldBe Right(camel)
}

如何正确配置circe以便能够在蛇形情况下使用键解析json?

我正在使用circe版本0.10.0

1 个答案:

答案 0 :(得分:3)

解决方案1 ​​

Circe从您的案例类实例中获取字段名称,并使用游标遍历JSON,尝试获取每个字段名称的值并将其转换为所需的类型。

这意味着您的解码器将无法处理这两种情况。

此问题的解决方案是编写两个解码器:

  1. 基本解码器(deriveEncoder将可用)
  2. 使用HCursor编码器浏览JSON并获取蛇形例密钥的编码器
val decoderDerived: Decoder[Camel] = deriveDecoder
val decoderCamelSnake: Decoder[Camel] = (c: HCursor) =>
    for {
      firstName <- c.downField("first_name").as[String]
      lastName <- c.downField("last_name").as[String]
      waterPerDay <- c.downField("water_per_day").as[Int]
    } yield {
      Camel(firstName, lastName, waterPerDay)
    }

然后您可以使用Decoder#or

将这两个解码器组合为一个
implicit val decoder: Decode[Camel] = decoderDerived or decoderCamelSnake

Decoder#or将尝试使用第一个解码器进行解码,如果失败,则将尝试第二个解码器。

解决方案2

如果只使用camel_case输入是可以的,则可以使用@ConfiguredJsonCodec包中的"io.circe" %% "circe-generic-extras" % circeVersion。请注意,要使用此注释,您还需要包括天堂编译器插件。

addCompilerPlugin(
  "org.scalamacros" % "paradise" % "2.1.1" cross CrossVersion.full
)
@ConfiguredJsonCodec
case class User(
  firstName: String,
  lastName: String
)

object User {
  implicit val customConfig: Configuration = Configuration.default.withSnakeCaseMemberNames
}

val userJson = User("John", "Doe").asJson
println(userJson)
// { "first_name" : "John", "last_name" : "Doe" } 

val decodedUser = decode[User](userJson.toString)
println(decodedUser)
// Right(User("John", "Doe"))

还请注意,您不需要编写自定义解码器和编码器派生程序,因为该配置可以为您完成此工作。