Xstream:隐式忽略所有字段

发布于 2024-08-16 08:08:40 字数 854 浏览 5 评论 0原文

如何告诉 Xstream 仅序列化显式注释的字段并忽略其余字段?

我正在尝试序列化一个 hibernate 持久对象,并且所有与代理相关的字段都被序列化,而我不希望在 xml 中出现这些字段。
eg

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

不是我想要的 xml 内容。

编辑:我认为我没有说清楚这个问题。一个类可能继承自一个基类,而我无法控制该基类的属性(如在 hibernate 的情况下)。

public class A {
    private String ShouldNotBeSerialized;
}

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

在这种情况下,当我序列化类 B 时,基类字段 ShouldNotBeSerialized 也将被序列化。这不是我想要的。在大多数情况下,我无法控制 A 类。

因此,我想默认情况下省略所有字段,并仅序列化我显式指定注释的字段。我想避免 GaryF 正在做的事情,在我需要的地方明确指定我需要省略的字段。

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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

雅心素梦 2024-08-23 08:08:40

您可以使用 @XstreamOmitField 注释省略字段。直接来自手册:

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

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);
    }
}
杀お生予夺 2024-08-23 08:08:40

我不能相信这个答案,只是分享我所发现的。您可以重写XStream类的wrapMapper方法来实现您所需要的。

此链接详细说明: http://pvoss.wordpress.com/2009/01/ 08/xstream/

如果您不需要解释,这里是您需要的代码:

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

您可能希望在实现此代码之前完成所有测试,因为默认 XStream 对象抛出的异常对于查找拼写很有用错误。

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.

扭转时空 2024-08-23 08:08:40

已经有一张给 XStream 人员的票了:

同样,这是设计使然。 XStream是序列化工具,而不是数据
装订工具。它用于将 Java 对象序列化为 XML 并返回。它
会将重新创建平等所需的任何内容写入 XML
对象图。生成的 XML 可以通过以下方式进行一定程度的调整:
配置是为了方便,但这已经是一个附加组件。你什么
喜欢做的事情可以通过实现自定义映射器来完成,但这是一个
有关用户列表的问题,无法在此处处理。

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

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

娇俏 2024-08-23 08:08:40

我想唯一直接的方法是深入编写 MapperWrapper 并排除所有未注释的字段。听起来像是对 XStream 的功能请求。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文