Files
jackson3-by-example/src/main/java/com/ankurm/jackson3/part0setup/A02SharedMapperConfiguration.java
T
asmhatre c438afc33b Jackson 3 series companion code
37 runnable examples covering the eight feature posts on ankurm.com, verified
against Jackson 3.2.1 on Temurin 21.0.5. Every output committed under docs/ was
produced by run-all.sh.

Also documents 11 places where the published snippets do not compile or do not
behave as printed against a real Jackson 3 build - most notably that
writeValueAsString(List<Base>) silently drops the polymorphic type discriminator,
so the post's serialised output cannot be read back.
2026-08-04 23:29:26 +05:30

65 lines
3.0 KiB
Java

package com.ankurm.jackson3.part0setup;
import com.fasterxml.jackson.annotation.JsonInclude;
import tools.jackson.databind.DeserializationFeature;
import tools.jackson.databind.json.JsonMapper;
import java.time.LocalDate;
/**
* Post: Jackson 101 — https://ankurm.com/jackson-java-tutorial/
* Section: "The ObjectMapper Lifecycle — The One Rule That Matters Most"
*
* Build the mapper ONCE at startup and share it. In Jackson 3 this is enforced
* by the API rather than by convention: the built mapper has no setters at all.
*/
public class A02SharedMapperConfiguration {
public record Invoice(Long invoiceId, String customerName, LocalDate issuedOn, String note) { }
/**
* The Jackson 3 equivalent of the classic Spring @Bean ObjectMapper.
*
* Three of the four settings people habitually copy from Jackson 2 tutorials
* are unnecessary or wrong in Jackson 3 — the comments say which and why.
*/
static JsonMapper buildSharedMapper() {
return JsonMapper.builder()
// NOT NEEDED: FAIL_ON_UNKNOWN_PROPERTIES already defaults to false in
// Jackson 3. Listed here only because it is the single most-copied line
// from Jackson 2 configuration; deleting it changes nothing.
.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false)
// NOT NEEDED: java.time support is built into jackson-databind 3.x and
// dates already serialise as ISO-8601 strings. There is no
// SerializationFeature.WRITE_DATES_AS_TIMESTAMPS in Jackson 3 — the flag
// moved to tools.jackson.databind.cfg.DateTimeFeature and is off by
// default. See beyond/Y03DateTimeDefaults for proof.
// Skip null fields. Jackson 2's setSerializationInclusion(...) and the
// builder's serializationInclusion(...) do NOT exist in Jackson 3.
// The real API is changeDefaultPropertyInclusion.
.changeDefaultPropertyInclusion(
incl -> incl.withValueInclusion(JsonInclude.Include.NON_NULL))
.build();
}
public static void main(String[] args) {
JsonMapper mapper = buildSharedMapper();
Invoice invoice = new Invoice(500L, "Alice", LocalDate.of(2026, 4, 9), null);
System.out.println("configured : " + mapper.writeValueAsString(invoice));
JsonMapper plain = JsonMapper.builder().build();
System.out.println("defaults : " + plain.writeValueAsString(invoice));
// Proof that the mapper is immutable: ObjectMapper in Jackson 3 exposes no
// set*() mutators at all, so there is no way to reconfigure a shared instance.
long setterCount = java.util.Arrays.stream(
tools.jackson.databind.ObjectMapper.class.getMethods())
.filter(m -> m.getName().startsWith("set"))
.count();
System.out.println("ObjectMapper set*() methods in Jackson 3: " + setterCount);
}
}