- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我有一些 json,其中包含一些字段,这些字段被压缩为 bson-ish 格式,如 {"foo.bar" : "bash"}
.我想将其转换为以下表示 {"foo" : { "bar" : "bash"}}
并想知道我会在哪里做这样的手术。使问题复杂化的是可能有多个这样的字段需要正确合并,例如{"foo.bar" : "a", "foo.bash" : "b", "foo.baz" : "c"}
-> {"foo" : { "bar" : "a", "bash" : "b", "baz" : "c"}}
.
最佳答案
这是一个快速实现:
import io.circe.Json
val Dotted = "([^\\.]*)\\.(.*)".r
def expandDotted(j: Json): Json = j.arrayOrObject(
j,
js => Json.fromValues(js.map(expandDotted)),
_.toList.map {
case (Dotted(k, rest), v) => Json.obj(k -> expandDotted(Json.obj(rest -> v)))
case (k, v) => Json.obj(k -> expandDotted(v))
}.reduceOption(_.deepMerge(_)).getOrElse(Json.obj())
)
scala> import io.circe.literal._
import io.circe.literal._
scala> val j1 = json"""{"foo.bar" : "a", "foo.bash" : "b", "foo.baz" : "c"}"""
j1: io.circe.Json =
{
"foo.bar" : "a",
"foo.bash" : "b",
"foo.baz" : "c"
}
scala> expandDotted(j1)
res1: io.circe.Json =
{
"foo" : {
"baz" : "c",
"bash" : "b",
"bar" : "a"
}
}
scala> expandDotted(json"""{ "x.y.z": true, "a.b": { "c": 1 } }""")
res2: io.circe.Json =
{
"a" : {
"b" : {
"c" : 1
}
},
"x" : {
"y" : {
"z" : true
}
}
}
scala> expandDotted(json"""{ "a.b": true, "x": 1 }""").noSpaces
res3: String = {"x":1,"a":{"b":true}}
Json#deepMerge
的行为。 :
scala> expandDotted(json"""{ "a.b": true, "a": 1 }""").noSpaces
res4: String = {"a":1}
scala> expandDotted(json"""{ "a": 1, "a.b": true }""").noSpaces
res5: String = {"a":{"b":true}}
关于json - 使用 circe 预处理点符号样式字段,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44057439/
我正在尝试为菊石 REPL 创建一个 predef.sc 文件。这是我写的 val fs2Version = "2.2.2" val circeVersion = "0.13.0" // fs2 in
我正在尝试为两个案例类生成编码器和解码器: object EventBusCases { case class ValuationRequest(function: RequestValue =
我已经为 JSON 表示定义了几个 case 类,但我不确定我是否做得正确,因为有很多嵌套的 case 类。 spec、meta 等实体属于 JSONObject 类型以及自定义对象本身。 这是我定义
为什么我得到错误 could not find Lazy implicit value of type io.circe.generic.decoding.DerivedDecoder[A$A6.th
我正在尝试将一些类编码为 json 字符串,但是无论我尝试什么,我的类似乎都无法为我正在使用的案例类找到隐式编码器。 这是我能够精简到的最小示例。 import io.circe._ import i
我以前见过类似的问题,但没有一个有效。我认为他们会问一些不同的问题,所以我在这里问。 我在一个文件中有这样的东西: sealed trait Thing case class SomeThing()
我的目标是将 JSON 转换为以下模型: case class Container(typeId: Int, timestamp: Long, content: Content) sealed tra
我想用 Circe 解码以下 ADT: sealed trait PaymentType object PaymentType extends EnumEncoder[PaymentType] {
我想将案例类的 Array[Byte] 字段编码为 Base64 字符串。出于某种原因,Circe 没有使用默认编解码器选择我的编解码器,而是将字节数组转换为 json 整数数组。 我应该怎么做才能修
我正在尝试使用 scala json 库 Circe,将它包装在一个简单的 trait 中以提供与 json 的转换,我有以下内容: import io.circe.generic.auto._ im
我试图将“5m”或“5s”或“5ms”形式的字符串解码为 FiniteDuration 类型的对象,分别为 5.minutes、5.seconds、5.milliseconds。 我正在尝试为涉及 F
当字段可以具有不同的原始值类型时,我在解析 json 时遇到问题。例如,我可以得到 json: { "name" : "john", "age" : 31 } 或者它可以是这种形式: {
我有一些 json,其中包含一些字段,这些字段被压缩为 bson-ish 格式,如 {"foo.bar" : "bash"} .我想将其转换为以下表示 {"foo" : { "bar" : "bash
我正在尝试实现类似的东西 object Claims { import shapeless._ import shapeless.labelled.FieldType import io.
我正在尝试使用 Circe 对对象列表进行编码,看起来类似于: val test = Seq(MyObject("hello", None, 1, 2, None) 我正在尝试使用 Circe 解析它
我的问题有点棘手。我有一个看起来像这样的案例类 case class Foo( id: String, name: String, field1: Boolean, f
我有以下案例类: final case class Camel(firstName: String, lastName: String, waterPerDay: Int) 和circe配置: obj
有没有办法将单个 None 字段序列化为“null”? 例如: // When None, I'd like to serialize only f2 to `null` case class Exa
我是 Scala 新手,正在使用 circe 来建模和序列化一些 API 响应。我发现自己使用以下样板 sealed trait SomeTrait object SomeTrait { im
可以有一个如下所示的类: case class Amount(value: Int) case class Data(insurance: Option[Amount], itemPrice: Amo
我是一名优秀的程序员,十分优秀!