Question

This is how I create XStream instance for XML:

XStream xstream = new XStream();

This is for JSON:

private final XStream xstream = new XStream(new JsonHierarchicalStreamDriver() {
        public HierarchicalStreamWriter createWriter(Writer writer) {
            return new JsonWriter(writer, JsonWriter.DROP_ROOT_MODE);
        }
    });

Both of them are pretty-printing (indenting) the output.

How can I ask the XStream to disable the pretty printing?

Was it helpful?

Solution 2

With some help from the community, I've figured out the answer.

For XML you have to change the way how you serialize:

The line:

xStream.toXML(o, new OutputStreamWriter(stream, encoding));

changed to line

xStream.marshal(o, new CompactWriter(new OutputStreamWriter(stream, encoding)));

For JSON you only change the way how the XStream is created. So the initialization of the XStream is changed to:

private final XStream xstreamOut = new XStream(new JsonHierarchicalStreamDriver() {
    public HierarchicalStreamWriter createWriter(Writer writer) {
        return new JsonWriter(writer, new char[0], "", JsonWriter.DROP_ROOT_MODE);
    }
});

Note that the 4-parameter JsonWriter constructor is used.

OTHER TIPS

Thanks, your posts helped!!! Here is what I use to convert to a String.

String strXML = "";
XStream xs = new XStream();
StringWriter sw = new StringWriter();
xs.marshal(this,  new CompactWriter(sw));
strXML = sw.toString();

Use the marschal method on xstream with a compact writer

xstream.marshal(someObject, new CompactWriter(out)); 

The default behaviour of pretty-printing comes from the AbstractXmlDriver.createWriter() method (XStream uses XppDriver as its default hierarchical stream driver and this extends AbstractXmlDriver):

public HierarchicalStreamWriter createWriter(Writer out) {
    return new PrettyPrintWriter(out, getNameCoder());
}

If you want to disable pretty-printing globally (whilst retaining all other default behaviours) and just use the simple toXML(o) method rather than messing about with the other per use options suggested here, you can initialise your XStream instance as follows. This overrides the above method with a CompactWriter instead.

XStream xstream = new XStream(new XppDriver() {
    @Override
    public HierarchicalStreamWriter createWriter(Writer out) {
        return new CompactWriter(out, getNameCoder());
    }
});

Create your XStream instance in this way:

XStream xstream = new XStream(new JsonHierarchicalStreamDriver() {
        public HierarchicalStreamWriter createWriter(Writer writer) {
            return new JsonWriter(writer, JsonWriter.DROP_ROOT_MODE, new JsonWriter.Format(new char[0], new char[0], 0));
        }
});

Here is a constructor of Format class:

/**
 * Create a new Formatter.
 * 
 * @param lineIndenter the characters used for indenting the line
 * @param newLine the characters used to create a new line
 * @param mode the flags for the format modes
 * @since 1.4
 */
public Format(char[] lineIndenter, char[] newLine, int mode) {
    this(lineIndenter, newLine, mode, new NoNameCoder());
}

Check a source code of JsonWriter for more information

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top