我正在使用json4s在我的Scala代码中处理JSON对象。我希望将JSON数据转换为内部表示。下面的学习测试说明了我的问题:
"Polimorphic deserailization" should "be possible" in {
import org.json4s.jackson.Serialization.write
val json =
"""
|{"animals": [{
| "name": "Pluto"
| }]
|}
""".stripMargin
implicit val format = Serialization.formats(ShortTypeHints(List(classOf[Dog], classOf[Bird])))
val animals = parse(json) \ "animals"
val ser = write(Animals(Dog("pluto") :: Bird(canFly = true) :: Nil))
System.out.println(ser)
// animals.extract[Animal] shouldBe Dog("Pluto") // Does not deserialize, because Animal cannot be constructed
}假设有一个JSON对象,它有一个动物列表。Animal是一种抽象类型,因此不能实例化。相反,我希望解析JSON结构以返回Dog或Bird对象。他们有一个不同的签名:
case class Dog(name: String) extends Animal
case class Bird(canFly: Boolean) extends Animal因为它们的签名是不同的,所以可以在JSON对象中不使用类标记来识别它们。(准确地说,我收到的JSON结构没有提供这些标记)。
我试图序列化一个动物对象列表(请参阅代码)。结果是:Ser: {"animals":[{"jsonClass":"Dog","name":"pluto"},{"jsonClass":"Bird","canFly":true}]}
正如您所看到的,在序列化时,json4s添加了类标记jsonClass。
如何反序列化不提供这样一个标记的JSON对象?可以通过扩展TypeHints来实现这一点吗?
我还发现了一个类似的问题:[json4s]:Extracting Array of different objects有一个解决方案,它以某种方式使用泛型而不是子类。但是,如果我正确理解,此解决方案不允许简单地传递json对象并具有内部表示形式。相反,我需要选择不是None的表单(同时检查继承中所有可能的类型。这有点乏味,因为我在JSON结构中有不同深度的多个多态类。
发布于 2014-11-26 23:40:19
最终,在导致这个问题的项目上,我同意了创建序列化JSON的人关于为所有多态类型添加类型提示的家伙的意见。回顾一下,这个解决方案可能是最干净的,因为它支持JSON模式的未来扩展,而不会引入歧义。
然而,对于实际的问题,有一个相当简单的解决方案(而不仅仅是解决办法)。
类型org.json4s.Formats是我们作用域中的隐式值,它提供了一个函数+(org.json4s.Serializer[A])。此函数允许我们添加新的自定义序列化程序。因此,对于每个多态超级类型(在我们的示例中,这只涉及Animal),我们可以定义一个自定义序列化程序。在我们的例子中,我们有
trait Animal
case class Dog(name: String) extends Animal
case class Bird(canFly: Boolean) extends Animal在没有类型提示的情况下操作的自定义序列化程序看起来如下:
class AnimalSerializer extends CustomSerializer[Animal](format => ( {
case JObject(List(JField("name", JString(name)))) => Dog(name)
case JObject(List(JField("canFly", JBool(canFly)))) => Bird(canFly)
}, {
case Dog(name) => JObject(JField("name", JString(name)))
case Bird(canFly) => JObject(JField("canFly", JBool(canFly)))
}))由于函数+,我们可以添加多个自定义序列化程序,同时保留默认序列化程序。
case class AnimalList(animals: List[Animal])
val json =
"""
|{"animals": [
| {"name": "Pluto"},
| {"name": "Goofy"},
| {"canFly": false},
| {"name": "Rover"}
| ]
|}
""".stripMargin
implicit val format = Serialization.formats(NoTypeHints) + new AnimalSerializer
println(parse(json).extract[AnimalList])版画
AnimalList(List(Dog(Pluto), Dog(Goofy), Bird(false), Dog(Rover)))https://stackoverflow.com/questions/25980949
复制相似问题