scala - 在json4s中提取多态类型

标签 scala json4s

我正在使用 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 结构以返回 DogBird对象。他们有不同的签名:
case class Dog(name: String) extends Animal
case class Bird(canFly: Boolean) extends Animal

因为它们的签名是不同的,所以它们可以在 JSON 对象中没有类 Tag 的情况下被识别。 (准确地说,我收到的 JSON 结构没有提供这些标签)。

我试图序列化一个 Animal 对象列表(见代码)。结果是:Ser: {"animals":[{"jsonClass":"Dog","name":"pluto"},{"jsonClass":"Bird","canFly":true}]}
可以看到,在序列化时,json4s 添加了 class-tag jsonClass .

如何反序列化不提供此类标记的 JSON 对象? 是否可以通过扩展 TypeHints 来实现这一点? ?

我也发现了一个类似的问题:[json4s]:Extracting Array of different objects使用以某种方式使用泛型而不是子类化的解决方案。但是,如果我理解正确,此解决方案不允许简单地传递 json 对象并具有内部表示。相反,我需要选择不是 None 的表单。 (同时检查继承层次结构中所有可能的类型。这有点乏味,因为我在 JSON 结构中的不同深度有多个多态类。

最佳答案

最终,在导致这个问题的项目中,我同意创建序列化 JSON 的人为所有多态类型添加类型提示。回想起来,这个解决方案可能是最干净的,因为它支持 JSON 模式的 future 扩展,而不会引入歧义。

尽管如此,对于实际问题,存在一个相当简单的解决方案(不仅仅是一种解决方法)。

型号org.json4s.Formats ,它是我们作用域中的一个隐式值,提供了一个函数 +(org.json4s.Serializer[A]) .此功能允许我们添加新的自定义序列化程序。因此,对于每个多态父类(super class)型(在我们的例子中,这仅涉及 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)))

关于scala - 在json4s中提取多态类型,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25980949/

相关文章:

java - Java库Args4j和Scala 2.10之间的互操作

scala - 处理枚举类型的通用映射函数

scala - 关闭 Scala 秋千架

scala - 无法将 ScalaCheck 与 Scala IDE for Eclipse 结合使用

scala - 将 JValue 转换为 JSON 字符串

json - 如何在 Spark 2 Scala 中将 Row 转换为 json

scala - 优化 Scala 代码时首先看哪里?

json - 使用 JSON4S 在重复节点上合并/组合 JSON

scala - 在 Scala json4s 中获取一个字段

scala - 当缺少必填字段时,是否可以使 json4s 不抛出异常?