使用json4s反序列化嵌套的多态json字段

编程入门 行业动态 更新时间:2024-10-17 18:29:44
本文介绍了使用json4s反序列化嵌套的多态json字段的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我有一个常见问题,但仍然无法将自己的头围在阅读中.

I have a common problem but still couldn't wrap my head around what I was reading around.

在scalatra应用中,我收到以下json:

In a scalatra app, I am receiving the following json:

{ _type: "hello", timestamp: 123, data: [ {table: "stuffJ",_id: 24}, {table: "preferences",_id: 34,word: "john"} ]}

字段数据"中的元素数量未知.字段表将始终存在,以区分类类型.我正在尝试将其解析为类RestAPIMessage.这是我到目前为止的内容:

with an unknown number of elements in field 'data'. The field table will always be there to differentiate between class types. I am trying to have it parsed to class RestAPIMessage. This is what I have so far:

implicit val jsonFormats = new DefaultFormats { outer => override val typeHintFieldName = "table" override val typeHints = ShortTypeHints(List(classOf[Preferences], classOf[StuffJ])) } sealed trait DataJson case class Preferences(table: String, _id: Long, word : String) extends DataJson case class StuffJ(table: String, _id: Long) extends DataJson case class RestAPIMessage(_type: String, timestamp: Long, data: List[DataJson]) // if sent as Json, returns a json with two "table" fields val message = new RestAPIMessage("hello", 123, List(new StuffJ("StuffJ", 24), new Preferences("preferences", 34, "john"))) // if received as Json, fails with a "no usable value for outer" val djson = """{"_type":"hello","timestamp":123,"data":[{"table":"StuffJ","_id":24},{"table":"table":"preferences","_id":34,"word":"john"}]}"""

感谢您的帮助!

推荐答案

好的,我想我明白了. 最后,似乎我无法开箱即用",而不得不编写自定义序列化程序.我在附近找不到一个简单的多态"示例,因此我在下面提出了一个最小的foobar示例:

Alright I think I got this. In the end it seems I couldn't have what I needed "out of the box", and had to write a custom serializer. I couldn't find a simple "polymorphic" example around, so I put up a minimal foobar example below:

import org.json4s.{DefaultFormats, Formats} import org.json4s._ import org.json4s.JsonDSL._ import org.json4s.native.Serialization.{read, write} import org.json4s.native.Serialization sealed trait Bar case class Bar1(name : String, value: Int) extends Bar case class Bar2(name : String, stuff: Int) extends Bar case class Foo(timestamp:Long, bar: List[Bar]) var testFoo : Foo = new Foo(123, List(Bar1("bar1",1), Bar2("bar2",2))) object BarSerializer extends CustomSerializer[Bar](format => ( { case x: JObject => val name = (x \ "name").extract[String] name match { case "bar1" => val value = (x \ "value").extract[Int] Bar1(name,value) case "bar2" => val value = (x \ "stuff").extract[Int] Bar2(name,value) case x => throw new MappingException("Can't convert bar with name " + x + " to Bar") } }, { case x: Bar => //if you need only the deserializing part above, I think you could replace the below with write(x) x match { case Bar1(a,b) => ("name" -> a) ~ ("value" -> b) case Bar2(a,b) => ("name" -> a) ~ ("stuff" -> b) } } )) implicit val jsonFormats: Formats = Serialization.formats(NoTypeHints) + BarSerializer val a = write(testFoo) //returns : String = {"timestamp":123,"bar":[{"name":"bar1","value":1},{"name":"bar2","value":2}]} read[Foo](a) //returns : Foo = Foo(123,List(Bar1(bar1,1), Bar2(bar2,2)))

更多推荐

使用json4s反序列化嵌套的多态json字段

本文发布于:2023-07-14 14:44:37,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1105091.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:嵌套   字段   序列化   多态   json4s

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!