HowToDoInJava

  • Python
  • Java
  • Spring Boot
  • Dark Mode
Home / Java / Java Libraries / GSON – Serialize and Deserialize JSON

GSON – Serialize and Deserialize JSON

Learn to use Google GSON library to serialize Java Objects into their JSON representation and to deserialize a JSON string to an equivalent Java object. GSON provides simple toJson() and fromJson() methods to convert Java objects to JSON and vice-versa.

Use GsonBuilder to create Gson object with custom configurations such as pretty printing.

//Gson gson = new Gson();
Gson gson = new GsonBuilder().setPrettyPrinting().create();
		
Employee emp = new Employee(1001, "Lokesh", "Gupta", "howtodoinjava@gmail.com");

String jsonString = gson.toJson(emp);

Employee empObject = gson.fromJson(jsonString, Employee.class);

1. Dependency

Maven dependency. Visit maven repository for latest version.

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

Gradle dependency.

<dependency>
dependencies {
  implementation 'com.google.code.gson:gson:2.8.5'
}

2. Serialization – Write JSON using Gson

Serialization in the context of Gson means converting a Java object to its JSON representation. In order to do the serialization, we need a Gson object, which handles the conversion. Next, we need to call the function toJson() and pass the Employee object.

Employee emp = new Employee(1001, "Lokesh", "Gupta", "howtodoinjava@gmail.com");
		
Gson gson = new Gson();

String jsonString = gson.toJson(emp);

System.out.println(jsonString);

Program output.

{
   "id":1001,
   "firstName":"Lokesh",
   "lastName":"Gupta",
   "email":"howtodoinjava@gmail.com"
}

2. Deserialization – Read JSON using Gson

Deserialization in the context of Gson means converting a JSON string to equivalent Java object. In order to do the deserialization, we need a Gson object and call the function fromJson() and pass two parameters i.e. JSON string and expected java type after parsing is finished.

String jsonString = "{'id':1001, 'firstName':'Lokesh', 'lastName':'Gupta', 'email':'howtodoinjava@gmail.com'}";
		
Gson gson = new Gson();

Employee empObject = gson.fromJson(jsonString, Employee.class);

System.out.println(empObject);

Program output.

Employee [id=1001, firstName=Lokesh, lastName=Gupta, email=howtodoinjava@gmail.com]

Drop me your question related to Gson object and it’s toJson() and fromJson() methods.

Happy Learning !!

References:

GSON Github

Was this post helpful?

Let us know if you liked the post. That’s the only way we can improve.
TwitterFacebookLinkedInRedditPocket

About Lokesh Gupta

A family guy with fun loving nature. Love computers, programming and solving everyday problems. Find me on Facebook and Twitter.

Feedback, Discussion and Comments

  1. Edgardo Cusi

    April 10, 2020

    In your example above, it seems @ sign in email address is not issue. But the problem I am trying to identify in deserializing Email address field, the ‘@‘ sign is dropped with the rest of the value intact. From the field annotation, it has ‘TEXT’ encoding and field type is alphanumeric / special characters.
    I googled several topics about json and gson but did not find anything close that would point me to the issue.
    Hope I explained my issue well.

    Thank you.
    Edgardo

    • Edgardo Cusi

      April 12, 2020

      Please ignore this. I have found the issue. I am new to Gson/Java so took time for me to understand the process. There is a replaceAll() command applied to all field types. Not the best logic obviously.
      Thank you.
      Sorry too—can not figure out how to edit the message.

Comments are closed on this article!

Search Tutorials

Gson Tutorial

  • Gson – Introduction
  • Gson – Installation
  • GSON – (De)serialize JSON
  • Gson – Pretty Printing
  • GSON – Array
  • GSON – Set
  • Gson – Map
  • Gson – GsonBuilder
  • Gson – NULL values
  • Gson – Version Support
  • Gson – @SerializedName
  • Gson – Ignore fields
  • Gson – JsonReader
  • Gson – JsonParser
  • Gson – Custom (De)serialization
  • Gson – Quick Guide

Meta Links

  • About Me
  • Contact Us
  • Privacy policy
  • Advertise
  • Guest and Sponsored Posts

Recommended Reading

  • 10 Life Lessons
  • Secure Hash Algorithms
  • How Web Servers work?
  • How Java I/O Works Internally?
  • Best Way to Learn Java
  • Java Best Practices Guide
  • Microservices Tutorial
  • REST API Tutorial
  • How to Start New Blog

Copyright © 2020 · HowToDoInjava.com · All Rights Reserved. | Sitemap

  • Sealed Classes and Interfaces