This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Type collectionType = new TypeToken<{generic-object-with-type-information}>(){}.getType(); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class Developer { | |
private String name; | |
private String classz; | |
List<String> languagesKnown; | |
public Developer() { | |
name = "ajduke"; | |
languagesKnown = new ArrayList<>(); | |
languagesKnown.add("Java"); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Gson gson = new Gson(); | |
String json = gson.toJson(new Developer()); | |
System.out.println("********* Compact mode ***********"); | |
System.out.println(json); | |
GsonBuilder gsonBuilder = new GsonBuilder(); | |
Gson prettyGson = gsonBuilder.setPrettyPrinting().create(); | |
json = prettyGson.toJson(new Developer()); | |
System.out.println("\n ******* Pretty formatting *********"); | |
System.out.println(json); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package in.ajduke.ap013; | |
import java.io.IOException; | |
import java.util.ArrayList; | |
import java.util.List; | |
import com.google.gson.Gson; | |
import com.google.gson.GsonBuilder; | |
import com.google.gson.annotations.SerializedName; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
********* Compact mode *********** | |
{"name":"ajduke","languagesKnown":["Java","Scala","Ruby"]} | |
******* Pretty formatting ********* | |
{ | |
"name": "ajduke", | |
"languagesKnown": [ | |
"Java", | |
"Scala", | |
"Ruby" |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class Developer { | |
private String name; | |
private String classz; | |
List<String> languagesKnown; | |
public Developer() { | |
name = "ajduke"; | |
classz= "Developer"; | |
languagesKnown = new ArrayList<>(); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Gson gson = new Gson(); | |
System.out.println("Default behaviour "); | |
GsonBuilder gsonBuilder = new GsonBuilder(); | |
Gson prettyGson = gsonBuilder.setPrettyPrinting().create(); | |
String json = prettyGson.toJson(new Developer()); | |
System.out.println(json); | |
System.out.println("Including the nulls "); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Gson gson = new Gson(); | |
System.out.println("Default behaviour "); | |
GsonBuilder gsonBuilder = new GsonBuilder(); | |
Gson prettyGson = gsonBuilder.setPrettyPrinting().create(); | |
String json = prettyGson.toJson(new Developer()); | |
System.out.println(json); | |
System.out.println("Ignoring/excluding fields "); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Default behaviour | |
{ | |
"name": "ajduke", | |
"classz": "Developer", | |
"languagesKnown": [ | |
"Java", | |
"Scala", | |
"Ruby" | |
] | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Default behaviour | |
{ | |
"name": "ajduke", | |
"languagesKnown": [ | |
"Java", | |
"Scala", | |
"Ruby" | |
] | |
} | |
Including the nulls |