Hello,
Today I ran into a bizarre issue where my application indented its JSON
improperly or not at all, sometimes always wrong, and sometimes only
partially wrong, and sometimes changing what kind of wrong from REST call
to REST call.
After researching the problem, I noticed that PrettyPrinter and Indenter
are not thread-safe, but do not mention this in their API documentation, as
far as I could read during my investigation (using Jackson 2.9.5).
I am not 100% sure I have fixed this properly in my code, because it's
extremely complicated. It appears that ObjectMapper is thread-safe, meaning
you can re-use a static ObjectMapper in multiple parts of the code,
however, it would seem that calling mapper.setDefaultPrettyPrinter(...)
would potentially mean that it's not-thread-safe anymore based on the issue
I was seeing
I had to make a factory method of sorts to generate a pretty-printer:
public static DefaultPrettyPrinter getPrinter() {
DefaultPrettyPrinter.Indenter indenter =
new DefaultIndenter(" ", DefaultIndenter.SYS_LF);
DefaultPrettyPrinter printer = new DefaultPrettyPrinter();
printer.indentObjectsWith(indenter);
printer.indentArraysWith(indenter);
return printer;
}
Then I needed this in my JsonFactory:
public static class CustomJsonFactory extends JsonFactory {
private static final long serialVersionUID = -3521368899846283407L;
@Override
protected JsonGenerator _createGenerator(Writer out, IOContext
ctxt) throws IOException {
DefaultPrettyPrinter printer = getPrinter();
return super._createGenerator(out,
ctxt).setPrettyPrinter(printer);
}
@Override
protected JsonGenerator _createUTF8Generator(OutputStream out,
IOContext ctxt) throws IOException {
DefaultPrettyPrinter printer = getPrinter();
return super._createUTF8Generator(out,
ctxt).setPrettyPrinter(printer);
}
}
However, I am not sure if this fix will help on the static copies of
ObjectMapper I set up in the code, for doing stuff like internally
converting YAML to Objects, Objects to JSON strings for log messages, etc.
If anybody has some more experience how this should work, I'd love
recommendations, and I would be happy to help write some kind of
documentation patch if I can understand how this should be done properly to
avoid breakage.
Matthew.
--
You received this message because you are subscribed to the Google Groups
"jackson-user" group.
To unsubscribe from this group and stop receiving emails from it, send an email
to [email protected].
To post to this group, send email to [email protected].
For more options, visit https://groups.google.com/d/optout.