Oops Null Pointer

Java programming related

XStream Removing no-comparator from XML output

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) {
    public boolean canConvert(@SuppressWarnings("rawtypes") Class type) {
        return type.equals(TreeSet.class);

    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.registerConverter(new IgnoreComparatorTreeSetConverter(xstream.getMapper()));

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

%d bloggers like this: