Note that there are some explanatory texts on larger screens.

plurals
  1. POFailing Polymorphic de-serialization in Jackson with Scala module when polymorphic types are within a Seq()
    primarykey
    data
    text
    <p>Given this simple example the third test "de-serialize" fails with the message.</p> <pre><code>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] </code></pre> <p>Even though the second test "de-serialize_a" proves that Jackson can resolve the correct polymorphic types.</p> <p>I am using Jackson version and scala module </p> <pre><code> &lt;dependency&gt; &lt;groupId&gt;com.fasterxml.jackson.module&lt;/groupId&gt; &lt;artifactId&gt;jackson-module-scala_2.10&lt;/artifactId&gt; &lt;version&gt;2.2.2&lt;/version&gt; &lt;/dependency&gt; &lt;dependency&gt; &lt;groupId&gt;com.fasterxml.jackson.datatype&lt;/groupId&gt; &lt;artifactId&gt;jackson-datatype-joda&lt;/artifactId&gt; &lt;version&gt;2.2.2&lt;/version&gt; &lt;/dependency&gt; </code></pre> <p>Code:</p> <pre><code>@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) } } </code></pre>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload