开发者

Xstream: Implicitly ignoring all fields

开发者 https://www.devze.com 2022-12-15 04:18 出处:网络
How do I tell Xst开发者_Go百科ream to serialize only fields which are annotated explicitly and ignore the rest?

How do I tell Xst开发者_Go百科ream 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.


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);
    }
}


I can take no credit for this answer, just sharing what I have found. You can override the wrapMapper method of the XStream class to achieve what you need.

This link explains in detail: http://pvoss.wordpress.com/2009/01/08/xstream/

Here is the code you need if you don't want the explanation:

    // Setup XStream object so that it ignores any undefined tags
    XStream xstream = new XStream() {
            @Override
            protected MapperWrapper wrapMapper(MapperWrapper next) {
                return new MapperWrapper(next) {
                    @Override
                    public boolean shouldSerializeMember(Class definedIn,
                            String fieldName) {
                        if (definedIn == Object.class) {
                            return false;
                        }
                        return super
                                .shouldSerializeMember(definedIn, fieldName);
                    }
                };
            }
        };

You might want to do all your testing before you implement this code because the exceptions thrown by the default XStream object are useful for finding spelling mistakes.


There was already a ticket for the XStream people:

Again, this is by design. XStream is a serialization tool, not a data binding tool. It is made to serialize Java objects to XML and back. It will write anything into XML that is necessary to recreate an equal object graph. The generated XML can be tweaked to some extend by configuration for convenience, but this is already an add-on. What you like to do can be done by implementing a custom mapper, but that's a question for the user's list and cannot be handled here.

http://jira.codehaus.org/browse/XSTR-569


I guess the only direct way is to dive into writing a MapperWrapper and exclude all fields you have not annotated. Sounds like a feature request for XStream.

0

精彩评论

暂无评论...
验证码 换一张
取 消