What is Gson and why is it needed?
Gson is a powerful library for the Java language, developed by Google, that allows you to convert Java objects into their JSON representation (serialization) and, conversely, restore Java objects from JSON strings (deserialization). JSON (JavaScript Object Notation) is a lightweight data interchange format widely used in web APIs, configuration files, and for storing structured data.
Why use Gson? In modern Java application development, almost no project can do without working with JSON. Whether it's communicating with a REST API, reading configurations, or exchanging data between microservices, Gson provides a simple, fast, and reliable way to convert. Unlike the standard Java library (org.json), Gson can work directly with arbitrary Java objects (POJOs), automatically mapping class fields to JSON keys. This saves the developer from manual parsing and writing boilerplate code.
Installing Gson
Gson is a Java library, so it is distributed through dependency management systems for the JVM. Below are installation methods for the most popular build tools.
Maven
Add the following dependency to your pom.xml file:
<dependency> <groupId>com.google.code.gson</groupId> <artifactId>gson</artifactId> <version>2.10.1</version> <!-- Check the latest version on Maven Central --></dependency>Gradle
Add to the build.gradle file:
implementation 'com.google.code.gson:gson:2.10.1'Direct JAR Download
If you are not using build systems, download the JAR file from Maven Central and add it to your project's classpath.
Important: Unlike Python (pip) or Node.js (npm), Java does not have a single package manager. Maven and Gradle are the de facto standard.
Quick Start: First Example
Let's create a simple Person class, serialize it to JSON, and then deserialize it back.
import com.google.gson.Gson;
public class QuickStart { public static void main(String[] args) { // 1. Create an object Person person = new Person("Ivan", 30, "ivan@example.com");
// 2. Create a Gson instance Gson gson = new Gson();
// 3. Serialization (Java -> JSON) String json = gson.toJson(person); System.out.println("JSON: " + json); // Output: JSON: {"name":"Ivan","age":30,"email":"ivan@example.com"}
// 4. Deserialization (JSON -> Java) String jsonInput = "{\\"name\\":\\"Maria\\",\\"age\\":25,\\"email\\":\\"maria@test.com\\"}"; Person personFromJson = gson.fromJson(jsonInput, Person.class); System.out.println("Name: " + personFromJson.getName()); // Output: Name: Maria }}
class Person { private String name; private int age; private String email;
// A default (no-argument) constructor is required for deserialization! public Person() {}
public Person(String name, int age, String email) { this.name = name; this.age = age; this.email = email; }
public String getName() { return name; } public int getAge() { return age; } public String getEmail() { return email; }}Main Methods and Functions of Gson
Below are the key methods of the Gson library. Each method is accompanied by a description and a working example.
1. Gson.toJson(Object src)
What it does: Serializes the given Java object into a JSON string. This is the most basic method for converting an object to JSON.