Skip to content

Instantly share code, notes, and snippets.

@ajduke
Last active August 14, 2018 17:12
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ajduke/6097579 to your computer and use it in GitHub Desktop.
Save ajduke/6097579 to your computer and use it in GitHub Desktop.
Getting started with GSON
repositories {
mavenCentral()
}
dependencies {
compile 'com.google.code.gson:gson:2.2.4'
}
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]
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);
}
}
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]
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);
}
}
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]
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);
}
}
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 + "]";
}
}
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 + "]";
}
}
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 + "]";
}
}
<dependency>
<groupId>com.google.code.gson</groupId>
<artifactId>gson</artifactId>
<version>2.2.4</version>
</dependency>
@nikhilr612
Copy link

Thanks for the introduction

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment