How can I make Simple not to serialize empty ArrayList?



Good day, let's say I have class Person:



@Root(name="Person")
public class Person {

@Element(name="Name")
public String name = "MyName"

@ElementList(name="AddressList", entry="Address", required=false)
public ArrayList<Address> addressList = new ArrayList<>();

}


... and class Address:



public class Address {

@Element(name="Street")
public String street;

@Element(name="City")
public String city;
}


When addressList has no items Simple produces this XML:



<Person>
<Name>MyName</Name>
<AddressList/>
</Person>


Please, how can I make Simple omit tag AddressList if there are no items on the list? I want the XML look like this in such case:



<Person>
<Name>MyName</Name>
</Person>


Thank you in advance.


UPDATE: I forgot to mention that I don't want to set the addressList to null. When I create Person object I want the addressList to be initialized so that I can easily add addresses if needed and I don't need to check whehter it is null and instantiate it. The example above is very simple but when my object hierarchy is more complicated it is unconvenient to check and initialize all lists. So I hoped I could resolve it using some anotation or custom converter.


No comments:

Post a Comment