Open In App

Difference Between GSON and JSON in Android

Last Updated : 13 Sep, 2022
Improve
Improve
Like Article
Like
Save
Share
Report

JSON stands for JavaScript Object Notation. It is a text format for storing and transporting data. JSON is “self-describing” and easy to understand. It is a lightweight data-interchange format. JSON is used to send data between computers as it is language-independent. Code for reading and generating JSON exists in many programming languages. The JSON format was originally specified by Douglas Crockford.

On the other hand, GSON is a Java library that can be used to convert Java Objects into their JSON representation. It can also be used to convert a JSON string to an equivalent Java object. GSON can work with arbitrary Java objects including pre-existing objects that you do not have source code of. It is an open-source library developed by Google.

Note: GSON is not an officially supported Google product.

Uses of JSON and GSON

Why do we use JSON?

The JSON format is syntactically similar to the code for creating JavaScript objects. Because of this, a JavaScript program can easily convert JSON data into JavaScript objects. Since the format is text only, JSON data can easily be sent between computers and used by any programming language. 

  • It is used while writing JavaScript-based applications that include browser extensions and websites.
  • The JSON format is used for serializing and transmitting structured data over a network connection.
  • It is primarily used to transmit data between a server and web applications.
  • Web services and APIs use JSON format to provide public data.
  • It can be used with modern programming languages.

JSON is a data format that is human-readable and supported by a wide variety of languages.

An example of JSON encoded data:

{
"name":"Amit",
"age":25,
"interests":["singing", "reading", "playing cricket"],
"favorites":{
             "color":"red",
             "cricketer":"Virat, Dhoni, Rohit"
            }
}

Individual examples of XML and JSON :

JSON

{
 "company": GeeksforGeeks,
 "established": "2007",
 "About": Best platform to learn DSA
}

XML

<GFG>
    <company>GeeksforGeeks</company>
    <established>2007</established>
    <About> Best platform to learn DSA</About>
</GFG>

Checkout JSON Parsing in Android to understand a few important functions related to JSON in Android.

Why do we use GSON?

  • It provides simple toJson() and fromJson() methods to convert Java objects to JSON and vice-versa
  • It allows pre-existing unmodifiable objects to be converted to and from JSON
  • It also provides extensive support for Java Generics
  • It allows custom representations for objects
  • GSON supports arbitrarily complex objects (with deep inheritance hierarchies and extensive use of generic types)
  • GSON is a standardized library that is managed by Google
  • It is a reliable, fast, and efficient extension of the Java standard library.
  • This library is highly optimized

How to Download GSON in an Android Application

Gradle:

dependencies {
    implementation 'com.google.code.gson:gson:2.9.1'
}

Maven:

<dependency>
    <groupId>com.google.code.gson</groupId>
    <artifactId>gson</artifactId>
    <version>2.9.1</version>
</dependency>

Full GSON Hello World Example

You can copy and paste this example into your IDE as long as you have GSON installed and running

Java




import com.google.gson.Gson;
  
public class GsonHelloWorld {
   public static void main(String[] args) {
       // init class
       Place place = new Place();
       place.setName("World");
  
       Human human = new Human();
       human.setMessage("Hi");
       human.setPlace(place);
  
       // convert to json
       Gson gson = new Gson();
       String jsonString = gson.toJson(human);
       // print "json {"message":"Hi","place":{"name":"World"}}"
       System.out.println("json " + jsonString);
  
       // convert from json
       Human newHuman = gson.fromJson(jsonString, Human.class);
       newHuman.say(); // print "Hi , World!"
   }
  
   private static class Human {
       private String message;
       private Place place;
  
       public String getMessage() {
           return message;
       }
  
       public void setMessage(String message) {
           this.message = message;
       }
  
       public Place getPlace() {
           return place;
       }
  
       public void setPlace(Place place) {
           this.place = place;
       }
  
       public void say() {
           System.out.println();
           System.out.println(getMessage() + " , " + getPlace().getName() + "!");
       }
   }
  
   private static class Place {
       private String name;
  
       public String getName() {
           return name;
       }
  
       public void setName(String name) {
           this.name = name;
       }
   }
}




Like Article
Suggest improvement
Share your thoughts in the comments

Similar Reads