I'm trying to unmarshall input JSON into JAXB objects with Eclipselink. However, when I try to do this, I find that nested objects end up being set as null. I can try and unmarshall the nested object by itself, and it will work up until the point that it has to unmarshall a further nested object, which is also then set to null.
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;
}
}
}
Where ObjectA has an object called ObjectBs which contains a list of ObjectB. When I try unmarshalling this class, any other field that ObjectA has would be correctly filled, and the ObjectBs object would be created, but the list of ObjectB would be empty. But, if I unmarshall just one ObjectB by itself, it will be created with it's fields filled, up until it's own nested objects.
This is the code I'm using to unmarshall the 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 some example JSON:
{
"objectBs": [
{
"a": "blahblah",
"b": 123456,
"c": "abc blah 123 blah",
"nestedObject": {
"evenMoreNestedObjects": {
"d": "blah",
"e": "blahblahblah",
"anotherNestedObject": {
"x": 25,
"y": 50
}
}}}]
}