java - Jackson Polymorphic Deserialization expected START_ARRAY -



java - Jackson Polymorphic Deserialization expected START_ARRAY -

i need deserializa json this:

{ "arrayobj1":[ { "type":"t1", "value":[ { "value1":"a" }, { "value2":"b" } ], "otherinfo":"abc" } ] }

thats how seek it:

public class classa{ private arrayobj1[] arrayobj1;}

then...

public class arrayobj1 extends value{ private string type; private value [] value; @jsontypeinfo(use = jsontypeinfo.id.name, include = jsontypeinfo.as.external_property, property = "type") @jsonsubtypes(value={ @jsonsubtypes.type(value = t1.class, name = "t1") }) public void setvalue (value [] value){ this.value = value; } }

and...

public abstract class value {}

this error iv got: com.fasterxml.jackson.databind.jsonmappingexception: unexpected token (start_object), expected start_array: need json array contain as.wrapper_array type info class ... etc

how handle this? thanks.

edit: how deserialize:

public classa getobj (string jsonstring) { classa obj = null; seek { obj = new objectmapper().readvalue(jsonstring, classa.class); } grab (ioexception e) { e.printstacktrace(); } homecoming obj; }

full error is:

com.fasterxml.jackson.databind.jsonmappingexception: unexpected token (start_object), expected start_array: need json array contain as.wrapper_array type info class .value @ [source: java.io.stringreader@4317b868; line: 1, column: 496] (through reference chain: .classa["arrayobj1"]->.arrayobj1["value"])

given construction use, need 1 more level of classes contain external type id. this:

public class classa { private valuewrapper[] arrayobj1; } public class valuewrapper { @jsontypeinfo(use = jsontypeinfo.id.name, include = jsontypeinfo.as.external_property, property = "type") @jsonsubtypes(value={ @jsonsubtypes.type(value = t1.class, name = "t1") } public value value; public string otherinfo; // or utilize getters/setters in addition; left fields brevity } public abstract class value { ... }

)

however, looking @ json more closely, work if values pojos. if assumption type elements of value array, there no automated way that. unfortunately need handle polymorphic types manually.

java json jackson polymorphism

Comments

Popular posts from this blog

php - Edges appear in image after resizing -

ios8 - iOS custom keyboard - preserve state between appearances -

Delphi change the assembly code of a running process -