使XStream忽略一个特定的私有变量

时间:2022-04-15 22:27:29

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().

所以,这里的问题是我不希望在序列化世界时序列化配置,而是在调用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类的私有引用,因此序列化配置意味着使用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

1 个解决方案

#1


10  

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(省略字段),您可以使用@XStreamOmitField注释来“忽略”字段。

#1


10  

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(省略字段),您可以使用@XStreamOmitField注释来“忽略”字段。