15

起源

{
  "first_name" : "foo",
  "last_name" : "bar",
  "parent" : {
    "first_name" : "baz",
    "last_name" : "bazz",
  }
}

预期的

 {
      "firstName" : "foo",
      "lastName" : "bar",
      "parent" : {
        "firstName" : "baz",
        "lastName" : "bazz",
      }
    }

如何转换 json 对象的所有键?

4

4 回答 4

11

这就是我写这个的方式。它不像我想要的那样简洁,但并不可怕:

import cats.free.Trampoline
import cats.std.list._
import cats.syntax.traverse._
import io.circe.{ Json, JsonObject }

/**
 * Helper method that transforms a single layer.
 */
def transformObjectKeys(obj: JsonObject, f: String => String): JsonObject =
  JsonObject.fromIterable(
    obj.toList.map {
      case (k, v) => f(k) -> v
    }
  )

def transformKeys(json: Json, f: String => String): Trampoline[Json] =
  json.arrayOrObject(
    Trampoline.done(json),
    _.traverse(j => Trampoline.suspend(transformKeys(j, f))).map(Json.fromValues),
    transformObjectKeys(_, f).traverse(obj => Trampoline.suspend(transformKeys(obj, f))).map(Json.fromJsonObject)
  )

接着:

import io.circe.literal._

val doc = json"""
{
  "first_name" : "foo",
  "last_name" : "bar",
  "parent" : {
    "first_name" : "baz",
    "last_name" : "bazz"
  }
}
"""

def sc2cc(in: String) = "_([a-z\\d])".r.replaceAllIn(in, _.group(1).toUpperCase)

最后:

scala> import cats.std.function._
import cats.std.function._

scala> transformKeys(doc, sc2cc).run
res0: io.circe.Json =
{
  "firstName" : "foo",
  "lastName" : "bar",
  "parent" : {
    "firstName" : "baz",
    "lastName" : "bazz"
  }
}

我们可能应该有某种方法Json => F[Json]更方便地递归应用这样的转换。

于 2016-06-03T16:54:05.370 回答
3

根据您的完整用例,对于最新的 Circe,您可能更喜欢根据这些参考资料利用现有的解码器/编码器在骆驼/蛇之间进行转换:

例如,在我的特定用例中,这是有道理的,因为我正在执行其他操作,这些操作受益于首先反序列化为案例类的类型安全性。因此,如果您愿意将 JSON 解码为案例类,然后将其编码回 JSON,那么您所需要的只是(反)序列化代码来扩展配置它的特征,例如:

import io.circe.derivation._
import io.circe.{Decoder, Encoder, ObjectEncoder, derivation}
import io.circe.generic.auto._
import io.circe.parser.decode
import io.circe.syntax._

trait JsonSnakeParsing {
  implicit val myCustomDecoder: Decoder[MyCaseClass] = deriveDecoder[MyCaseClass](io.circe.derivation.renaming.snakeCase)
  // only needed if you want to serialize back to snake case json:
  // implicit val myCustomEncoder: ObjectEncoder[MyCaseClass] = deriveEncoder[MyCaseClass](io.circe.derivation.renaming.snakeCase)
}

例如,当我实际解析或输出 JSON 时,我会扩展它:

trait Parsing extends JsonSnakeParsing {

  val result: MyCaseClass = decode[MyCaseClass](scala.io.Source.fromResource("my.json").mkString) match {
    case Left(jsonError) => throw new Exception(jsonError)
    case Right(source) => source
  }

  val theJson = result.asJson
}

对于此示例,您的案例类可能如下所示:

case class MyCaseClass(firstName: String, lastName: String, parent: MyCaseClass)

这是此示例的完整 circe 依赖项列表:

val circeVersion = "0.10.0-M1"

"io.circe" %% "circe-generic" % circeVersion,
"io.circe" %% "circe-parser" % circeVersion,
"io.circe" %% "circe-generic-extras" % circeVersion,
"io.circe" %% "circe-derivation" % "0.9.0-M5",
于 2018-08-14T17:52:51.963 回答
2
def transformKeys(json: Json, f: String => String): TailRec[Json] = {
      if(json.isObject) {
        val obj = json.asObject.get
        val fields = obj.toList.foldLeft(done(List.empty[(String, Json)])) { (r, kv) =>
          val (k, v) = kv
          for {
            fs <- r
            fv <- tailcall(transformKeys(v, f))
          } yield fs :+ (f(k) -> fv)
        }
        fields.map(fs => Json.obj(fs: _*))
      } else if(json.isArray) {
        val arr = json.asArray.get
        val vsRec = arr.foldLeft(done(List.empty[Json])) { (vs, v) =>
          for {
            s <- vs
            e <- tailcall(transformKeys(v, f))
          } yield s :+ e
        }
        vsRec.map(vs => Json.arr(vs: _*))
      } else {
        done(json)
      }
    }

目前我确实像这样进行转换,但是相当复杂,希望有一个简单的方法。

于 2016-06-02T07:54:38.583 回答
0

我接受了@Travis 的回答并对其进行了一些现代化改造,我接受了他的代码,但我遇到了一些错误和警告,因此带有 Cats 1.0.0-MF 的 Scala 2.12 的更新版本:

import io.circe.literal._
import cats.free.Trampoline, cats.instances.list._, cats.instances.function._, cats.syntax.traverse._, cats.instances.option._

def transformKeys(json: Json, f: String => String): Trampoline[Json] = {
  def transformObjectKeys(obj: JsonObject, f: String => String): JsonObject =
    JsonObject.fromIterable(
      obj.toList.map {
        case (k, v) => f(k) -> v
      }
    )
  json.arrayOrObject(
    Trampoline.done(json),
    _.toList.traverse(j => Trampoline.defer(transformKeys(j, f))).map(Json.fromValues(_)),
    transformObjectKeys(_, f).traverse(obj => Trampoline.defer(transformKeys(obj, f))).map(Json.fromJsonObject)
  )
}

def sc2cc(in: String) = "_([a-z\\d])".r.replaceAllIn(in, _.group(1).toUpperCase)

def camelizeKeys(json: io.circe.Json) = transformKeys(json, sc2cc).run
于 2017-09-16T16:49:34.997 回答