反序列化嵌套多态字段

时间:2019-07-02 11:38:46

标签: json scala json4s

是否可以反序列化以下JSON

{
  "operation": "create", // type discriminator
  "value": "some text",
  "source": {
    "name": "source name",
    "kind": "db" // type discriminator
  }
}

进入以下一组课程

sealed trait Source

case class DbSource(name: String) extends Source

sealed trait Operation

case class CreateOperation(value: String, source: Source) extends Operation

使用Json4s而不求助于自定义反序列化器吗?

2 个答案:

答案 0 :(得分:0)

Json库Circe提供了您所需要的东西:The relevant page

implicit val sourceConfig: Configuration =
  Configuration.default.withDiscriminator("operation")

答案 1 :(得分:0)

不了解json4s,不过您可以使用jackson来做到这一点: http://chromedriver.chromium.org/downloads