在json4s中提取多态类型

时间:2014-09-22 18:37:53

标签: 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会添加类标记jsonClass

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

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

1 个答案:

答案 0 :(得分:15)

最终,在导致这个问题的项目中,我同意创建序列化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)))