0

鉴于这个简单的示例,第三个测试“反序列化”失败并显示消息。

Can not construct instance of com.egc.ost.pricing.contracts.response.A, problem: abstract types either need to be mapped to concrete types, have custom deserializer, or be instantiated with additional type information
 at [Source: java.io.StringReader@1f03691; line: 2, column: 29]
com.fasterxml.jackson.databind.JsonMappingException: Can not construct instance of com.egc.ost.pricing.contracts.response.A, problem: abstract types either need to be mapped to concrete types, have custom deserializer, or be instantiated with additional type information
 at [Source: java.io.StringReader@1f03691; line: 2, column: 29] 

即使第二个测试“de-serialize_a”证明杰克逊可以解析正确的多态类型。

我正在使用杰克逊版本和 scala 模块

  <dependency>
        <groupId>com.fasterxml.jackson.module</groupId>
        <artifactId>jackson-module-scala_2.10</artifactId>
        <version>2.2.2</version>
    </dependency>
    <dependency>
        <groupId>com.fasterxml.jackson.datatype</groupId>
        <artifactId>jackson-datatype-joda</artifactId>
        <version>2.2.2</version>
    </dependency>

代码:

@JsonTypeInfo(use = JsonTypeInfo.Id.NAME, include = JsonTypeInfo.As.EXTERNAL_PROPERTY, property = "type")
@JsonSubTypes(Array(
  new Type(value = classOf[B], name = "B"),
  new Type(value = classOf[C], name = "C")))
trait A{}

case class B(value : Double) extends A
case class C(value : String) extends A

case class Owner( results: Seq[(String, A)])

class ATest extends FlatSpec with ShouldMatchers {
  behavior of "A"

  it should "serialise"  in {
    val owner : Owner = Owner(Seq(("ExampleB",B(1.0)),("ExampleC",C("One"))))
    val serialize: String = JsonMarshall.serialize(owner)
    println(serialize)
  }

  it should "de-serialize_a" in {
    val a: A = JsonMarshall.deserialize[A]("""{
                                             |    "type" : "C",
                                             |    "value" : "One"
                                             |  }""".stripMargin)
    println(a)
  }

  val json = """{
               |  "results" : [ [ "ExampleB", {
               |    "type" : "B",
               |    "value" : 1.0
               |  } ], [ "ExampleC", {
               |    "type" : "C",
               |    "value" : "One"
               |  } ] ]
               |}""".stripMargin

  it should "de-serialize" in {
    val owner: Owner = JsonMarshall.deserialize[Owner](json)
    println(owner)
  }

}
4

2 回答 2

0

版本2.2.3中对此问题进行了修复。使用这个版本避免了对包装类的需要。

于 2014-11-07T13:04:52.507 回答
0

我相信这里有两个错误,一个与 Map 相关,另一个与 Seq(tuple)

1) 映射不导出序列化类型信息

2) Seq(Tuple) 忽略反序列化的类型信息

解决方法: - 使用包装类替换元组。

@JsonTypeInfo(use = JsonTypeInfo.Id.NAME, include = JsonTypeInfo.As.EXTERNAL_PROPERTY, property = "type")
@JsonSubTypes(Array(
  new Type(value = classOf[B], name = "B"),
  new Type(value = classOf[C], name = "C")))
trait A{}

case class B(value : Double) extends A
case class C(value : String) extends A

case class ContainerWithMap( results: Map[String, A])
case class ContainerWithTupleSeq( results: Seq[(String, A)])
case class ContainerWithWrappedSeq( results: Seq[WrapperClass])

case class WrapperClass(s : String, a : A)

class ATest extends FlatSpec with ShouldMatchers {
  behavior of "A"
  val map: Map[String, A] = Map("ExampleB" -> B(1.0), "ExampleC" -> C("One"))
  val seq: Seq[WrapperClass] = Seq(WrapperClass("ExampleB", B(1.0)), WrapperClass( "ExampleC",C("One")))

  it should "fail not supporting reciprocal serialize de-serialize behaviour for maps"  in {
    val owner : ContainerWithMap = ContainerWithMap(map)
    val serialize: String = JsonMarshall.serialize(owner)
    println(serialize)      // types not exported on serialization 
    val thrown = evaluating{JsonMarshall.deserialize[ContainerWithMap](serialize)} should produce [JsonMappingException]
    thrown.getMessage should startWith("Unexpected token (END_OBJECT), expected FIELD_NAME: missing property 'type' that is to contain type id")
  }

  it should "fail not supporting reciprocal serialize de-serialize behaviour for sequence of tuples"   in {
    val owner : ContainerWithTupleSeq = ContainerWithTupleSeq(map.toSeq)
    val serialize: String = JsonMarshall.serialize(owner)
    println(serialize)     // types ignored on de-Serialization
    val thrown = evaluating{JsonMarshall.deserialize[ContainerWithTupleSeq](serialize)} should produce [JsonMappingException]
    thrown.getMessage should startWith("Can not construct instance of com.egc.ost.pricing.contracts.response.A, problem: " +
      "abstract types either need to be mapped to concrete types, have custom deserializer, or be instantiated with additional type information")
  }

  it should "work if using a wrapper class"  in {
    val owner : ContainerWithWrappedSeq = ContainerWithWrappedSeq(seq)
    val serialize: String = JsonMarshall.serialize(owner)
    println(serialize)
    val deserialize: ContainerWithWrappedSeq = JsonMarshall.deserialize[ContainerWithWrappedSeq](serialize)
    println(deserialize)
    deserialize should be(owner)
  }

}
于 2013-08-12T15:41:18.113 回答