用于变体json-field的定制circe解码器

时间:2018-04-05 21:51:27

标签: json scala circe

如何为类

编写circe解码器
case class KeyValueRow(count: Int, key: String)

其中json包含字段“count”(Int)和一些额外的字符串字段(此字段的名称可能各种各样,如“url”,“city”,等等)?

{"count":974989,"url":"http://google.com"}
{"count":1234,"city":"Rome"}

1 个答案:

答案 0 :(得分:1)

你可以这样做:

import io.circe.syntax._
import io.circe.parser._
import io.circe.generic.semiauto._
import io.circe.{ Decoder, Encoder, HCursor, Json, DecodingFailure}

object stuff{
  case class KeyValueRow(count: Int, key: String)

  implicit def jsonEncoder : Encoder[KeyValueRow] = deriveEncoder

  implicit def jsonDecoder : Decoder[KeyValueRow] = Decoder.instance{ h =>
    (for{
      keys <- h.keys
      key <- keys.dropWhile(_ == "count").headOption
    } yield { 
      for{
        count <- h.get[Int]("count")
        keyValue <- h.get[String](key)
      } yield KeyValueRow(count.toInt, keyValue)
    }).getOrElse(Left(DecodingFailure("Not a valid KeyValueRow", List())))
  }
}

import stuff._

val a = KeyValueRow(974989, "www.google.com")

println(a.asJson.spaces2)

val test1 = """{"count":974989,"url":"http://google.com"}"""
val test2 = """{"count":1234,"city":"Rome", "will be dropped": "who cares"}"""

val parsedTest1 = parse(test1).flatMap(_.as[KeyValueRow])
val parsedTest2 = parse(test2).flatMap(_.as[KeyValueRow])

println(parsedTest1)
println(parsedTest2)

println(parsedTest1.map(_.asJson.spaces2))
println(parsedTest2.map(_.asJson.spaces2))

Scalafiddle:link here

正如我在上面的评论中所提到的,请记住,如果你解码一些json,然后重新编码它,结果将与初始输入不同。要解决此问题,您需要跟踪关键字段的原始名称。