【问题标题】:Deserialize nested polymorphic json field with json4s使用 json4s 反序列化嵌套的多态 json 字段
【发布时间】:2015-10-29 11:35:39
【问题描述】:

我有一个常见问题,但仍然无法理解我正在阅读的内容。

在一个 scalatra 应用程序中,我收到以下 json:

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

字段“数据”中的元素数量未知。字段表将始终存在以区分类类型。我正在尝试将其解析为 RestAPIMessage 类。这是我目前所拥有的:

    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"}]}"""

感谢您的帮助!

【问题讨论】:

    标签: scala json-deserialization json4s


    【解决方案1】:

    好吧,我想我明白了。 最后,我似乎无法“开箱即用”我需要的东西,不得不编写一个自定义序列化程序。我找不到一个简单的“多态”示例,所以我在下面提供了一个最小的 foobar 示例:

    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)))
    

    【讨论】:

      猜你喜欢
      • 2023-03-17
      • 1970-01-01
      • 2017-12-07
      • 1970-01-01
      • 1970-01-01
      • 2022-01-22
      • 2017-05-19
      • 2016-10-14
      • 2011-06-03
      相关资源
      最近更新 更多