Gson Online Tutorials

Gson, or Google Gson, is an open-source Java library that serializes Java objects to JSON (and deserializes them back to Java).

Google Gson
Developer(s)Google
Initial releaseMay 22, 2008; 16 years ago (2008-05-22)
Stable release
2.11.0 Edit this on Wikidata / 20 May 2024; 6 months ago (20 May 2024)
Repository
  • github.com/google/gson Edit this at Wikidata
Written inJava
Operating systemCross-platform
LicenseApache License 2.0
Websitegithub.com/google/gson

History

edit

The Gson library was originally developed for internal purposes at Google, with Version 1.0 released on May 22, 2008, under the terms of the Apache License 2.0. The latest version, 2.11, was released on May 20, 2024.

Usage

edit

Gson utilizes reflection, meaning that classes do not have to be modified to be serialized or deserialized. By default, a class only needs a defined default (no-args) constructor; however, this requirement can be circumvented (see Features).

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

package example;

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 example;

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 car : cars) {
            i++;
            sb.append("Car ").append(i).append(": ").append(car).append("\n");
        }
        return sb.toString();
    }
}
package main;

import example.Car;
import example.Person;
import com.google.gson.Gson;
import com.google.gson.GsonBuilder;

public class Main {
    public static void main(String[] args) {
        // Enable pretty printing for demonstration purposes
        // Can also directly create instance with `new Gson()`; this will produce compact JSON
        Gson gson = new GsonBuilder().setPrettyPrinting().create();
        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));
    }
}

Calling the code of the above Main class will result in the following JSON output:

{
  "name":
		 	 
Gson Tutorials: Gson is Google\'s open-source library for serializing and deserializing Java objects to/from JSON.

Latest online Gson Tutorials with example so this page for both freshers and experienced candidate who want to get job in Gson company

Latest online Gson Tutorials for both freshers and experienced

advertisements

View Tutorials on Gson View all questions

Ask your interview questions on Gson

Write Your comment or Questions if you want the answers on Gson from Gson Experts
Name* :
Email Id* :
Mob no* :
Question
Or
Comment* :
 





Disclimer: PCDS.CO.IN not responsible for any content, information, data or any feature of website. If you are using this website then its your own responsibility to understand the content of the website

--------- Tutorials ---