Simplify and optimize prettyPrint and stringify methods #1280
+74
−83
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Pull Request Checklist
Purpose
This PR simplifies the code of
JacksonJson(JVM) that is used on the path of theJson.stringify,Json.asciiStringify,Json.prettyPrintandJson.prettyPrintToStreammethods.The simplification consists of:
JsonGeneratorat each call (mapper.getFactory.createGenerator)prettyPrintonly set theDefaultPrettyPrinteronce rather than doing it once on theObjectWriterand once on theJsonGenerator(to be fair, I don't know if both were useful or one would have been enough anyways)StringWriter: Jackson handles it itself.On top of code simplification, this also brings performance improvements.
A minimal JMH benchmark (not included in the PR but I could if you think it's worth it) gives the following results on my laptop:
While this is a very basic benchmark on a single case (a single
JsValue), I think it's safe to assume that the code change improves performance.Benchmark code
Background Context
I noticed this when working on the upgrade to Jackson 3 (#1268) and I think it's worth a separate PR before any upgrade to Jackson 3.
Additional notes
The 1st commit may seem unrelated, I can open a dedicated PR for it if you prefer (or squash it with the 2nd). In practice it's partially related because at some point (again when working on the Jackson 3 upgrade), I was able to get tests working when comparing strings but the underlying object (Jackson
JsonNode) was not of the right type.Having 2 mappers (one without and one with the
ESCAPE_NON_ASCIIfeature) goes in the direction of Jackson 3 where these objects are immutable