On Wed, Apr 25, 2018 at 4:16 PM, <
mhcom...@gmail.com> wrote:
> 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).
Have another look at `PrettyPrinter` Javadoc, this is expalined there, in second
paragraph (wrt `Instantiatable`).
So: to make sure `PrettyPrinter` works in thread-safe manner, it needs
to either:
1. Be stateless (like `MinimalPrettyPrinter`), immutable. OR
2. Implement `com.fasterxml.jackson.core.util.Instantiatable`,
implement `createInstance()` method
that creates stateful instance that is not shared across threads
`DefaultPrettyPrinter` does the latter. If you want to sub-class it,
however, you MUST override `createInstance()`
method; otherwise it will probably not work the way you want (since by
default it can not create instance of sub-class).
So use of `DefaultPrettyPrinter` should be fine without any extra work.
Assuming that all code paths in Jackson do use `createInstance`. So
one thing that could be useful is to see how
your code is creating generators. With that, it should be possible to
create actual test case with custom `PrettyPrinter`
that verifies that a copy must have been made (keep a flag or
something, set on "blueprint" instance, cleared for
actual instances created).
-+ Tatu +-