private static PrettyPrinter buildDefaultPrettyPrinter() {
DefaultPrettyPrinter pp =
new DefaultPrettyPrinter("") {
@Override
public DefaultPrettyPrinter withSeparators(Separators separators) {
this._separators = separators;
this._objectFieldValueSeparatorWithSpaces =
separators.getObjectFieldValueSeparator() + " ";
return this;
}
};
Indenter lfOnlyIndenter = new DefaultIndenter(" ", "\n");
pp.indentArraysWith(lfOnlyIndenter);
pp.indentObjectsWith(lfOnlyIndenter);
return pp;
}
@Override
public void init(Map<String, String> options, ConfigurationSource cfg) {
super.initCfg(cfg);
int indent = Integer.parseInt(options.getOrDefault("indent", "4"));
String lineEnding = options.getOrDefault("lineending", System.lineSeparator());
boolean spaceBeforeSeparator = Boolean.parseBoolean(options.getOrDefault("spaceBeforeSeparator", "true"));
formatter = new ObjectMapper();
// Setup a pretty printer with an indenter (indenter has 4 spaces in this case)
DefaultPrettyPrinter.Indenter indenter = new DefaultIndenter(Strings.repeat(" ", indent), lineEnding);
DefaultPrettyPrinter printer = new DefaultPrettyPrinter() {
private static final long serialVersionUID = 1L;
@Override
public DefaultPrettyPrinter createInstance() {
return new DefaultPrettyPrinter(this);
}
@Override
public DefaultPrettyPrinter withSeparators(Separators separators) {
this._separators = separators;
this._objectFieldValueSeparatorWithSpaces = (spaceBeforeSeparator ? " " : "")
+ separators.getObjectFieldValueSeparator() + " ";
return this;
}
};
printer.indentObjectsWith(indenter);
printer.indentArraysWith(indenter);
formatter.setDefaultPrettyPrinter(printer);
formatter.enable(SerializationFeature.INDENT_OUTPUT);
}