java 使 XStream 忽略一个特定的私有变量
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/2795138/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Make XStream ignore one specific private variable
提问by Tigraine
I have a little problem with a class I am currently writing a save function for.
我目前正在为其编写保存函数的类有一个小问题。
I'm using XStream (com.thoughtworks.xstream) to serialize a class to XML using the DOMDriver.
我正在使用 XStream (com.thoughtworks.xstream) 使用 DOMDriver 将类序列化为 XML。
The class looks like this:
这个类看起来像这样:
public class World {
private Configuration config;
public World(Configuration config) {
this.config = config;
}
}
So, the issue here is that I do not want to serialize Configuration when serializing world, rather I'd like to give XStream a preconstructed Configuration instance when calling fromXml().
所以,这里的问题是我不想在序列化世界时序列化 Configuration,而是我想在调用 fromXml() 时给 XStream 一个预先构造的 Configuration 实例。
Problem here is mainly class design, Configuration holds a private reference to the GUI classes and therefore serializing Configuration means serializing the whole application completely with GUI etc.. And that's kind of bad.
这里的问题主要是类设计,Configuration 持有对 GUI 类的私有引用,因此序列化 Configuration 意味着使用 GUI 等完全序列化整个应用程序。这有点糟糕。
Is there a way to instruct XStream to not serialize the private field config, and upon load supply XStream with a configuration instance to use?
有没有办法指示 XStream 不序列化私有字段配置,并在加载时为 XStream 提供要使用的配置实例?
greetings Daniel
问候丹尼尔
回答by bealex
As documentation says here: http://x-stream.github.io/annotations-tutorial.html(Omitting Fields) you can use @XStreamOmitField annotation to "ignore" fields.
正如这里的文档所说:http: //x-stream.github.io/annotations-tutorial.html (Omitting Fields) 您可以使用 @XStreamOmitField 注释来“忽略”字段。

