Skip to content

Instantly share code, notes, and snippets.

@ajduke

ajduke/Exe5.java

Last active Dec 20, 2015
Embed
What would you like to do?
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