Last active
December 20, 2015 23:29
-
-
Save ajduke/6212773 to your computer and use it in GitHub Desktop.
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; | |
public class GsonEx { | |
public static void main(String[] args) throws IOException { | |
Gson gson = new Gson(); | |
System.out.println("Writing JSON to file ...."); | |
// using try with resources | |
try (FileWriter writer = new FileWriter("d:/output.json")) { | |
gson.toJson(new Developer(), writer); // writing to file | |
} | |
System.out.println("Reading from file.. "); | |
FileReader fileReader = new FileReader("d:/output.json"); | |
dev= gson.fromJson(fileReader, Developer.class); | |
System.out.println(dev); | |
} | |
} | |
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"); | |
} | |
@Override | |
public String toString() { | |
return "Developer [name=" + name + ", classz=" + classz | |
+ ", languagesKnown=" + languagesKnown + "]"; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment