最近項目上用到了序列化的東西,是以就來總結一下java序列化的一些基礎東西,已經google 出的一款可以用于序列話的工具Gson.
一. java 序列化
什麼是序列化呢,在java中實際就是用對象轉換為位元組,在有位元組轉換為對象的過程.有什麼用處呢.
首先是可以進行簡單的持久化,你可以把一個對象序列化後放入檔案系統中,然後在需要的時候反序列化來恢複這個對象.
其次你可以通過它來儲存一個對象的狀态
最後是除了clone之外複制對象的另一種方式.
在java中怎麼用呢,很簡單,隻要這個對象實作了Serializable接口就行了
簡單列個代碼:
首先是個school的對象:
public class School implements Serializable{
/**
*
*/
private static final long serialVersionUID = 1L;
private String schName;
private String schCode ;
public String getSchName() {
return schName;
}
public void setSchName(String schName) {
this.schName = schName;
}
public String getSchCode() {
return schCode;
}
public void setSchCode(String schCode) {
this.schCode = schCode;
}
}
然後是個student 的對象,我為了讓它更複雜點讓student包含了school的引用.
package com.yecg.java.serialization;
import java.io.Serializable;
public class Student implements Serializable{
/**
*
*/
private static final long serialVersionUID = 1L;
private String name;
private int age;
private School school;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
public School getSchool() {
return school;
}
public void setSchool(School school) {
this.school = school;
}
}
那麼現在我們就來序列化和反序列化他來看下有什麼效果:
package com.yecg.java.serialization;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
public class SerializationTest {
public static void serializationObject(Object a) throws FileNotFoundException, IOException{
ObjectOutputStream out = new ObjectOutputStream( new FileOutputStream("worm.out"));
out.writeObject(a);
out.close();
}
public static Object deserializationObject() throws FileNotFoundException, IOException, ClassNotFoundException{
ObjectInputStream in = new ObjectInputStream( new FileInputStream("worm.out"));
Object b = in.readObject();
in.close();
return b;
}
/**
* @param args
* @throws IOException
* @throws FileNotFoundException
* @throws ClassNotFoundException
*/
public static void main(String[] args) throws FileNotFoundException, IOException, ClassNotFoundException {
// TODO Auto-generated method stub
School school = new School();
school.setSchName("Chong QING");
school.setSchCode("0001");
Student student = new Student();
student.setName("yecg");
student.setAge(10);
student.setSchool(school);
serializationObject(student);
}
}
你可以看到檔案裡面的資訊是這樣的:
¨Ìsr#com.yecg.java.serialization.StudentIageLnametLjava/lang/String;Lschoolt$Lcom/yecg/java/serialization/School;xp
tyecgsr"com.yecg.java.serialization.SchoolLschCodeq~LschNameq~xpt0001t
Chong QING
現在我們來反序列化這個檔案.同樣是在main方法中加入
Student deseStudent =(Student)deserializationObject();
System.out.println(deseStudent.getName());
System.out.println(deseStudent.getAge());
System.out.println(deseStudent.getSchool().getSchName());
System.out.println(deseStudent.getSchool().getSchCode());
結果是:
yecg
10
Chong QING
0001
不光是student,連它裡面包含的school也被反序列化出來了.
在我的項目中實際上也有是要用他的對象複制功能,但在效率方面序列化确實要比clone低,但是随着jdk的優化,現在這個差距也是在可以接受的範圍之内了,并且他操作簡單,是以可以作為clone的另一種替代方案
當然序列化不是這麼簡單就結束了,比如如果你某個字段不想序列化,那麼可以使用transient等.
二 . Gson
Gson是google出的一款工具,它可以友善的把對象轉為json,把json轉為對象.這有很多用處,比如我們經常用的的ajax,就可以在背景把對象轉為json傳到前台然後由前台顯示.
當然同時它也有序列化的作用,就來簡單看下它是如何序列化的
public static void serializationObject(Object a) throws IOException{
Gson gson = new Gson();
String json =gson.toJson(a);
BufferedWriter output = new BufferedWriter(new FileWriter("gson.out"));
output.write(json);
output.close();
}
public static Object deserializationObject() throws IOException {
Gson gson = new Gson();
BufferedReader input = new BufferedReader(new FileReader("gson.out"));
String json =input.readLine();
input.close();
Object a =gson.fromJson(json, Student.class);
return a;
}
可以看到gson.out檔案裡是這樣的
{"age":10,"school":{"schName":"Chong QING","schCode":"0001"}}
是json的表示.,放到檔案中後可以序列化回來.
gson還有一些優點,比如支援泛型,看下官方的例子:
Gson gson = new Gson();
Collection<Integer> ints = Lists.immutableList(1,2,3,4,5);
(Serialization)
String json = gson.toJson(ints); ==> json is [1,2,3,4,5]
(Deserialization)
Type collectionType = new TypeToken<Collection<Integer>>(){}.getType();
Collection<Integer> ints2 = gson.fromJson(json, collectionType);
還有如果你沒有這個類的source檔案,隻是class檔案,照樣可以序列化.