In modern Java, using JSON libraries like Jackson or Gson is the standard way to handle data exchange, as the Java Standard Library does not include a built-in JSON parser.
With Java 25, you can take advantage of Records for clean data models and Text Blocks for readable JSON strings in your code.
1. Using Jackson (Industry Standard)
Jackson is highly recommended for modern applications, especially within the Spring and Jakarta EE ecosystems.
Maven Dependency:
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-databind</artifactId>
<version>2.18.2</version>
</dependency>
Code Example:
import com.fasterxml.jackson.databind.ObjectMapper;
// 1. Define a Record (Modern Java way)
public record User(Long id, String name) {}
public class JacksonDemo {
public static void main(String[] args) throws Exception {
ObjectMapper mapper = new ObjectMapper();
// Serialization (Object to JSON)
User user = new User(1L, "Duke");
String json = mapper.writeValueAsString(user);
System.out.println("JSON: " + json);
// Deserialization (JSON to Object)
String inputJson = """
{
"id": 2,
"name": "Java"
}
""";
User decodedUser = mapper.readValue(inputJson, User.class);
System.out.println("User Name: " + decodedUser.name());
}
}
2. Using Gson (Google’s Library)
Gson is known for its simplicity and ease of use for smaller projects or quick utilities.
Maven Dependency:
<dependency>
<groupId>com.google.code.gson</groupId>
<artifactId>gson</artifactId>
<version>2.11.0</version>
</dependency>
Code Example:
import com.google.code.gson.Gson;
import com.google.code.gson.GsonBuilder;
public class GsonDemo {
public static void main(String[] args) {
// Use GsonBuilder for custom configurations like Pretty Printing
Gson gson = new GsonBuilder().setPrettyPrinting().create();
// Serialization
User user = new User(10L, "Modern Java");
String json = gson.toJson(user);
System.out.println(json);
// Deserialization
User fromJson = gson.fromJson(json, User.class);
System.out.println("ID from JSON: " + fromJson.id());
}
}
Key Comparisons & Tips
- Records Support: Both Jackson (2.12+) and Gson (2.10+) support Java Records natively. This is the preferred way to create POJOs for data mapping.
- Immutability: Since Records are immutable, these libraries use reflection or canonical constructors to populate the data, which is much safer than traditional setter-based beans.
- Performance: Jackson is generally faster and offers more features for complex mapping (like
@JsonPropertyfor renaming fields). - Integration: If you are using Spring Boot, Jackson is already included and configured for you.
Integrating with HttpClient
When fetching data from an API using the java.net.http.HttpClient, you typically receive a String which you then pass to these libraries:
HttpResponse<String> response = client.send(request, HttpResponse.BodyHandlers.ofString());
User user = mapper.readValue(response.body(), User.class);
For more advanced use cases, you can even write a custom BodyHandler that uses Jackson to parse the stream directly, saving memory on large JSON payloads.
