Last active
December 20, 2015 23:19
-
-
Save ajduke/6211620 to your computer and use it in GitHub Desktop.
An article complete gist
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; | |
public class GsonEx1 { | |
public static void main(String[] args) throws IOException { | |
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); | |
} | |
} | |
class Developer { | |
private String classz; | |
private String name; | |
List<String> languagesKnown; | |
public Developer() { | |
name = "ajduke"; | |
languagesKnown = new ArrayList<>(); | |
languagesKnown.add("Java"); | |
languagesKnown.add("Scala"); | |
languagesKnown.add("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
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; | |
public class GsonEx { | |
public static void main(String[] args) throws IOException { | |
Gson gson = new Gson(); | |
String json; | |
System.out.println("Default behaviour "); | |
GsonBuilder gsonBuilder = new GsonBuilder(); | |
Gson prettyGson = gsonBuilder.setPrettyPrinting().create(); | |
json = prettyGson.toJson(new Developer()); | |
System.out.println(json); | |
System.out.println("Including the nulls "); | |
Gson includeNullsGson = gsonBuilder.serializeNulls().create(); | |
String json2 = includeNullsGson.toJson(new Developer()); | |
System.out.println(json2); | |
} | |
} | |
class Developer { | |
private String name; | |
private String classz; | |
List<String> languagesKnown; | |
public Developer() { | |
name = "ajduke"; | |
languagesKnown = new ArrayList<>(); | |
languagesKnown.add("Java"); | |
languagesKnown.add("Scala"); | |
languagesKnown.add("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
package in.ajduke.ap013; | |
import java.io.IOException; | |
import java.lang.reflect.Modifier; | |
import java.util.ArrayList; | |
import java.util.List; | |
import com.google.gson.Gson; | |
import com.google.gson.GsonBuilder; | |
public class GsonEx { | |
public static void main(String[] args) throws IOException { | |
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 "); | |
GsonBuilder excludeFieldsWithModifiers = gsonBuilder | |
.excludeFieldsWithModifiers(Modifier.PRIVATE); | |
Gson create = excludeFieldsWithModifiers.create(); | |
String json2 = create.toJson(new Developer()); | |
System.out.println(json2); | |
} | |
} | |
class Developer { | |
private String name; | |
private String classz; | |
List<String> languagesKnown; | |
public Developer() { | |
name = "ajduke"; | |
languagesKnown = new ArrayList<>(); | |
languagesKnown.add("Java"); | |
languagesKnown.add("Scala"); | |
languagesKnown.add("Ruby"); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment