Jackson scala模块:反序列化案例对象枚举

问题描述 投票:0回答:1

我正在使用Jackson的scala模块,我需要对案例对象枚举进行序列化/反序列化。序列化工作正常,但是反序列化值时得到的是com.fasterxml.jackson.databind.exc.InvalidDefinitionException。有办法使它起作用吗?我想避免使用“经典” scala枚举,因为我将失去定义枚举层次结构的能力。

用于测试的代码:

import com.fasterxml.jackson.annotation.JsonValue
import com.fasterxml.jackson.databind.{DeserializationFeature, ObjectMapper}
import com.fasterxml.jackson.module.scala.DefaultScalaModule
import com.fasterxml.jackson.module.scala.experimental.ScalaObjectMapper


sealed trait Operations { @JsonValue def jsonValue: String }

case object Foo extends Operations { override def jsonValue: String = "Foo" }

case object Bar extends Operations { override def jsonValue: String = "Bar" }


sealed trait RichOperations extends Operations

case object Baz extends RichOperations { override def jsonValue: String = "Baz" }


object JsonUtils extends App {

  val jsonMapper = new ObjectMapper() with ScalaObjectMapper
  jsonMapper.registerModule(DefaultScalaModule)
    .configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false)

  def toJson(value: Any): String = jsonMapper.writeValueAsString(value)

  def fromJson[T: Manifest](json: String): T = jsonMapper.readValue[T](json)


  println(toJson(Foo)) // "Foo"
  println(toJson(Bar)) // "Bar"
  println(toJson(Baz)) // "Baz"
  println(fromJson[Operations](toJson(Foo))) // throws InvalidDefinitionException
  println(fromJson[Operations](toJson(Bar)))
  println(fromJson[RichOperations](toJson(Baz)))
}
json scala jackson jackson2
1个回答
0
投票

我解决了定义自定义反序列化器的问题。

import com.fasterxml.jackson.core.JsonParser
import com.fasterxml.jackson.databind.{DeserializationContext, JsonDeserializer}


class OperationsDeserializer extends JsonDeserializer[Operations] {

  override def deserialize(p: JsonParser, ctxt: DeserializationContext): Operations = {
    p.getValueAsString match {
      case Foo.jsonValue => Foo
      case Bar.jsonValue => Bar
      case value => throw new IllegalArgumentException(s"Undefined deserializer for value: $value")
    }
  }
}


class RichOperationsDeserializer extends JsonDeserializer[Operations] {

  override def deserialize(p: JsonParser, ctxt: DeserializationContext): Operations = {
    p.getValueAsString match {
      case Foo.jsonValue => Foo
      case Bar.jsonValue => Bar
      case Baz.jsonValue => Baz
      case value => throw new IllegalArgumentException(s"Undefined deserializer for value: $value")
    }
  }
}

此外,由于匹配/大小写需要稳定的标识符,因此我将jsonValuedef更改为val:不幸的是,在每种情况下,对象枚举都需要声明@JsonValue批注。

import com.fasterxml.jackson.databind.annotation.JsonDeserialize


@JsonDeserialize(using = classOf[OperationsDeserializer])
sealed trait Operations { val jsonValue: String }

case object Foo extends Operations { @JsonValue override val jsonValue: String = "Foo" }

case object Bar extends Operations { @JsonValue override val jsonValue: String = "Bar" }


@JsonDeserialize(using = classOf[RichOperationsDeserializer])
sealed trait RichOperations extends Operations

case object Baz extends RichOperations { @JsonValue override val jsonValue: String = "Baz" }
© www.soinside.com 2019 - 2024. All rights reserved.