问题 在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结构以返回 Dog 要么 Bird 对象。他们有不同的签名:

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]:提取不同对象的数组 一个解决方案,以某种方式使用泛型而不是子类。但是,如果我理解正确,此解决方案不允许简单地传递json对象并具有内部表示。相反,我需要选择不是的形式 None (同时检查继承层次结构中所有可能的类型。这有点单调乏味,因为我在JSON结构中有不同深度的多个多态类。


1449
2017-09-22 18:37


起源

你有没有找到答案?我在这面临着同样的挑战...... - borck
不幸的是,我没有找到答案。作为一种解决方法,我同意创建序列化JSON以添加类型提示的人;但如果你不能影响JSON方案,这显然不是解决方案。我仍然对答案感兴趣,对json4s的了解比我在编写问题时的知识要多,所以我会尝试提出一个解决方案。 - Kulu Limpa
@borck感谢你重温这个问题。我找到了扩展 CustomSerializer是一个相当简单的解决方案(虽然提取大型多态结构的代码可能会变得有点膨胀)。我希望这也可以帮助您解决问题。 - Kulu Limpa


答案:


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

15
2017-11-26 23:40



这确实有效,感谢您追求您的调查...我觉得如果您抓住json制作人的类型提示效果会更好,因为这确实会变得迅速膨胀...... - borck