objectmapper-jsonmapper: Jackson 2.23.0 vs 3.3.0 side by side (ObjectMapper/JsonMapper guide rewrite)

Seven runnable programs P01-P07 plus five compile-failure files, JDK 25, no docs/ folder.
Output in objectmapper-jsonmapper/output/ is regenerated by scripts/run-all.sh.

Co-Authored-By: Claude Sonnet 5.5 <[email protected]>
Claude-Session: https://claude.ai/code/session_017ic4b75Ao6imWN5bj7FHVw
This commit is contained in:
2026-11-17 00:53:38 +05:30
co-authored by Claude Sonnet 5.5
parent 1d0cd8b36e
commit f5ed5d1605
26 changed files with 857 additions and 0 deletions
@@ -0,0 +1,63 @@
package com.ankurm.objectmapper;
import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.atomic.AtomicInteger;
import tools.jackson.databind.json.JsonMapper;
/** One shared mapper across threads, versus building a mapper for every call. Timings are indicative. */
public class P07SharedMapperThreads {
static final int THREADS = 8;
static final int PER_THREAD = 20_000;
public static void main(String[] args) throws Exception {
JsonMapper shared = JsonMapper.builder().build();
Article article = new Article(1L, "Jackson Deep Dive", List.of("java", "json"));
String json = shared.writeValueAsString(article);
// ---- correctness: many threads, one mapper ----
AtomicInteger wrong = new AtomicInteger();
runThreads(() -> {
for (int i = 0; i < PER_THREAD; i++) {
Article a = shared.readValue(json, Article.class);
if (!"Jackson Deep Dive".equals(a.getTitle()) || !shared.writeValueAsString(a).equals(json)) {
wrong.incrementAndGet();
}
}
});
System.out.println(THREADS + " threads x " + PER_THREAD + " round trips on ONE JsonMapper, wrong results: " + wrong.get());
System.out.println();
// ---- cost: shared mapper vs a new mapper per call (single thread, warm JVM) ----
int n = 2_000;
for (int round = 1; round <= 3; round++) { // three rounds so the JIT settles; read the last
long t0 = System.nanoTime();
for (int i = 0; i < n; i++) {
shared.readValue(json, Article.class);
}
long sharedMs = (System.nanoTime() - t0) / 1_000_000;
t0 = System.nanoTime();
for (int i = 0; i < n; i++) {
JsonMapper.builder().build().readValue(json, Article.class);
}
long perCallMs = (System.nanoTime() - t0) / 1_000_000;
System.out.println("round " + round + ": shared " + sharedMs + " ms, new mapper per call " + perCallMs
+ " ms (" + n + " reads each)");
}
}
private static void runThreads(ThrowingRunnable body) throws Exception {
List<Thread> threads = new ArrayList<>();
for (int t = 0; t < THREADS; t++) {
Thread th = Thread.ofPlatform().start(() -> {
try { body.run(); } catch (Exception e) { throw new RuntimeException(e); }
});
threads.add(th);
}
for (Thread th : threads) { th.join(); }
}
interface ThrowingRunnable { void run() throws Exception; }
}