On this page
  1. Jackson
  2. Overview
  3. Architecture (Three Layers)
  4. Modern Usage with Java record & java.time
  5. Tree Model (JsonNode) for Dynamic Payloads
  6. Security & Gotchas

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)

  1. jackson-core (Streaming API): Low-level, low-allocation JsonParser and JsonGenerator for huge payloads.
  2. jackson-annotations: Declarative metadata (@JsonProperty, @JsonIgnore, @JsonInclude, @JsonTypeInfo, @JsonSubTypes).
  3. jackson-databind: High-level ObjectMapper that binds JSON directly to Java records, POJOs, collections, or generic JsonNode trees.

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

Linked from

Related notes