Unmarshalling nested objects from JSON with JAXB

I am trying to disable JSON input in JAXB objects using Eclipselink. However, when I try to do this, I find that nested objects are eventually set to null. I can try and unmarshall a nested object on its own, and it will work until it has to cancel the marker of another nested object, which will also be set to zero.

For example, take this 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; } } } 

If ObjectA has an ObjectBs that contains a list of ObjectBs. When I try to decouple this class, any other field that ObjectA will be correctly populated and ObjectBs will be created, but the ObjectB list will be empty. But, if I unmount only one ObjectB by itself, it will be created with filled fields, up to its own nested objects.

This is the code that I use to unmount 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 in some JSON example:

  { "objectBs": [ { "a": "blahblah", "b": 123456, "c": "abc blah 123 blah", "nestedObject": { "evenMoreNestedObjects": { "d": "blah", "e": "blahblahblah", "anotherNestedObject": { "x": 25, "y": 50 } }}}] } 
+4
source share
1 answer

You did not provide the JSON that you are trying to untie, but I did a little reverse development, and below is an example that works using the model posted in your 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" : [ { }, { } ] } } 
+5
source

Source: https://habr.com/ru/post/1493507/


All Articles