How to disable pretty-printing(white space/newline) in XStream?
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();
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.
Use the marschal method on xstream with a compact writer
xstream.marshal(someObject, new CompactWriter(out));