2010-05-08 69 views
7

我有一个小问题,我正在写一个保存函数的类。使XStream忽略一个特定的私有变量

我正在使用XStream(com.thoughtworks.xstream)使用DOMDriver将一个类序列化为XML。

类看起来是这样的:

public class World { 
    private Configuration config; 
    public World(Configuration config) { 
    this.config = config; 
    } 
} 

所以,这里的问题是,我不希望序列化的世界时序列化配置,而我想打电话fromXml时候给XStream的预先构造配置实例()。

这里的问题主要是类的设计,Configuration拥有对GUI类的私有引用,因此序列化配置意味着将整个应用程序与GUI完全序列化等等。而且这样做很糟糕。

有没有办法指示XStream不要序列化私有字段配置,并且在负载供应XStream时使用配置实例?

问候丹尼尔

回答

10

由于文件说,在这里:http://x-stream.github.io/annotations-tutorial.html(忽略字段),可以使用@XStreamOmitField标注为“忽略”的字段。

+1

感谢您的回答。如本页所述:仅当您调用'xstream.processAnnotations()'或'xstream.autodetectAnnotations(true)'时才会处理注释。 – mins 2014-10-12 10:11:51

+2

或者,'XStream.omitField'。 – amos 2016-01-12 21:31:03