Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Xstream: Implicitly ignoring all fields

Tags:

java

xml

xstream

How do I tell Xstream to serialize only fields which are annotated explicitly and ignore the rest?

I am trying to serialize a hibernate persistent object and all proxy related fields get serialized which I don’t want in my xml.
e.g.

<createdBy class="com..domain.Users " reference="../../values/createdBy"/>

is not something I want in my xml.

Edit: I don’t think I made this question clear. A class may inherit from a base class on which I have no control (as in hibernate’s case) on the base class properties.

public class A {
    private String ShouldNotBeSerialized;
}

public class B extends A {
    @XStreamAlias("1")
    private String ThisShouldbeSerialized;
}

In this case when I serialize class B, the base class field ShouldNotBeSerialized will also get serialized. This is not something I want. In most circumstances I will not have control on class A.

Therefore I want to omit all fields by default and serialize only fields for which I explicitly specify the annotation. I want to avoid what GaryF is doing, where I need to explicitly specify the fields I need to omit.

like image 373
Quintin Par Avatar asked Dec 10 '25 04:12

Quintin Par


1 Answers

You can omit fields with the @XstreamOmitField annotation. Straight from the manual:

@XStreamAlias("message")
class RendezvousMessage {

    @XStreamOmitField
    private int messageType;

    @XStreamImplicit(itemFieldName="part")
    private List<String> content;

    @XStreamConverter(SingleValueCalendarConverter.class)
    private Calendar created = new GregorianCalendar();

    public RendezvousMessage(int messageType, String... content) {
        this.messageType = messageType;
        this.content = Arrays.asList(content);
    }
}
like image 86
GaryF Avatar answered Dec 12 '25 16:12

GaryF



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!