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:
Edgardo Cusi
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
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.