개발자 | 구글 |
---|---|
발표일 | 2008년 5월 22일 |
안정화 버전 | 2.8.5
/ 2018년 5월 21일 |
저장소 | |
프로그래밍 언어 | 자바 |
운영 체제 | 크로스 플랫폼 |
라이선스 | 아파치 라이선스 2.0 |
웹사이트 | github |
Gson(구글 Gson, Google Gson)은 JSON의 자바 오브젝트의 직렬화, 역직렬화를 해주는 오픈 소스 자바 라이브러리이다.
Gson 라이브러리는 원래 구글의 내부 목적을 위해 개발되었으며 버전 1.0이 아파치 라이선스 2.0의 조항 하에 2008년 5월 22일 출시되었다. 최신 버전 2.8.5는 2018년 5월 21일에 출시되었다.
Gson은 반영을 사용하므로 (역)직렬화된 오브젝트의 클래스에 대한 추가적인 수정이 필요하지 않다. 사실 클래스가 no-args 기본 생성자만 있으면 된다. (무조건 그런 것은 아님. 기능 문단 참고)
다음의 예는 샘플 오브젝트를 직렬화할 때 가장 기초적인 Gson 사용법을 표현한 것이다:
module GsonExample {
requires gson;
requires java.sql; // Required by gson
exports Person;
exports Car;
}
package Car;
public class Car {
public String manufacturer;
public String model;
public double capacity;
public boolean accident;
public Car() {
}
public Car(String manufacturer, String model, Double capacity, boolean accident) {
this.manufacturer = manufacturer;
this.model = model;
this.capacity = capacity;
this.accident = accident;
}
@Override
public String toString() {
return ("Manufacturer: " + manufacturer + ", " + "Model: " + model + ", " + "Capacity: " + capacity + ", " + "Accident: " + accident);
}
}
package Person;
import Car.Car;
public class Person {
public String name;
public String surname;
public Car[] cars;
public int phone;
public transient int age;
public Person() {
}
public Person(String name, String surname, int phone, int age, Car[] cars) {
this.name = name;
this.surname = surname;
this.cars = cars;
this.phone = phone;
this.age = age;
}
@Override
public String toString() {
StringBuilder sb = new StringBuilder();
sb.append("Name: ").append(name).append(" ").append(surname).append("\n");
sb.append("Phone: ").append(phone).append("\n");
sb.append("Age: ").append(age).append("\n");
int i = 0;
for (Car item : cars) {
i++;
sb.append("Car ").append(i).append(": ").append(item).append("\n");
}
return sb.toString();
}
}
호출 후
package Main;
import Car.Car;
import Person.Person;
import com.google.gson.Gson;
public class Main {
public static void main(String[] args) {
Gson gson = new Gson();
Car audi = new Car("Audi", "A4", 1.8, false);
Car skoda = new Car("Škoda", "Octavia", 2.0, true);
Car[] cars = {audi, skoda};
Person johnDoe = new Person("John", "Doe", 2025550191, 35, cars);
System.out.println(gson.toJson(johnDoe));
}
}
다음의 출력을 받게 된다:
{
"name":"John",
"surname":"Doe",
"cars":[
{
"manufacturer":"Audi",
"model":"A4",
"capacity":1.8,
"accident":false
},
{
"manufacturer":"Škoda",
"model":"Octavia",
"capacity":2.0,
"accident":true
}
],
"phone":2025550191
}
Person 필드 "age"가 transient로 표시되므로 출력에 포함되지 않는다.
마지막 예제에서 만든 출력을 역직렬화하려면 다음의 코드를 실행할 수 있다:
package Main;
import Person.Person;
import com.google.gson.Gson;
public class Main {
public static void main(String[] args) {
Gson gson = new Gson();
String json = "{\"name\":\"John\",\"surname\":\"Doe\",\"cars\":[{\"manufacturer\":\"Audi\",\"model\":\"A4\"," +
"\"capacity\":1.8,\"accident\":false},{\"manufacturer\":\"Škoda\",\"model\":\"Octavia\",\"capacity\"" +
":2.0,\"accident\":true}],\"phone\":2025550191}";
Person johnDoe = gson.fromJson(json, Person.class);
System.out.println(johnDoe.toString());
}
}
다음의 출력이 만들어진다:
Name: John Doe
Phone: 2025550191
Age: 0
Car 1: Manufacturer: Audi, Model: A4, Capacity: 1.8, Accident: false
Car 2: Manufacturer: Škoda, Model: Octavia, Capacity: 2.0, Accident: true