如何禁用XStream中的漂亮打印(空白/换行符)? [英] How to disable pretty-printing(white space/newline) in XStream?
本文介绍了如何禁用XStream中的漂亮打印(空白/换行符)?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
这是我为XML创建XStream实例的方法:
This is how I create XStream instance for XML:
XStream xstream = new XStream();
这是针对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.
如何让XStream禁用漂亮的打印?
How can I ask the XStream to disable the pretty printing?
推荐答案
在社区的帮助下,我找到了答案。
With some help from the community, I've figured out the answer.
对于XML ,您必须改变序列化的方式:
For XML you have to change the way how you serialize:
该行:
xStream.toXML(o, new OutputStreamWriter(stream, encoding));
更改为行
xStream.marshal(o, new CompactWriter(new OutputStreamWriter(stream, encoding)));
对于JSON ,您只能更改XStream的创建方式。因此,XStream的初始化更改为:
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);
}
});
请注意,使用了4参数JsonWriter构造函数。
Note that the 4-parameter JsonWriter constructor is used.
这篇关于如何禁用XStream中的漂亮打印(空白/换行符)?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文