我正在使用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
}
Run Code Online (Sandbox Code Playgroud)
假设有一个JSON对象,它有一个动物列表.Animal是一种抽象类型,因此无法实例化.相反,我想解析JSON结构以返回其中一个Dog或Bird对象.他们有不同的签名:
case class Dog(name: String) extends Animal
case class Bird(canFly: Boolean) extends Animal
Run Code Online (Sandbox Code Playgroud)
因为它们的签名是不同的,所以可以在JSON对象中没有类Tag的情况下识别它们.(确切地说,我收到的JSON结构不提供这些标签).
我试图序列化Animal对象列表(参见代码).结果是:Ser: {"animals":[{"jsonClass":"Dog","name":"pluto"},{"jsonClass":"Bird","canFly":true}]}
如您所见,在序列化时,json4s会添加class-tag jsonClass.
如何反序列化不提供此类标记的JSON对象?是否有可能通过扩展实现这一目标TypeHints?
我还发现了一个类似的问题:[json4s]:使用一种使用泛型而不是子类的解决方案来提取不同对象的数组.但是,如果我理解正确,此解决方案不允许简单地传递json对象并具有内部表示.相反,我需要选择不是的形式None(同时检查继承层次结构中的所有可能的类型.这有点单调乏味,因为我在JSON结构中有不同深度的多个多态类.
Kul*_*mpa 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
Run Code Online (Sandbox Code Playgroud)
在没有类型提示的情况下运行的自定义序列化程序如下所示:
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)))
}))
Run Code Online (Sandbox Code Playgroud)
由于该功能,+我们可以添加多个自定义序列化程序,同时保留默认的序列化程序.
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])
Run Code Online (Sandbox Code Playgroud)
版画
AnimalList(List(Dog(Pluto), Dog(Goofy), Bird(false), Dog(Rover)))
Run Code Online (Sandbox Code Playgroud)