Unmarshalling nested objects from JSON with JAXB -
i'm trying unmarshall input json jaxb objects eclipselink. however, when try this, find nested objects end being set null. can try , unmarshall nested object itself, , work until point has unmarshall further nested object, set null.
for example, take class:
@xmlaccessortype(xmlaccesstype.field) @xmltype(name = "event", proporder = { "objectbs" }) public class objecta implements serializable { private final static long serialversionuid = 56347348765454329l; @xmlelement(required = true) protected objecta.objectbs objectbs; public objecta.objectbs getobjectbs() { return objectbs; } public void setobjectbs(objecta.objectbs value) { this.objectbs = value; } public boolean issetobjectbs() { return (this.objectbs!= null); } @xmlaccessortype(xmlaccesstype.field) @xmltype(name = "", proporder = { "objectb" }) public static class objectbs implements serializable { private final static long serialversionuid = 56347348765454329l; @xmlelement(required = true) protected list<objectb> objectb; public list<objectb> getobjectb() { if (objectb == null) { objectb = new arraylist<objectb>(); } return this.objectb; } public boolean issetobjectb() { return ((this.objectb!= null)&&(!this.objectb.isempty())); } public void unsetobjectb() { this.objectb = null; } } }
where objecta has object called objectbs contains list of objectb. when try unmarshalling class, other field objecta has correctly filled, , objectbs object created, list of objectb empty. but, if unmarshall 1 objectb itself, created it's fields filled, until it's own nested objects.
this code i'm using unmarshall json:
jaxbcontext jc = jaxbcontextfactory.createcontext( "com.package1" + ":com.package2" + ":com.package3" , null); unmarshaller um = jc.createunmarshaller(); um.setproperty("eclipselink.media-type", "application/json"); um.setproperty(marshallerproperties.json_include_root, false); objecta obja = unmarshaller.unmarshal(new streamsource( new stringreader(json)), objecta.class).getvalue();
and example json:
{ "objectbs": [ { "a": "blahblah", "b": 123456, "c": "abc blah 123 blah", "nestedobject": { "evenmorenestedobjects": { "d": "blah", "e": "blahblahblah", "anothernestedobject": { "x": 25, "y": 50 } }}}] }
you didn't provide json trying unmarshal, i've done little reverse engineering , below example works using model posted in question:
import javax.xml.bind.*; import javax.xml.transform.stream.streamsource; import org.eclipse.persistence.jaxb.marshallerproperties; import org.eclipse.persistence.jaxb.unmarshallerproperties; public class demo { public static void main(string[] args) throws exception { jaxbcontext jc = jaxbcontext.newinstance(objecta.class); unmarshaller unmarshaller = jc.createunmarshaller(); unmarshaller.setproperty(unmarshallerproperties.media_type, "application/json"); unmarshaller.setproperty(unmarshallerproperties.json_include_root, false); streamsource json = new streamsource("src/forum17866155/input.json"); objecta objecta = unmarshaller.unmarshal(json, objecta.class).getvalue(); marshaller marshaller = jc.createmarshaller(); marshaller.setproperty(marshallerproperties.media_type, "application/json"); marshaller.setproperty(marshallerproperties.json_include_root, false); marshaller.setproperty(marshaller.jaxb_formatted_output, true); marshaller.marshal(objecta, system.out); } }
input.json/output
{ "objectbs" : { "objectb" : [ { }, { } ] } }
Comments
Post a Comment