java - Parse to a Subclass by default with Jackson -
i have class called product , subclasses extending it. in annotations have many types, this:
@jsontypeinfo(use=jsontypeinfo.id.name, include=jsontypeinfo.as.wrapper_object) @jsonsubtypes({@type(value=roomproduct.class, name="roomproduct"), @type(value=groundproduct.class, name="groundproduct"), })
and definition of product class. want if jackson cannot detect field not complying of these structures, return
unknownproduct
how can jackson @type annotation? should putting blank in name or flag in value don't know (i have tried creating unknownproduct extends product , putting nothing in name value no success.
@jsontypeinfo has option specify default implementation class after debugging found 'defaultimpl' broken wrapperobject. configuration:
@jsontypeinfo(use=jsontypeinfo.id.name, include= jsontypeinfo.as.wrapper_object, defaultimpl = unknownproduct.class)
jackson implementation (aswrappertypedeserializer):
public aswrappertypedeserializer(javatype bt, typeidresolver idres, string typepropertyname, boolean typeidvisible, class<?> defaultimpl) { super(bt, idres, typepropertyname, typeidvisible, null); }
note 'defaultimpl' passed ignored , configured default class not used. didn't find logged ticket problem in jackson's jira.
this problem wrapper_object, defaultimpl working fine other formats. change json format. if can change -- can use external_property example default implementation:
@jsontypeinfo(use=jsontypeinfo.id.name, include= jsontypeinfo.as.external_property, property = "type", defaultimpl = unknownproduct.class)
another solution: if have use wrapper_object can configure jackson not fail when find unknown subtype:
objectmapper.configure(deserializationfeature.fail_on_invalid_subtype, false);
it not same asking in case product null. can treat null unknown product.
update i've filed jackson bug: https://github.com/fasterxml/jackson-databind/issues/656
update ticket resolved 2.3 , 2.4 jackson , should able use when jars re-installed maven repository or in new version.
Comments
Post a Comment