java - JACKSON JSON deserialization issue with overrloaded setter -
from several reasons have create developers convenience able set 1 reference via overloaded setters ( due modelled oneof attribute).
i expect depending on json schema polymorphic (oneof) property have deserialized reference object of footype or bartype, ....
depending on json schema. hoping since footype , bartype follow bean convention can determined happens jacksonfeature in jaxrs ....
in dummy test seems not work described below :
objectmapper mapper = new objectmapper(); mapper.enable(serializationfeature.indent_output); mapper.setserializationinclusion(jsoninclude.include.non_null); mapper.configure(serializationfeature.write_dates_as_timestamps, false); mapper.configure(deserializationfeature.fail_on_unknown_properties, false); mapper.configure(deserializationfeature.read_enums_using_to_string, true); mapper.configure(serializationfeature.write_enums_using_to_string, true); mapper.readvalue(schema, simplepojo.class);
the issue mapper crashes, , json schema(sch1) can not deserialized pojo
the json schema (sch1)
{ "dummy" : { "bar" : "bar", "baz" : 10 }, "other" : { "foo" : "hi there" }, "simple" : "simple" }
the sub element types dummy, other :
public class bartype { private string bar; private number baz; public string getbar() { homecoming bar; } public void setbar(string bar) { this.bar = bar; } public number getbaz() { homecoming baz; } public void setbaz(number baz) { this.baz = baz; }
and
public class footype { private object foo; public object getfoo() { homecoming foo; } public void setfoo(object foo) { this.foo = foo; }
the top level pojo ( skipped part )
public class simplepojo { private string simplefield; private object dummyfield; private object otherfield; public string getsimple() { homecoming simplefield; } public void setsimple(string simple) { this.simplefield = simple; } ... public void setdummy(final footype dummy) { this.dummyfield = dummy; } public void setdummy(final bartype dummy) { this.dummyfield = dummy; } public void setdummy(final string dummy) { this.dummyfield = dummy; }
the issue can not deserialize correctly schema (sch1), instead receive :
com.fasterxml.jackson.databind.jsonmappingexception: conflicting setter definitions property "dummy": com.hybris.api.poc.simplepojo#setdummy(1 params) vs com.hybris.api.poc.simplepojo#setdummy(1 params)
i trying utilize @jsoncreator, , @jsondeserialize no luck seems can not have 2 (non primitive) override setters
@jsondeserialize( builder = footype.footypebuilder.class) @jsoncreator public void setdummy(final footype dummy) { this.dummyfield = dummy; } /** * type specific setter #dummy; * * @param dummy reference set #dummy */ @jsondeserialize( builder = bartype.bartypebuilder.class ) @jsoncreator public void setdummy(final bartype dummy) { this.dummyfield = dummy; }
can hint me should solution or breaking principal concept ?
i think breaking principal concept there. type of scenario, having base of operations abstract class jsontypeinfo , jsonsubtypes annotations describe sub-object preferred. if absolutely need ability set 3 types via setdummy(...), work you?
replace:
public void setdummy(final footype dummy) { this.dummyfield = dummy; } public void setdummy(final bartype dummy) { this.dummyfield = dummy; } public void setdummy(final string dummy) { this.dummyfield = dummy; }
with:
@jsondeserialize( using = dummydeserializer.class ) public void setdummy(final object dummy) { // if need restrict 3 types, throw exception here if (! (dummy instanceof footype || dummy instanceof bartype || dummy instanceof string) ) { throw new exception("cannot setdummy dummy!"); } this.dummyfield = dummy; }
this require deserialization manually 3 classes in dummybuilder, should solve multi-setters problem. i've not tried implement this, think works.
java json jackson jsonschema
No comments:
Post a Comment