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 *********** | |
{"firstName":"ajduke","languagesKnown":["Java","Scala","Ruby"]} | |
******* Pretty formatting ********* | |
{ | |
"firstName": "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
package in.ajduke.ap013; | |
import java.io.FileWriter; | |
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
package test; | |
import java.util.concurrent.TimeUnit; | |
import org.junit.After; | |
import org.junit.AfterClass; | |
import org.junit.Before; | |
import org.junit.BeforeClass; | |
import org.junit.Ignore; | |
import org.junit.Test; |
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 { | |
@SerializedName("firstName") | |
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
public class Example33 { | |
public static void main(String[] args) { | |
Gson gson = new GsonBuilder().setVersion(2.0).create(); | |
String json = gson.toJson(new ExampleClass()); | |
System.out.println("Output for version 2.0..."); | |
System.out.println(json); | |
gson= new GsonBuilder().setVersion(1.0).create(); | |
json = gson.toJson(new ExampleClass()); | |
System.out.println("\nOutput for version 1.0..."); |
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
Output for version 2.0... | |
{"field":"field","newField1":"field 1","newField2":"field 2"} | |
Output for version 1.0... | |
{"field":"field","newField1":"field 1"} | |
Output for No version set... | |
{"field":"field","newField1":"field 1","newField2":"field 2"} |
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
public class Example34 { | |
public static void main(String[] args) { | |
String str ="<myval>"; | |
Gson gson = new Gson(); | |
System.out.println("Normal behaviour..."); | |
System.out.println(gson.toJson(str)); | |
System.out.println("\nDisabled html escaping..."); | |
gson = new GsonBuilder().disableHtmlEscaping().create(); | |
System.out.println(gson.toJson(str)); |
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
Normal behaviour... | |
"\u003cmyval\u003e" | |
Disabled html escaping... | |
"<myval>" |
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 { | |
// this field will be included | |
@Expose | |
private String name; | |
private String classz; | |
List<String> languagesKnown; | |
public Developer() { | |
name = "ajduke"; | |
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
public class GsonEx { | |
public static void main(String[] args) throws IOException { | |
Gson gson = new GsonBuilder().excludeFieldsWithoutExposeAnnotation().create(); | |
String json = gson.toJson(new Developer()); | |
System.out.println(json); | |
} | |
} |