tags:

views:

39

answers:

1

It is easy to set a converter for a certain type (http://xstream.codehaus.org/javadoc/com/thoughtworks/xstream/XStream.html gives an example):

xstream.registerConverter(new SqlTimestampConverter());
xstream.registerConverter(new DynamicProxyConverter());

I would like to register a converter, but on different element names. The Converter interface does not offer the name of the element.

For example: A date should be converted to a detailed string if the element name (property name) will be in <longDate> but short if in <shortDate>. How can I add different converters based on the property name? Something like xstream.registerConverter( Class classtype, propertyname, converter ) didn't exist but would be nice.

I know that I can use a specialised version of a PrettyPrintWriter but that seems to much work for this simple job. Any other ideas?

BTW: I could use an annotation @XStreamConverter(XXX.class) but I don't want to use annotations. I want my bean free of any annotation.

A: 

Use registerLocalConverter() instead of registerConverter().

xstream.registerLocalConverter( MyClazz.class, "property", new MyConverter() );
Christian Ullenboom