给出以下JSON:
{
"foo": "bar",
"baz":[
{ "qux" : "quux" },
{ "quuux" : "quuuux" }
]
}
将它表示为Scala案例类的最佳方法是什么?从逻辑上讲,它似乎应该是这样的:
case class Foo(
foo: String,
baz: List[(String, String)]
)
但是,当我尝试用Json4s和Jackson解析时,我得到了:
No usable value for baz
No usable value for _1
Did not find value which can be converted into java.lang.String
org.json4s.package$MappingException: No usable value for baz
No usable value for _1
Did not find value which can be converted into java.lang.String
如果我构建预期的Foo
...
val foo = Foo(foo = "bar", baz = List(("qux" -> "qux1"), ("qux" -> "qux2")))
...并将其写为JSON,我没有得到我的元组列表,我得到:
{
"foo" : "bar",
"baz" : [ {
"_1" : "qux",
"_2" : "qux1"
}, {
"_1" : "qux",
"_2" : "qux2"
} ]
}
我在this answer中看到尽管Json4s claims在DSL中从Tuple2s
生成对象,但实际上它不能用于对象字段,除非该对象字段定义为一个JValue
。由于除了序列化和反序列化之外我想用Foo
模型对象做其他事情,我不特别想用JValues
来定义它。
鉴于此,我应该做什么,这里?
答案 0 :(得分:1)
我看到至少有两个选项可以解决这个问题:
有时直接在AST上工作或通过values函数提取普通Map [String,Any]也很有用。只要您使用JValue DSL,Tuple2转换就有效,但在从Scala类型中提取/分解时则无效。
import org.json4s._
import org.json4s.jackson.JsonMethods._
import org.json4s.JsonDSL._
val json = """{
| "foo": "bar",
| "baz":[
| { "qux" : "quux" },
| { "quuux" : "quuuux" }
| ]
|}""".stripMargin
class StringTupleSerializer extends CustomSerializer[(String, String)](format => ( {
case JObject(List(JField(k, JString(v)))) => (k, v)
}, {
case (s: String, t: String) => (s -> t)
}))
implicit val formats = DefaultFormats + new StringTupleSerializer
case class FooMap(foo: String, baz: List[Map[String, String]])
case class FooTuple(foo: String, baz: List[(String, String)])
val ast = parse(json)
println(ast.extractOpt[FooMap])
// Some(FooWithMap(bar,List(Map(qux -> quux), Map(quuux -> quuuux))))
println(ast.extractOpt[FooTuple])
// Some(FooWithTuple(bar,List((qux,quux), (quuux,quuuux))))
val foo1 = FooMap(foo = "bar", baz = List(Map("qux" -> "qux1"), Map("qux" -> "qux2")))
val foo2 = FooTuple(foo = "bar", baz = List(("qux" -> "qux1"), ("qux" -> "qux2")))
println(pretty(Extraction.decompose(foo1)))
println(pretty(Extraction.decompose(foo2)))