Gson

Google Gson
Developer(s) Google
Initial release May 22, 2008 (2008-05-22)
Stable release
2.8.1 / 30 May 2017 (2017-05-30)
Written in Java
Operating system Cross-platform
License Apache License 2.0
Website github.com/google/gson

Gson (also known as Google Gson) is an open source Java library to serialize and deserialize Java objects to (and from) JSON.

History

The Gson library was originally developed for internal purposes of Google, and Version 1.0 was later released on May 22, 2008 under the terms of Apache License 2.0. The latest version, 2.8, was released on October 27, 2016.

Version history

Usage

Gson uses reflection so it does not require additional modifications to classes of (de)serialized objects. In fact it just needs the class to have defined default no-args constructor (not entirely true, see Features).

The following example demonstrates the most basic usage of Gson when serializing a sample object:

public class Car {
    private String manufacturer;
    private String model;
    private Double capacity;
    private boolean accident;

    private 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);
    }
}

public class Person {
    private String name;
    private String surname;
    private Car[] cars;
    private int phone;
    private transient int age;

    private 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: " + name + " " + surname + "\n");
        sb.append("Phone: " + phone + "\n");
        sb.append("Age: " + age + "\n");

        int i = 0;
        for (Car item : cars) {
            i++;
            sb.append("Car " + i + ": " + item + "\n");
        }

        return sb.toString();
    }
}

After calling

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", 245987453, 35, cars);
System.out.println(gson.toJson(johnDoe));

you will get this output:

{
    "name": "John",
    "surname": "Doe",
    "cars": [
        {
            "manufacturer": "Audi",
            "model": "A4",
            "capacity": 1.8,
            "accident": false
        },
        {
            "manufacturer": "Škoda",
            "model": "Octavia",
            "capacity": 2,
            "accident": true
        }
    ],
    "phone": 245987453
}

Since the Person's field "age" is marked as transient, it is not included in the output.

To deserialize output produced by last example, you can execute the following code:

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\":245987453}";
Person johnDoe = gson.fromJson(json, Person.class);
System.out.println(johnDoe.toString());

And the following output will be generated:

Name: John Doe
Phone: 245987453
Age: 0
Car 1: Manufacturer: Audi, Model: A4, Capacity: 1.8, Accident: false
Car 2: Manufacturer: Škoda, Model: Octavia, Capacity: 2.0, Accident: true

Features

  • Compact/pretty printing (whether you want compact or readable output)
  • How to handle null object fields - by default they are not present in the output
  • Rules of what fields are intended to be excluded from (de)serialization
  • How to convert Java field names
This article is issued from Wikipedia. The text is licensed under Creative Commons - Attribution - Sharealike. Additional terms may apply for the media files.