Hello All,
Attempting to use XStream's JavaBeanConverter and running into an issue. Most likely I'm missng something simple, or not understanding XStream's converter handling well enough.
@XStreamAlias("test")
public class TestObject
{
private String foo;
public String getFoo()
{
//return foo; -- Adjusted for EDIT#2
return foo.toLowerCase();
}
public void setFoo(String foo)
{
this.foo = foo;
}
}
public void test() throws Exception
{
XStream x = new XStream(new XppDriver());
x.autodetectAnnotations(true);
x.processAnnotations(TestObject.class);
x.registerConverter(new JavaBeanConverter(x.getMapper()));
TestObject o = new TestObject();
//o.setFoo("bar"); -- Adjusted for EDIT#2
o.setFoo("BAR");
String xml = x.toXML(o);
System.out.println(xml);
/*
Expecting...
<test>
<foo>bar</foo>
</test>
But instead getting...
<test>
<foo/>
</test>
*/
}
I tried adding a trace on the TestObject.getFoo()
method and it appears it is being called by XStream, but the data isn't being written to the output stream.
After looking at the source for JavaBeanConverter, it looks like my implementation should work, which leads me to believe I haven't configured something correctly during the XStream setup.
Am I just missing something simple?
Thanks!
Edit 1
Also, if it helps, I'm using the following Maven deps for this...
<dependency>
<groupId>org.apache.servicemix.bundles</groupId>
<artifactId>org.apache.servicemix.bundles.xstream</artifactId>
<version>1.3_3</version>
</dependency>
<dependency>
<groupId>org.apache.servicemix.bundles</groupId>
<artifactId>org.apache.servicemix.bundles.xpp3</artifactId>
<version>1.1.4c_3</version>
</dependency>
Edit 2
I altered the TestObject.getFoo()
method to better explain my objective.
I'm trying to use the getters/setters to do "sanitation" of the internal object's data. Specifically, I'm trying to clean up incoming deserialized data, while trying to avoid having to implement Serializable's readResolve() method.
If I implemented readResolve's method I would have to do the cleanup in both the setter method (for any non-deserialized incoming data set elsewhere) and also in the readResolve (for XML data I deserialized).
That's the reason why I'm using JavaBeanConverter instead of XStream's normal field based practices... to force data through the getters/setters.
Hopefully this explains a little better.
Thanks.