下面就java常用的序列化来做一个比较,分别是java自身的序列化,xml,json,protostuff序列化
先来看看他们的实现
一、java自身序列化
<span style="font-size:18px;">package com.plateno.web.util;
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import com.plateno.platform.dto.UserDTO;
public class JavaSerialized {
public static byte[] serialize(Object obj) {
ObjectOutputStream oos = null;
try {
ByteArrayOutputStream baos = new ByteArrayOutputStream();
oos = new ObjectOutputStream(baos);
oos.writeObject(obj);
byte[] b = baos.toByteArray();
return b;
} catch (Exception e) {
e.printStackTrace();
} finally {
if(oos != null) {
try {
oos.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
return null;
}
public static Object deserilize(byte[] b) {
ObjectInputStream ois = null;
try {
ByteArrayInputStream bais = new ByteArrayInputStream(b);
ois = new ObjectInputStream(bais);
Object obj = ois.readObject();
return obj;
} catch (Exception e) {
e.printStackTrace();
} finally {
if(ois != null) {
try {
ois.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
return null;
}
public static void main(String[] args) {
UserDTO dto = new UserDTO();
long begin = System.nanoTime();
byte[] b = serialize(dto);
System.out.println(System.nanoTime() - begin);//10.2ms
begin = System.nanoTime();
UserDTO oo = (UserDTO)deserilize(b);
System.out.println(System.nanoTime() - begin);//1.8
System.out.println(b.length);//910
}
}
</span>
二、xml序列化:
<span style="font-size:18px;">package com.plateno.util;
import java.io.StringReader;
import java.io.StringWriter;
import javax.xml.bind.JAXBContext;
import javax.xml.bind.JAXBException;
import javax.xml.bind.Marshaller;
import javax.xml.bind.Unmarshaller;
import com.plateno.platform.dto.UserDTO;
public class XmlUtils {
public static String obj2xml(Object obj, Class<?> clz) {
try {
JAXBContext jaxb = JAXBContext.newInstance(clz);
Marshaller marshaller = jaxb.createMarshaller();
StringWriter writer = new StringWriter();
marshaller.marshal(obj, writer);
return writer.toString();
} catch (JAXBException e) {
e.printStackTrace();
}
return null;
}
@SuppressWarnings("unchecked")
public static <T> T xml2obj(String xml, Class<T> clz) {
try {
JAXBContext jaxb = JAXBContext.newInstance(clz);
Unmarshaller unmarshaller = jaxb.createUnmarshaller();
StringReader reader = new StringReader(xml);
return (T)unmarshaller.unmarshal(reader);
} catch (JAXBException e) {
e.printStackTrace();
}
return null;
}
public static void main(String[] args) {
UserDTO dto = new UserDTO();
long begin = System.nanoTime();
String xml = XmlUtils.obj2xml(dto, UserDTO.class);
System.out.println(System.nanoTime() - begin);//95ms
begin = System.nanoTime();
UserDTO o = XmlUtils.xml2obj(xml, UserDTO.class);
System.out.println(System.nanoTime() - begin);//25
System.out.println(xml.length());//552
}
}
</span>
三、json序列化:
<span style="font-size:18px;">package com.plateno.util;
import org.codehaus.jackson.map.ObjectMapper;
import com.plateno.platform.dto.UserDTO;
public class JsonUtils {
private final static ObjectMapper map = new ObjectMapper();
public static <T> T json2obj(String json, Class<T> clz) {
try {
return map.readValue(json, clz);
} catch (Exception e) {
e.printStackTrace();
}
return null;
}
public static <T> String obj2json(T obj) {
try {
return map.writeValueAsString(obj);
} catch (Exception e) {
e.printStackTrace();
}
return null;
}
public static void main(String[] args) {
UserDTO dto = new UserDTO();
long begin = System.nanoTime();
String json = JsonUtils.obj2json(dto);
System.out.println(System.nanoTime() - begin);//51ms
begin = System.nanoTime();
UserDTO aa = JsonUtils.json2obj(json, UserDTO.class);
System.out.println(System.nanoTime() - begin);//23
System.out.println(json.length());//298
}
}
</span>
四、protostuff序列化:
<span style="font-size:18px;">package com.plateno.web.util;
import com.dyuproject.protostuff.Schema;
import com.dyuproject.protostuff.SmileIOUtil;
import com.dyuproject.protostuff.runtime.RuntimeSchema;
import com.plateno.platform.dto.UserDTO;
public class ProtostuffSerialized {
public static <T> byte[] serialize(T obj, Class<T> clz) {
try {
Schema<T> schema = RuntimeSchema.getSchema(clz);
byte[] bytes = SmileIOUtil.toByteArray(obj, schema, false);
return bytes;
} catch (Exception e) {
e.printStackTrace();
}
return null;
}
public static <T> T deserialize(byte[] bytes, Class<T> clz) {
try {
Schema<T> schema = RuntimeSchema.getSchema(clz);
T obj = schema.newMessage();
SmileIOUtil.mergeFrom(bytes, obj, schema, false);
return obj;
} catch (Exception e) {
e.printStackTrace();
}
return null;
}
public static void main(String[] args) {
UserDTO dto = new UserDTO();
long begin = System.nanoTime();
byte[] b = serialize(dto, UserDTO.class);
System.out.println(System.nanoTime() - begin);//9ms
begin = System.nanoTime();
UserDTO o = deserialize(b, UserDTO.class);
System.out.println(System.nanoTime() - begin);//6.9ms
System.out.println(b.length);//245
}
}
</span>
总结
从上面比较可以看出,java自身序列化最快,但是序列化后长度最长,不便于网络传输,json和xml序列化都比较慢,json要比xml快,数据也比xml短,他们都比较容易阅读,xml阅读性优于json,protostuff序列化跟java自身序列化耗时差不多,但是其长度最短,适于网络传输及存储。