使XStream忽略一个特定的私有变量 [英] Make XStream ignore one specific private variable
问题描述
我正在为我正在编写保存函数的类有一点问题。
I have a little problem with a class I am currently writing a save function for.
我正在使用XStream(com.thoughtworks.xstream)来序列化一个使用DOMDriver将类转换为XML。
I'm using XStream (com.thoughtworks.xstream) to serialize a class to XML using the DOMDriver.
该类看起来像这样:
public class World {
private Configuration config;
public World(Configuration config) {
this.config = config;
}
}
所以,这里的问题是我不想要在序列化世界时序列化配置,而不是在调用fromXml()时给XStream一个预构建的配置实例。
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().
这里的问题主要是类设计,配置有一个对GUI类的私有引用因此序列化配置意味着使用GUI等完全序列化整个应用程序。这有点不好。
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.
有没有办法指示XStream不序列化私有字段配置,并在加载XStream时使用配置实例?
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?
问候Daniel
推荐答案
正如文档所述: http:// x-stream.github.io/annotations-tutorial.html (省略字段)您可以使用@XStreamOmitField注释来忽略字段。
As documentation says here: http://x-stream.github.io/annotations-tutorial.html (Omitting Fields) you can use @XStreamOmitField annotation to "ignore" fields.
这篇关于使XStream忽略一个特定的私有变量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!