I use XStream to serialize and de-serialize an object. For example, a class named Rating is defined as follows:
Public Class Rating { String id; int score; int confidence;
However, in this class, the confidence variable is optional.
So, when the confidence value is known (and not 0), the XML representation of the Rating object should look like this:
<rating> <id>0123</id> <score>5</score> <confidence>10</confidence> </rating>
However, when trust is unknown (the default value is 0), the trust attribute must be omitted from the XML view:
<rating> <id>0123</id> <score>5</score> </rating>
Can someone tell me how to conditionally serialize a field using XStream?
source share