7

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.

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

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.

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?

greetings Daniel

Tigraine
  • 23,358
  • 11
  • 65
  • 110

1 Answers1

11

As documentation says here: http://x-stream.github.io/annotations-tutorial.html (Omitting Fields) you can use @XStreamOmitField annotation to "ignore" fields.

facundofarias
  • 2,973
  • 28
  • 27
bealex
  • 10,004
  • 1
  • 21
  • 27
  • 2
    Thanks for this answer. As explained in this page: Annotations will be processed only if you call `xstream.processAnnotations()` or `xstream.autodetectAnnotations(true)`. – mins Oct 12 '14 at 10:11
  • 2
    Alternatively, `XStream.omitField`. – amos Jan 12 '16 at 21:31