Jackson

Language: Java

Web

Jackson was created to provide a flexible, efficient, and feature-rich library for working with JSON in Java. It supports streaming, tree model, and data-binding approaches, making it popular for REST APIs, configuration parsing, and data serialization.

Jackson is a high-performance JSON processor for Java. It allows for parsing, generating, and transforming JSON data, and integrates seamlessly with Java objects using annotations and data binding.

Installation

maven: Add com.fasterxml.jackson.core:jackson-databind dependency in pom.xml
gradle: Add implementation 'com.fasterxml.jackson.core:jackson-databind:2.15.2' in build.gradle

Usage

Jackson allows Java developers to serialize Java objects to JSON and deserialize JSON into Java objects. It supports annotations for customizing serialization, ignores unknown properties, and integrates with frameworks like Spring Boot.

Serializing a Java object to JSON

import com.fasterxml.jackson.databind.ObjectMapper;

public class Main {
    public static void main(String[] args) throws Exception {
        ObjectMapper mapper = new ObjectMapper();
        User user = new User(1, "Alice");
        String json = mapper.writeValueAsString(user);
        System.out.println(json);
    }
}

class User {
    public int id;
    public String name;
    public User(int id, String name) { this.id = id; this.name = name; }
}

Converts a Java object `User` into a JSON string using `ObjectMapper`.

Deserializing JSON to a Java object

String json = "{\"id\":1,\"name\":\"Alice\"}";
User user = mapper.readValue(json, User.class);
System.out.println(user.name);

Converts a JSON string into a Java `User` object using Jackson.

Ignoring unknown properties

import com.fasterxml.jackson.annotation.JsonIgnoreProperties;

@JsonIgnoreProperties(ignoreUnknown = true)
class User {...}

Prevents Jackson from failing when extra fields exist in JSON that are not in the Java class.

Custom serialization

import com.fasterxml.jackson.core.JsonGenerator;
import com.fasterxml.jackson.databind.SerializerProvider;
import com.fasterxml.jackson.databind.ser.std.StdSerializer;

class CustomUserSerializer extends StdSerializer<User> {
    public void serialize(User user, JsonGenerator gen, SerializerProvider provider) throws IOException {
        gen.writeStartObject();
        gen.writeStringField("full_name", user.name);
        gen.writeEndObject();
    }
}

Demonstrates customizing how a Java object is serialized to JSON.

Working with Collections

List<User> users = Arrays.asList(new User(1,"Alice"), new User(2,"Bob"));
String json = mapper.writeValueAsString(users);
List<User> deserialized = mapper.readValue(json, new TypeReference<List<User>>() {});

Shows serialization and deserialization of lists of Java objects.

Tree model for dynamic JSON

JsonNode rootNode = mapper.readTree(json);
String name = rootNode.get("name").asText();

Allows parsing JSON into a tree structure for dynamic access without mapping to a Java class.

Error Handling

JsonMappingException: Occurs when JSON cannot be mapped to Java object. Ensure field names and types match.
JsonParseException: Occurs when JSON is invalid or malformed. Validate JSON input before deserialization.
IOException: Occurs during I/O operations. Handle file/stream errors when reading or writing JSON.

Best Practices

Use `ObjectMapper` as a singleton to avoid performance overhead.

Leverage annotations like `@JsonProperty`, `@JsonIgnore`, and `@JsonInclude` for fine-grained control.

Handle unknown properties gracefully with `@JsonIgnoreProperties`.

Use TypeReference for deserializing generic types like lists and maps.

Integrate with Spring Boot’s `MappingJackson2HttpMessageConverter` for REST APIs.