一、对java原生、xml、json、Hessian序列化对比
测试类: package serizlizer; /*** * @desc 序列化测试类 * @author gzyc * */ public class TestMain { public static void main(String[] args) { //java原生序列化 //JavaSerializer serializer = new JavaSerializer(); //xml序列化 //XStreamSerializer serializer = new XStreamSerializer(); //json序列化 //FastJsonSeriliazer serializer = new FastJsonSeriliazer(); HessianSerializer serializer = new HessianSerializer(); User user = new User("张三",18,"男"); byte[] bytes = serializer.serializer(user); /** * 序列化之后的长度对比 * java原生序列化:length:100 * xml: length:88 * json: length:35 * Hessian: length:59 */ System.out.println("byte.length:"+bytes.length); System.out.println(new String(bytes)); //反序列化 User deUser = serializer.decSerializer(bytes, User.class); System.out.println(deUser); } }
序列化接口类 package serizlizer; public interface ISerializer { //序列化 <T> byte[] serializer(T t); //反序列化 <T> T decSerializer(byte[] data, Class<T> clazz); }
java原生序列化 package serizlizer; import java.io.ByteArrayInputStream; import java.io.ByteArrayOutputStream; import java.io.IOException; import java.io.ObjectInputStream; import java.io.ObjectOutputStream; /** * @desc java原生序列化 * @author gzyc * */ public class JavaSerializer implements ISerializer{ @Override public <T> byte[] serializer(T t) { ByteArrayOutputStream aos = new ByteArrayOutputStream(); ObjectOutputStream os = null; try { os = new ObjectOutputStream(aos); os.writeObject(t); return aos.toByteArray(); } catch (IOException e) { e.printStackTrace(); }finally { try { if(aos!=null) aos.close(); if(os!=null) os.close(); } catch (IOException e) { e.printStackTrace(); } } return new byte[0]; } @Override public <T> T decSerializer(byte[] data, Class<T> clazz) { ByteArrayInputStream stream = new ByteArrayInputStream(data); ObjectInputStream is = null; try { is = new ObjectInputStream(stream); return (T)is.readObject(); } catch (Exception e) { e.printStackTrace(); }finally { try { if(stream!=null) stream.close(); if(is!=null) is.close(); } catch (IOException e) { e.printStackTrace(); } } return null; } }
xml序列化 package serizlizer; import com.thoughtworks.xstream.XStream; import com.thoughtworks.xstream.io.xml.DomDriver; public class XStreamSerializer implements ISerializer{ private XStream xStream = new XStream(new DomDriver()); @Override public <T> byte[] serializer(T t) { return xStream.toXML(t).getBytes(); } @Override public <T> T decSerializer(byte[] data, Class<T> clazz) { return (T)xStream.fromXML(new String(data)); } }
Json序列化 package serizlizer; import com.alibaba.fastjson.JSON; public class FastJsonSeriliazer implements ISerializer{ @Override public <T> byte[] serializer(T t) { return JSON.toJSONString(t).getBytes(); } @Override public <T> T decSerializer(byte[] data, Class<T> clazz) { return JSON.parseObject(new String(data),clazz); } }
package serizlizer; import java.io.ByteArrayInputStream; import java.io.ByteArrayOutputStream; import java.io.IOException; import com.caucho.hessian.io.HessianInput; import com.caucho.hessian.io.HessianOutput; public class HessianSerializer implements ISerializer{ @Override public <T> byte[] serializer(T t) { ByteArrayOutputStream outputStream=new ByteArrayOutputStream(); HessianOutput hessianOutput=new HessianOutput(outputStream); try { hessianOutput.writeObject(t); return outputStream.toByteArray(); } catch (IOException e) { e.printStackTrace(); } return new byte[0]; } @Override public <T> T decSerializer(byte[] data, Class<T> clazz) { ByteArrayInputStream inputStream=new ByteArrayInputStream(data); HessianInput hessianInput=new HessianInput(inputStream); try { return (T)hessianInput.readObject(); } catch (IOException e) { e.printStackTrace(); } return null; } }
实体类 package serizlizer; import java.io.Serializable; public class User implements Serializable{ private static final long serialVersionUID = 7751730997726292334L; private String name; private int age; private String sex; public User(String name, int age, String sex) { this.name = name; this.age = age; this.sex = sex; } 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 getSex() { return sex; } public void setSex(String sex) { this.sex = sex; } @Override public String toString() { return "User [name=" + name + ", age=" + age + ", sex=" + sex + "]"; } }