Search code examples
javaxmlserializationxml-serializationxstream

using Xstream is possible serialize methods?


I have a Java object like this:

class A{
  public int id;
  public double hours;
  public int getMinutes(){
    return hours * 60;
  }
}

I would like serialize the class A using the xstream.toXml method in this way;

A a = new A();
a.id = 10;
a.hours = 2.5;
XStream xstream = new XStream(new DomDriver());
xstream.autodetectAnnotations(true);
xstream.toXML(a, writer);

to output this this result:

<A>
<id>10</id>
<minutes>150</minutes>
</A>

Is it possible? There is a special XStream annotation that can be used? Should I use some workaround?


Solution

  • The only solution I found is create my own Converter that implements:

    com.thoughtworks.xstream.converters.Converter