So I'm using BeanIO and I have to write a type handler, which will assigned null to list if the generic list is empty. For example I'm importing addresses from xml file.
If there is full information about address, xml looks like this:
<Address>
<Street1>aaaaaa</Street1>
<PostCode>00-000</PostCode>
<City>bbbbb</City>
<CountryCode>AA</CountryCode>
<PhoneNo>+00 00 00000000</PhoneNo>
<Email>aaa@aaa.aaa</Email>
</Address>
If there is no informations of address in xml file, it looks like this:
<Address />
Mapping in java class looks like this - with BeanIO @segment annotation:
@Segment(xmlName = "Address", minOccurs = 0, maxOccurs = -1)
private List<Address> address;
Basically, the test class expects that if above list is empty, the type handler should assign null to it. Without any type handler for the list, the empty brackets are assigned:
address=[]
And here is my question, does anyone know how the handler should looks for Collections types, in this case List? Previously I wrote some handlers for strings, ints and so on - things with @Field annotation and everything work well. Now I'm struggling to write for the things with @Segment annotations. Any hints? From which class to inherit from?
Since version 2.1 you can use lazy="true"
on collections. From the migrating to 2.1 documentation
Prior to 2.1, repeating segments designated lazy="true" were unmarshalled as an empty collection. Going forward, a collection will no longer be created if designated lazy and all items are null or the empty String.
Instead of trying to write a TypeHandler
, I would first try this:
@Segment(xmlName = "Address", minOccurs = 0, maxOccurs = -1, lazy="true")
private List<Address> address;
I think the "empty brackets" as you called it, is just the way a toString()
method was implemented to show that the list is empty.