序列化和反序列化
Posted 邵鸿鑫
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了序列化和反序列化相关的知识,希望对你有一定的参考价值。
互联网的产生带来了机器间通讯的需求,而互联通讯的双方需要采用约定的协议,序列化和反序列化属于通讯协议的一部分。通讯协议往往采用分层模型,不同模型每层的功能定义以及颗粒度不同,例如:TCP/IP协议是一个四层协议,而OSI模型却是七层协议模型。在OSI七层协议模型中展现层(Presentation Layer)的主要功能是把应用层的对象转换成一段连续的二进制串,或者反过来,把二进制串转换成应用层的对象--这两个功能就是序列化和反序列化。一般而言,TCP/IP协议的应用层对应与OSI七层协议模型的应用层,展示层和会话层,所以序列化协议属于TCP/IP协议应用层的一部分。本文对序列化协议的讲解主要基于OSI七层协议模型。序列化: 将数据结构或对象转换成二进制串的过程
反序列化:将在序列化过程中所生成的二进制串转换成数据结构或者对象的过程
下面以Java序列化、Hessian序列化、JSON序列化和XML序列化为例简单分析序列化与反序列化的过程。
Person类
/**
*
*/
package com.http.testserialization;
import java.util.Date;
/**
* @Description: 人的属性集合
* @Author chenkangxian
* @Date 2013-6-25 下午4:45:59
* @Copyright: 2012 chenkangxian, All rights reserved.
**/
class Person implements java.io.Serializable
private static final long serialVersionUID = 1L;
private String name;
private int age;
private String address;
private Date birth;
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 String getAddress()
return address;
public void setAddress(String address)
this.address = address;
public Date getBirth()
return birth;
public void setBirth(Date birth)
this.birth = birth;
Java序列化
/**
*
*/
package com.http.testserialization;
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.util.Date;
/**
* @Description: java序列化
* @Author chenkangxian
* @Date 2013-6-25 下午3:43:08
* @Copyright: 2012 chenkangxian, All rights reserved.
**/
public class TestJavaSerialization
public static void main(String[] args) throws IOException, ClassNotFoundException
Person zhansan = new Person();
zhansan.setAddress("hangzhou");
zhansan.setAge(30);
zhansan.setBirth(new Date());
zhansan.setName("zhansan");
//定义一个字节数组输出流
ByteArrayOutputStream os = new ByteArrayOutputStream();
//对象输出流
ObjectOutputStream out = new ObjectOutputStream(os);
//将对象写入到字节数组输出,进行序列化
out.writeObject(zhansan);
byte[] zhansanByte = os.toByteArray();
//字节数组输入流
ByteArrayInputStream is = new ByteArrayInputStream(zhansanByte);
//执行反序列化,从流中读取对象
ObjectInputStream in = new ObjectInputStream(is);
Person person = (Person)in.readObject();
System.out.println("name : " + person.getName() + ", age : " + person.getAge());
Hessian序列化
/**
*
*/
package com.http.testserialization;
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.util.Date;
import com.caucho.hessian.io.HessianInput;
import com.caucho.hessian.io.HessianOutput;
/**
* @Description: hessian序列化
* @Author chenkangxian
* @Date 2013-6-25 下午3:57:53
* @Copyright: 2012 chenkangxian, All rights reserved.
**/
public class TestHessianSerialization
public static void main(String[] args) throws IOException
Person zhansan = new Person();
zhansan.setAddress("hangzhou");
zhansan.setAge(30);
zhansan.setBirth(new Date());
zhansan.setName("zhansan");
ByteArrayOutputStream os = new ByteArrayOutputStream();
//hessian的序列化输出
HessianOutput ho = new HessianOutput(os);
ho.writeObject(zhansan);
byte[] zhansanByte = os.toByteArray();
ByteArrayInputStream is = new ByteArrayInputStream(zhansanByte);
//hessian的反序列化读取对象
HessianInput hi = new HessianInput(is);
Person person = (Person)hi.readObject();
System.out.println("name : " + person.getName() + ", age : " + person.getAge());
JSON序列化
/**
*
*/
package com.http.testserialization;
import java.io.IOException;
import java.io.StringWriter;
import java.util.Date;
import org.codehaus.jackson.JsonFactory;
import org.codehaus.jackson.JsonGenerator;
import org.codehaus.jackson.map.ObjectMapper;
/**
* @Description: 使用json进行序列化
* @Author chenkangxian
* @Date 2013-7-3 下午8:52:34
* @Copyright: 2012 chenkangxian, All rights reserved.
**/
public class TestJSONSerialization
public static void main(String[] args) throws IOException
Person person = new Person();
person.setAddress("hangzhou,china");
person.setAge(18);
person.setBirth(new Date());
person.setName("zhangsan");
//json对象序列化
String personJson = null;
ObjectMapper mapper = new ObjectMapper();
StringWriter sw = new StringWriter();
JsonGenerator gen = new JsonFactory().createJsonGenerator(sw);
mapper.writeValue(gen, person);
gen.close();
personJson = sw.toString();
//json对象反序列化
Person zhangsan = (Person)mapper.readValue(personJson, Person.class);
System.out.println(personJson);
System.out.println(zhangsan.getName());
XML序列化
/**
*
*/
package com.http.testserialization;
import java.util.Date;
import com.thoughtworks.xstream.XStream;
import com.thoughtworks.xstream.io.xml.DomDriver;
/**
* @Description: 使用xml进行对象序列化
* @Author chenkangxian
* @Date 2013-7-3 下午10:10:18
* @Copyright: 2012 chenkangxian, All rights reserved.
**/
public class TestXMLSerialization
/**
* @param args
*/
public static void main(String[] args)
Person person = new Person();
person.setAddress("hangzhou,china");
person.setAge(18);
person.setBirth(new Date());
person.setName("zhangsan");
//将person对象序列化为XML
XStream xStream = new XStream(new DomDriver());
//设置Person类的别名
xStream.alias("person", Person.class);
String personXML = xStream.toXML(person);
//将XML反序列化还原为person对象
Person zhangsan = (Person)xStream.fromXML(personXML);
System.out.println(personXML);
System.out.println(zhangsan.getBirth());
以上是关于序列化和反序列化的主要内容,如果未能解决你的问题,请参考以下文章