I'm doing serialization with XStream. For XML I use the StaxDriver and for JSON I use JettisonMappedXmlDriver:
if (this.format == ISerializer.Format.JSON){
logger.info("json");
/* note: JsonHierarchicalStreamDriver can read Json only */
this.xstream = new XStream(new JettisonMappedXmlDriver());
}
else if (this.format == ISerializer.Format.XML){
logger.info("xml");
this.xstream = new XStream(new StaxDriver());
}
With XML I get pretty print, with JSON, I never get pretty print:
public boolean toStream(Object object, Writer writer){
if(this.usePrettyPrint == true){
this.xstream.marshal(object, new PrettyPrintWriter(writer));
}else{
this.xstream.toXML(object, writer);
}
return true;
}
And if I leave my code this way, I will get XML instead of JSON, I had to rewrite my code this way to get JSON, but not pretty-printed:
public boolean toStream(Object object, Writer writer){
if (this.format == ISerializer.Format.JSON){
this.xstream.toXML(object, writer);
}
else{
if(this.usePrettyPrint == true){
this.xstream.marshal(object, new PrettyPrintWriter(writer));
}else{
this.xstream.toXML(object, writer);
}
}
return true;
}
Do you know a way to get pretty printing in JSON with the JettisonMappedXmlDriver?
In the XStream documentation there is no information about it, they even seem to find it OK:
http://x-stream.github.io/json-tutorial.html
but I cannot believe that there is no way to get pretty printed JSON using XStream if you want to be able to serialize and deserialize (JettisonMappedXmlDriver)...
Thanks!