Oops Null Pointer

Java programming related

Category Archives: XStream

XStream 1.3.1 to 1.4.3 ReflectionConverter changes

Recently while upgrading to Java 7 I had to upgrade XStream due to (I think) Oracle changing the name of the JVM or reflection providers. My converters that subclass the ReflectionConverter class started to fail. I had been omitting fields and then manually marshalling them in by overriding the marshall and marshallFields methods.

The 1.3.1 version of the RelectionConverter (in AbstractConverter) passed all non transient fields to the marshallField method. But in 1.4.3 it now checks if the fields should be omitted and will not pass omitted fields through to the marshallField method.

While mine was a relatively unusual case I hope this post can help others out stuck on similar issues.

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) {
        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()));