为什么要用序列化
对象序列化机制
允许把内存中的Java对象转换成平台无关的二进制流,从而允许把这种二进制流持久地保存在磁盘上,或通过网络将这种二进制流传输到另一个网络节点。//当其它程序获取了这种二进制流,就可以恢复成原来的Java对象。
对象的类型
和对象中存储的属性
等信息。字节序列写出到文件之后,相当于文件中持久保存
反序列化
。对象的数据
、对象的类型
和对象中存储的数据
字节数据
项目中为什么不使用JAVA内置序列化
1、跨平台性
JAVA序列化只能用在JAVA和JAVA项目之间,要是JAVA和Python项目之间需要相互传输数据就不能使用JAVA序列化了。这个问题就是致命的,因为很多公司不可能只使用一门语言来做项目。
因为这个原因,很多公司前后端分离和微服务项目之间传输数据使用JSON
2、效率上的问题
JAVA 序列化 VS NIO ByteBuffer序列化
结论:NIO Byte Buffer序列化长度和时间都比JAVA内置序列化好一些,但是在单体执行下优势不算明显
JAVA 序列化效率测试:
public class JDKSerTest { public static void main(String[] args) throws IOException { String str = "关于JAVA序列化的效率测试"; long startTime = System.currentTimeMillis(); for (int i = 0; i < 10000000; i++) { ByteArrayOutputStream os = new ByteArrayOutputStream(); ObjectOutputStream out = new ObjectOutputStream(os); out.writeObject(str); out.flush(); out.close(); byte[] testByte = os.toByteArray(); if (i==0){ System.out.println("ObjectOutputStream序列化长度:" + testByte.length); } os.close(); } long endTime = System.currentTimeMillis(); System.out.println("ObjectOutputStream序列化时间" + (endTime - startTime) + "\n"); } }
NIO ByteBuffer效率测试:
public class ByteBufferTest { public static void main(String[] args) throws IOException { String str = "关于JAVA序列化的效率测试"; long startTime = System.currentTimeMillis(); for (int i = 0; i < 10000000; i++) { ByteBuffer byteBuffer = ByteBuffer.allocate(2048); byte[] s = str.getBytes(); byteBuffer.putInt(s.length); byteBuffer.put(s); byteBuffer.flip(); byte[] bytes = new byte[byteBuffer.remaining()]; if (i==0){ System.out.println("ByteBuffer序列化长度:" + bytes.length); } } long endTime = System.currentTimeMillis(); System.out.println("ByteBuffer序列化时间" + (endTime - startTime) + "\n"); } }
Java序列化 VS JSON序列化
结论:通过测试可以得出结论,JSON序列化不论是大小还是执行时间都比JAVA内置序列化快了3倍左右
JAVA 序列化效率测试:
public class JDKSerMapTest { public static void main(String[] args) throws IOException { long startTime = System.currentTimeMillis(); for (int i = 0; i < 10000000; i++) { Map map = new HashMap(); map.put(String.valueOf(i),"关于JAVA序列化的效率测试"); ByteArrayOutputStream os = new ByteArrayOutputStream(); ObjectOutputStream out = new ObjectOutputStream(os); out.writeObject(map); out.flush(); out.close(); byte[] testByte = os.toByteArray(); if (i==0){ System.out.println("ObjectOutputStream序列化长度:" + testByte.length); } os.close(); } long endTime = System.currentTimeMillis(); System.out.println("ObjectOutputStream序列化时间" + (endTime - startTime) + "\n"); } }
JSON 序列化效率测试:
public class JsonMapTest { public static void main(String[] args) throws IOException { ObjectMapper objectMapper = new ObjectMapper(); long startTime = System.currentTimeMillis(); for (int i = 0; i < 10000000; i++) { Map map = new HashMap(); map.put(String.valueOf(i),"关于JAVA序列化的效率测试"); String json = objectMapper.writeValueAsString(map); if (i==0){ System.out.println("JSON序列化长度:" + json.getBytes().length); } } long endTime = System.currentTimeMillis(); System.out.println("JSON序列化时间" + (endTime - startTime) + "\n"); } }
除了json序列化之外还有哪些好的序列化方案
1、Spring Boot内置Jackson的JSON序列化
2、Dubbo内置的hessian(改)序列化方案
3、Google的Protocol Buffers序列化
4、Hadoop 大数据序列化 自研发
转载请注明:西门飞冰的博客 » JAVA基础—项目中为什么不使用JAVA内置序列化