package
exam.java.io;
import
java.io.FileInputStream;
import
java.io.ObjectInputStream;
import
java.util.ArrayList;
import
com.google.gson.Gson;
import
com.google.gson.reflect.TypeToken;
public
class
CustometToJson {
public
static
void
main(String[] args)
throws
Exception {
ObjectInputStream ois =
new
ObjectInputStream(
new
FileInputStream(
"customer.data"
));
ArrayList<customer> custs = (ArrayList<customer>)ois.readObject();
Gson gson =
new
Gson();
for
(Customer cust : custs) {
System.out.println(gson.toJson(cust));
}
System.out.println(gson.toJson(custs));
String custsTest =
"[{\"name\":\"홍길동\",\"gender\":\"M\",\"email\":\"hong@hong.com\",\"birthYear\":1980},"
+
"{\"name\":\"홍길서\",\"gender\":\"M\",\"email\":\"seo@hong.com\",\"birthYear\":1985}]"
;
java.lang.reflect.Type collectionType =
new
TypeToken<arraylist<customer>>(){}.getType();
ArrayList<customer> custs2 = gson.fromJson(custsTest, collectionType);
for
(Customer c2 : custs2) {
System.out.println(c2.toString());
}
}
}
</customer></arraylist<customer></customer></customer>