On this page
Jackson
Prev: Google Guava | Up: third_party/java | Next: Apache Maven
Rel: record, java.time, java.net.http, Spring Framework
Ref: Jackson Project Home | Jackson Databind Docs
Tags: #public #prog #java #third-party #json #serialization #tier/core
Overview
Jackson (com.fasterxml.jackson.core:jackson-databind) is the standard JSON (and YAML, CBOR, Smile, XML) serialization and deserialization library in the Java ecosystem. Because the JDK does not ship a built-in JSON parser, Jackson fills that gap across almost all Java services and frameworks.
Architecture (Three Layers)
jackson-core(Streaming API): Low-level, low-allocationJsonParserandJsonGeneratorfor huge payloads.jackson-annotations: Declarative metadata (@JsonProperty,@JsonIgnore,@JsonInclude,@JsonTypeInfo,@JsonSubTypes).jackson-databind: High-levelObjectMapperthat binds JSON directly to Javarecords, POJOs, collections, or genericJsonNodetrees.
Modern Usage with Java record & java.time
Register JavaTimeModule (com.fasterxml.jackson.datatype:jackson-datatype-jsr310) so Instant, LocalDate, and Duration serialize as ISO-8601 strings rather than numeric arrays. Reuse a single configured ObjectMapper instance (it is thread-safe once configured):
import com.fasterxml.jackson.annotation.JsonProperty;
import com.fasterxml.jackson.core.type.TypeReference;
import com.fasterxml.jackson.databind.DeserializationFeature;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.SerializationFeature;
import com.fasterxml.jackson.datatype.jsr310.JavaTimeModule;
import java.time.Instant;
import java.util.List;
record Event(
String id,
@JsonProperty("created_at") Instant createdAt,
List<String> tags
) {}
ObjectMapper mapper = new ObjectMapper()
.registerModule(new JavaTimeModule())
.disable(SerializationFeature.WRITE_DATES_AS_TIMESTAMPS)
.disable(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES);
// Serialize
String json = mapper.writeValueAsString(new Event("evt-1", Instant.now(), List.of("java")));
// Deserialize single record
Event evt = mapper.readValue(json, Event.class);
// Deserialize generic collection (TypeReference preserves generic type info past erasure)
List<Event> list = mapper.readValue("[...]", new TypeReference<List<Event>>() {});
Tree Model (JsonNode) for Dynamic Payloads
When the schema is unknown or partially structured, parse into a JsonNode:
var root = mapper.readTree(json);
String id = root.path("id").asText(); // safe: returns "" if missing
Security & Gotchas
- Never enable unrestricted
enableDefaultTyping()on untrusted JSON—it enables polymorphic deserialization gadgets. Use explicit@JsonTypeInfo+@JsonSubTypes(or sealed hierarchies) with a closed allowlist of subtypes instead. - Avoid creating a new
ObjectMapperper request; constructing one is expensive and discards internal serializer caches. - Jackson 3 (
tools.jackson): Uses immutableJsonMapper.builder()...build()and shipsjava.timesupport built-in, while Jackson 2 (com.fasterxml.jackson) remains the dominant baseline across existing libraries.
Linked from
Related notes
- tuts/third_party/java/guava Linked here; links to this page; shared tags: #java, #prog, #tier
- tuts/third_party/java/maven Linked here; links to this page; shared tags: #java, #prog, #tier
- tuts/third_party/java/spring_framework Linked here; links to this page; shared tags: #java, #prog, #tier
- tuts/third_party/java Linked here; links to this page; shared tags: #java, #prog
- tuts/java/builtins/record Linked here; shared tags: #java, #prog, #tier
- tuts/third_party/java/spring_boot links to this page; shared tags: #java, #prog, #tier
- tuts/java links to this page; shared tags: #java, #prog
- tuts/java/stdlib/java_net_http Linked here; shared tags: #java, #prog
- tuts/java/stdlib/java_time Linked here; shared tags: #java, #prog
- tuts/interop/java_and_c_ffm_and_jni shared tags: #java, #prog, #tier