Take a class member like this one:
@ElementArray
private String[] names;
Simple XML will serialize this as:
<names length="3">
<string>Jonny Walker</string>
<string>Jack Daniels</string>
<string>Jim Beam</string>
</names>
Is there any way I can suppress the length attribute for the enclosing element?
There doesn’t seem to be a straightforward way to do it (see issue #39), but a hack will do the trick: Lists can be inlined (so only the elements for the items will be added, without the enclosing element and its unwanted attribute). You can then add a “pristine” enclosing element with a
@Pathannotation. Arrays cannot be inlined directly, but you can convert them to lists.List.@ElementList(inline=true)and@Path("names").@Defaultannotation, annotate the array as@Transientso it won’t get serialized twice.Like this:
Which will then yield:
You’ll have to add some extra magic for de-serialization, likely constructor injection. (Since in my case the array is
final, I will need that anyway.)If you require de-serialization, you will need to de-serialize the exact same elements you serialized. That is,
@ElementList(inline=true) @Path("names")will work whereas e.g.@ElementList(name="names")will throw a validation error.