关于序列化

 

最近项目上用到了序列化的东西,所以就来总结一下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文件,照样可以序列化.

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值