现在的比较主流的java序列化技术有以下几种:
JSON、hessian、XML、protobuf、kryo、msgpack、FST、thrift、protostuff、avro。
下面就以其中几种作为示例:
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>fastjson</artifactId>
<version>1.2.29</version>
</dependency>
<dependency>
<groupId>org.codehaus.jackson</groupId>
<artifactId>jackson-mapper-asl</artifactId>
<version>1.9.13</version>
</dependency>
<dependency>
<groupId>com.baidu</groupId>
<artifactId>jprotobuf</artifactId>
<version>2.1.2</version>
</dependency>
<dependency>
<groupId>com.caucho</groupId>
<artifactId>hessian</artifactId>
<version>4.0.38</version>
</dependency>
public class Computer implements Serializable {
@Protobuf(fieldType = FieldType.INT32,order = 1)
private int size;
@Protobuf(fieldType = FieldType.STRING,order = 2)
private String color;
public int getSize() {
return size;
}
public void setSize(int size) {
this.size = size;
}
public String getColor() {
return color;
}
public void setColor(String color) {
this.color = color;
}
@Override
public String toString() {
return "Computer{" +
"size='" + size + '\'' +
", color=" + color +
'}';
}
}
public class serializableMethodDemo {
//对象初始化
public static Computer init(){
Computer computer=new Computer();
computer.setSize(25);
computer.setColor("黑色");
return computer;
}
//fastJson序列化
public static void fastJsonSerializable(){
Computer computer=init();
String text = null;
long startTime=System.currentTimeMillis();
for(int i=0;i<10000;i++){
text=JSONObject.toJSONString(computer);
}
System.out.println("fastJson序列化:"+(System.currentTimeMillis()-startTime)
+" ms;序列化后大小:"+text.length());
Computer computer1=JSONObject.parseObject(text,Computer.class);
System.out.println(computer1);
}
//jack序列化
public static void jackSerializable() throws IOException {
Computer computer=init();
ObjectMapper mapper=new ObjectMapper();
byte[] writeBytes=null;
long startTime=System.currentTimeMillis();
for(int i=0;i<10000;i++){
writeBytes=mapper.writeValueAsBytes(computer);
}
System.out.println("\njackJson序列化:"+(System.currentTimeMillis()-startTime)
+" ms;序列化后大小:"+writeBytes.length);
Computer computer1=mapper.readValue(writeBytes,Computer.class);
System.out.println(computer1);
}
//protoBuf序列化
public static void protoBufSerializable() throws IOException {
Computer computer=init();
Codec<Computer> computerCodec= ProtobufProxy.create(Computer.class,false);
long startTime=System.currentTimeMillis();
byte[] bytes=null;
for(int i=0;i<10000;i++){
bytes=computerCodec.encode(computer);
}
System.out.println("\nprotoBuf序列化:"+(System.currentTimeMillis()-startTime)
+" ms;序列化后大小:"+bytes.length);
Computer computer1=computerCodec.decode(bytes);
System.out.println(computer1);
}
//hessian序列化
public static void hessianSerializable() throws IOException {
Computer computer=init();
ByteArrayOutputStream byteArrayOutputStream=new ByteArrayOutputStream();
HessianOutput hessianOutput=new HessianOutput(byteArrayOutputStream);
long startTime=System.currentTimeMillis();
for(int i=0;i<10000;i++){
hessianOutput.writeObject(computer);
//因为hessian是叠加的,要想输出一个对象,需要判断
if(i==0){
System.out.println("\nhessian序列化大小:" + byteArrayOutputStream.toByteArray().length);
}
}
System.out.println("hessian序列化时间:" + (System.currentTimeMillis() - startTime)+" ms");
HessianInput hessianInput=new HessianInput(new ByteArrayInputStream(byteArrayOutputStream.toByteArray()));
Computer computer1= (Computer) hessianInput.readObject();
System.out.println(computer1);
}
public static void main(String[] args) throws IOException {
fastJsonSerializable();
jackSerializable();
protoBufSerializable();
hessianSerializable();
}
}
运行结果如下:
几种序列化的比较:
fastjson:序列化时间最长,长度适中。由于可以跨平台通用,学习成本低,所以比较流行。
jackjson:时间和长度比较适中。
protobuf:序列化时间短,长度也很小。示例中用的是百度封装的谷歌的protobuf,由于谷歌protobuf写法很麻烦,还要生成文件之类的,所以没有在示例中用。
hessian: 序列化时间短,但是序列化的长度很大。
需要注意:
(1)protobuf的实体类属性要加注解。
由于Codec<Computer> computerCodec= ProtobufProxy.create(Computer.class,false);
这一步的初始化时间很长,所以计算开始时间要从下一个步骤开始。
(2)hessian的实体类必须要实现Serializable接口,其它的方式不需要。