In order to use Gson you have to include it in your project. You can do this by adding the following dependency of the Gson version available in Maven Central:
Maven
Add to pom.xml
<dependencies>
<dependency>
<groupId>com.google.code.gson</groupId>
<a...
Gson gson = new Gson(); //Create a Gson object
MyType target = new MyType(); //This is the object you want to convert to JSON
String json = gson.toJson(target); // serializes target to Json
MyType target2 = gson.fromJson(json, MyType.class); // deserializes json into target2
The Gson library provides Gson.class which handles all conversion between Java and JSON objects. An instance of this class can be created by invoking default constructor. You usually would like to have one Gson instance for the most part of operations in your program.
Gson gson = new Gson();
Fir...
GSON does not support inheritance our of the box. Let's say we have the following class hierarchy:
public class BaseClass {
int a;
public int getInt() {
return a;
}
}
public class DerivedClass1 extends BaseClass {
int b;
@Override
public int getIn...