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对象有一个动物列表.动物是一种抽象类型,因此不能被实例化.相反,我想解析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]:Extracting Array of different objects与解决方案,以某种方式使用泛型而不是子类化.但是,如果我理解正确,此解决方案不允许简单地传递json对象并具有内部表示.相反,我需要选择不是None的表单(同时检查继承hiearchy中的所有可能的类型),这是有点乏味,因为我在JSON结构中有不同深度的多个多态类. 解决方法
最终,在导致这个问题的项目中,我同意为所有多态类型添加类型提示创建序列化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))) (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |
- webservice 两种消息格式 SoapDocumentProtocol,
- WebService 的一些基本概念-2 (PortType, Bindin
- typescript – Angular 2:ReferenceError:requ
- 如何从.vimrc创建文件夹(如果不存在)?
- 创建快捷方式到桌面、任务栏、开始菜单、快速启动
- 用空格替换所有逗号 – 使用Unix命令替换S / R?
- 用于varargs方法的Scala类型边界
- Bootstrap框架----多条记录双文本(List)添加
- angularjs – 即使日期有效,md-datepicker也会显
- SOAP webserivce 和 RESTful webservice 对比及区