JSON обработка в Java: библиотеки Jackson и Gson
JSON (JavaScript Object Notation) is a lightweight data interchange format widely used in web development, APIs, and microservices. In Java, there are several powerful libraries for working with JSON, among which the most popular are Jackson and Gson. In this article, we will take a detailed look at both libraries, their capabilities, usage examples, and features.
1. Jackson Library
Jackson is a high-performance library for JSON processing in Java, developed by FasterXML. It provides a complete set of tools for serialization, deserialization, parsing, and data transformation. Jackson is the de facto standard in the Spring ecosystem.
Key Features of Jackson:
- ObjectMapper — the main class for converting Java objects to JSON and vice versa.
- Annotations — flexible configuration of serialization using annotations (
@JsonProperty,@JsonIgnore,@JsonFormat). - Stream API Support — reading and writing JSON streams.
- Custom Serialization — the ability to create custom serializers and deserializers.
Example of Using Jackson:
import com.fasterxml.jackson.databind.ObjectMapper;import com.fasterxml.jackson.databind.SerializationFeature;
public class JacksonExample { public static void main(String[] args) throws Exception { // Create an object mapper ObjectMapper mapper = new ObjectMapper(); mapper.enable(SerializationFeature.INDENT_OUTPUT);
// Example class Person person = new Person("Иван", 30, "ivan@example.com");
// Serialization to JSON String jsonString = mapper.writeValueAsString(person); System.out.println("JSON: " + jsonString);
// Deserialization from JSON String jsonInput = "{\\"name\\":\\"Мария\\",\\"age\\":25,\\"email\\":\\"maria@example.com\\"}"; Person deserializedPerson = mapper.readValue(jsonInput, Person.class); System.out.println("Объект: " + deserializedPerson.getName()); }}
class Person { private String name; private int age; private String email;
public Person() {} // mandatory constructor
public Person(String name, int age, String email) { this.name = name; this.age = age; this.email = email; }
// getters and setters public String getName() { return name; } public void setName(String name) { this.name = name; } public int getAge() { return age; } public void setAge(int age) { this.age = age; } public String getEmail() { return email; } public void setEmail(String email) { this.email = email; }}Adding Jackson (Maven):
<dependency> <groupId>com.fasterxml.jackson.core</groupId> <artifactId>jackson-databind</artifactId> <version>2.15.2</version></dependency>2. Gson Library
Gson is a library from Google for serializing and deserializing Java objects to JSON and vice versa. It stands out for its ease of use and lightweight nature. Gson is often chosen for small projects and mobile development on Android.
Key Features of Gson:
- GsonBuilder — configuration of serialization behavior.
- Annotations — support for
@SerializedNameto rename fields. - JSON Array Support — working with collections.
- Null Handling — option to include/exclude null values.
Example of Using Gson:
import com.google.gson.Gson;import com.google.gson.GsonB Also in library
Sending email in TypeScript: libraries and code examples
Processing and Parsing Markdown in PHP: Libraries and Examples
ClosedXML for C# (.NET): Complete Guide with Examples
Java Standard Library: A Complete Overview for Beginners and Pros
Working with Excel in C# (.NET): EPPlus and ClosedXML Libraries