When XStream exports a TreeSet to XML the output contains “no-comparator” and “comparator” nodes. For my clients the comparator node is not used and makes the XML less clear.
To remove these nodes from the output I added a customer converter:
public class IgnoreComparatorTreeSetConverter extends CollectionConverter {
public IgnoreComparatorTreeSetConverter(Mapper mapper) {
super(mapper);
}
@Override
public boolean canConvert(@SuppressWarnings("rawtypes") Class type) {
return type.equals(TreeSet.class);
}
@Override
public Object unmarshal(HierarchicalStreamReader reader, UnmarshallingContext context) {
TreeSet<Object> result = new TreeSet<Object>();
super.populateCollection(reader, context, result);
return result;
}
}
And to use it just register the converter:
XStream xstream = new XStream(new DomDriver());
xstream.setMode(XStream.NO_REFERENCES);
xstream.registerConverter(new IgnoreComparatorTreeSetConverter(xstream.getMapper()));