public
Last active

Getting started with GSON

  • Download Gist
Example1-Output
1 2 3 4 5 6
toJson ---
Original Java object : ModelObject [name=myname, val=12, status=true, f=2.3]
Converted JSON string is : {"name":"myname","val":12,"status":true,"f":2.3}
fromJson----
Original JSON string is : {"name":"myname","val":12,"status":true,"f":2.3}
Converted Java object : ModelObject [name=myname, val=12, status=true, f=2.3]
Example1.java
Java
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30
package in.ajduke.ap012;
 
import com.google.gson.Gson;
 
/**
* An example of Gson demo
*
* @author ajduke
*
*/
public class GsonExample {
public static void main(String[] args) {
 
final Gson gson = new Gson();
// original object instantiation
ModelObject modelObject = new ModelObject("myname", 12, true, 2.3);
System.out.println("toJson ---");
System.out.println("Original Java object : " + modelObject);
// converting an object to json object
String json = gson.toJson(modelObject);
System.out.println("Converted JSON string is : " + json);
 
System.out.println("fromJson----");
// getting object from json representation
System.out.println("Original JSON string is : " + json);
// converting json to object
ModelObject modelObject1 = gson.fromJson(json, ModelObject.class);
System.out.println("Converted Java object : " + modelObject1);
}
}
Example2-Output
1 2 3 4 5 6 7 8
A generic object demo
json representation :{"value":12}
converted object representation: Model2 [value=12]
 
A object from collection framework
 
json representation :["ajduke","ajduchess"]
converted object representation: [ajduke, ajduchess]
Example2.java
Java
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51
package in.ajduke.ap012;
 
import java.lang.reflect.Type;
import java.util.ArrayList;
import java.util.List;
 
import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
 
/**
* An example of Gson demo
*
* @author ajduke
*
*/
public class GsonExample {
 
public static void main(String[] args) {
Gson gson = new Gson();
 
System.out.println("A generic object demo");
// a generified object
GenericModel<Integer> model = new GenericModel<>(12);
 
// converting to json representation
String json = gson.toJson(model);
System.out.println("json representation :" + json);
 
// converting back to object
Type collectionType = new TypeToken<GenericModel<Integer>>() {
}.getType();
GenericModel<Integer> modelObj = gson.fromJson(json, collectionType);
System.out.println("converted object representation: " + modelObj);
 
System.out.println("\nA object from collection framework\n");
// for collection framework objects
List<String> listOfString = new ArrayList<>();
listOfString.add("ajduke");
listOfString.add("ajduchess");
 
// conversion to json
String jsonStr = gson.toJson(listOfString);
System.out.println("json representation :" + jsonStr);
 
Type collectionType2 = new TypeToken<List<String>>() {
}.getType();
List<String> listObj = gson.fromJson(jsonStr, collectionType2);
System.out.println("converted object representation: " + listObj);
 
}
}
Example3-Output
1 2 3
Original Java object : ModelObject [name=namesake, val=50, status=true, f=4.3]
Converted JSON string is : {"name":"namesake","status":true,"f":4.3}
Converted Java object : ModelObject [name=namesake, val=0, status=true, f=4.3]
Example3.java
Java
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30
package in.ajduke.ap012;
 
import com.google.gson.Gson;
 
/**
* An example of Gson demo
*
* @author ajduke
*
*/
public class GsonExample {
 
public static void main(String[] args) {
Gson gson = new Gson();
// original object
ModelObject modelObject = new ModelObject("namesake", 50, true, 4.3);
System.out.print("Original Java object : ");
System.out.println(modelObject);
 
// converting to an json representation
String json = gson.toJson(modelObject);
System.out.print("Converted JSON string is : ");
System.out.println(json);
 
// getting back the object from json representation
ModelObject modelObject3 = gson.fromJson(json, ModelObject.class);
System.out.print("Converted Java object : ");
System.out.println(modelObject3);
}
}
GenericModel.java
Java
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
package in.ajduke.ap012;
/**
* An generified model for demo of gson conversion
* @author ajduke
*/
public class GenericModel<T> {
T value;
 
public GenericModel(T value) {
super();
this.value = value;
}
 
@Override
public String toString() {
return "Model2 [value=" + value + "]";
}
 
}
ModelObject.java
Java
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30
package in.ajduke.ap012;
/**
* An model for demo of gson conversion
*
* @author ajduke
*
*/
public class ModelObject {
String name;
int val;
boolean status;
double f;
 
public ModelObject(String name, int val,
boolean status, double f) {
super();
this.name = name;
this.val = val;
this.status = status;
this.f = f;
}
 
@Override
public String toString() {
return "ModelObject [name=" + name + ",
val=" + val + ", status="
+ status + ", f=" + f + "]";
}
 
}
ModelObject2.java
Java
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30
package in.ajduke.ap012;
/**
* An model for demo of gson conversion
*
* @author ajduke
*
*/
public class ModelObject {
String name;
transient int val;
boolean status;
double f;
public ModelObject(String name, int val,
boolean status, double f) {
super();
this.name = name;
this.val = val;
this.status = status;
this.f = f;
}
@Override
public String toString() {
return "ModelObject [name=" + name + ",
val=" + val + ", status="
+ status + ", f=" + f + "]";
}
}
build.gradle
Groovy
1 2 3 4 5 6 7
repositories {
mavenCentral()
}
 
dependencies {
compile 'com.google.code.gson:gson:2.2.4'
}
pom.xml
XML
1 2 3 4 5
<dependency>
<groupId>com.google.code.gson</groupId>
<artifactId>gson</artifactId>
<version>2.2.4</version>
</dependency>

Please sign in to comment on this gist.

Something went wrong with that request. Please try again.