企业网站页脚,做签到的网站,手机网站判断代码,企业网站建设论文什么是序列化和反序列化#xff1f; 把对象转换成字节序列把字节序列恢复成对象 结合OSI七层协议模型#xff0c;序列化和反序列化是在那一层做的#xff1f; 在OSI七层模型中#xff0c;序列化工作的层级是表示层。这一层的主要功能包括把应用层的对象转换成一段连续的二进…什么是序列化和反序列化 把对象转换成字节序列把字节序列恢复成对象 结合OSI七层协议模型序列化和反序列化是在那一层做的 在OSI七层模型中序列化工作的层级是表示层。这一层的主要功能包括把应用层的对象转换成一段连续的二进制串或者反过来把二进制串转换成应用层的对象。 为什么要使用序列化
序列化的作用是将对象转换为可以存储或传输形式的过程这样对象可以存储在内存或文件中内存或文件实际上是以字节为单位的我们要转换成机器能够认识的单位。比方说我们需要把一个对象传输到另一台机器
网络传输跨机器要转换成机器能识别的格式反之 Serializable接口的作用
做标记实现Serializable接口的类表示可以序列化告诉程序实现了它的对象是可以被序列化的。如果对某个没有实现Serializable接口的类直接序列化将会报”NotSerializableException“错误。 这个时候有些人就会想当对象中的某些信息比方说密码或身份证我不想暴露有没有什么办法当然JDK已经给我们想到了这些问题为了保障数据的安全性当实现serialVersional接口实现序列化的时候可以使用transient或static关键字修饰某个字段被修饰的字段就不会被序列化反序列时也不会被持久化和恢复会被置成默认值
//因为序列化保存的是对象的状态而 static 修饰的字段属于类的状态因此可以证明序列化并
//不保存 static 修饰的字段。
public static Long cardId;// transient临时修饰成员,阻止字段被序列化
//当对象被反序列化时被transient修饰的变量值不会被持久化和恢复会被置成类型默认值
private transient String password;
注意
普通对象序列化和反序列化返回的是两个不同的对象
枚举类型序列化的对象是static final的不会回收所以反序列化回来的对象和原来的对象是同一个 序列化和反序列化的方式有哪些
JDK自带序列化方式Kryo JDK自带序列化方式
Student类
package org.example.SerializableTest;import lombok.Data;import java.io.Serializable;Data
public class Student implements Serializable {private static final long serialVersionUID 1L;public Student(String name,Integer age,Integer score,Integer studentId,String password){this.namename;this.ageage;this.scorescore;this.studentIdstudentId;this.passwordpassword;}private String name;private Integer age;private Integer score;private Integer studentId;// transient瞬态修饰成员,不会被序列化//当对象被反序列化时被transient修饰的变量值不会被持久化和恢复会被置成类型默认值private transient String password;
} 序列化
Student student new Student(唐三, 18, 100, 001, 123456);try {FileOutputStream fos new FileOutputStream(dlm.txt);ObjectOutputStream oos new ObjectOutputStream(fos);oos.writeObject(student);oos.close();} catch (IOException e) {e.printStackTrace();
} 反序列化 通过输出文件中的对象我们可以发现用transient修饰的password字段被隐藏了保证了信息的安全 在Student类中不知道大家有没有发现serialVersionUID这个字段这个字段的作用用来验证版本的对版本进行表示。在序列化的时候会记录将serialVersionUID
在反序列化的时候将serialVersionUID和本地实体类的serialVersionUID进行比较如果一致则可以反序列化否则则说明序列化版本不一致
serialVersionUID默认是“1L”可以自动生成 Kryo
是一个支持序列化/反序列化的工具 KryoStudent类
package org.example.SerializableTest;import lombok.Data;Data
class KryoStudent {public KryoStudent() {}public KryoStudent(String name, Integer age, Integer score, Integer studentId, String password) {this.name name;this.age age;this.score score;this.studentId studentId;this.password password;}private String name;private Integer age;private Integer score;private Integer studentId;private transient String password;Overridepublic String toString() {return KryoStudent{ name name \ , age age , score score , studentId studentId , password password \ };}
} package org.example.SerializableTest;import com.esotericsoftware.kryo.Kryo;
import com.esotericsoftware.kryo.io.Input;
import com.esotericsoftware.kryo.io.Output;
import org.objenesis.strategy.StdInstantiatorStrategy;import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;public class KryoDemo {public static void main(String[] args) throws FileNotFoundException {//创建一个 Kryo 对象Kryo kryo new Kryo();//将对象进行注册kryo.register(KryoStudent.class);KryoStudent object new KryoStudent(唐三, 18, 100, 001, 123456);//序列化Output output new Output(new FileOutputStream(dlm.txt));//将 Java 对象序列化为二进制流kryo.writeObject(output, object);output.close();Input input new Input(new FileInputStream(dlm.txt));//将二进制流反序列化为 Java 对象KryoStudent object2 kryo.readObject(input, KryoStudent.class);System.out.println(object2);input.close();}//反序列化public void setSerializableObjectStudent() throws FileNotFoundException {Output output new Output(new FileOutputStream(dlm.txt));Kryo kryo new Kryo();kryo.setReferences(false);kryo.setRegistrationRequired(false);kryo.setInstantiatorStrategy(new StdInstantiatorStrategy());kryo.register(KryoStudent.class);}
}
Kryo方式有什么缺点吗
不是线程安全的每个线程都有自己的Kryo对象、输入和输出实例只支持Java实现 既然JDK和Kryo都可以进行序列化和反序列化那分别用JDK和Kryo提供的序列化和反序列化方式对10000个对象进行转换从时间上我们来看一下它的性能
JDK和Kryo性能对比
Student类
import lombok.Data;import java.io.Serializable;Data
public class Student implements Serializable {public Student(String name, Integer age, Integer score, Integer studentId, String password){this.namename;this.ageage;this.scorescore;this.studentIdstudentId;this.passwordpassword;}private String name;private Integer age;private Integer score;private Integer studentId;// transient瞬态修饰成员,不会被序列化private transient String password;
} KryoStudent类
import lombok.Data;Data
class KryoStudent {//Kryo不支持包含无参构造器类的反序列化所以需要把无参构造器显示出来。public KryoStudent() {}public KryoStudent(String name, Integer age, Integer score, Integer studentId, String password) {this.name name;this.age age;this.score score;this.studentId studentId;this.password password;}private String name;private Integer age;private Integer score;private Integer studentId;private transient String password;Overridepublic String toString() {return KryoStudent{ name name \ , age age , score score , studentId studentId , password password \ };}
} JDK序列化和反序列化
import com.esotericsoftware.kryo.Kryo;
import com.esotericsoftware.kryo.KryoException;
import com.esotericsoftware.kryo.io.Input;
import com.esotericsoftware.kryo.io.Output;
import org.junit.Test;import java.io.*;
import java.util.ArrayList;public class test5 {//序列化Testpublic void test1() throws Exception {long time System.currentTimeMillis();ObjectOutputStream oos new ObjectOutputStream(new FileOutputStream(dlm.txt));for (int i 0; i 10000; i) {oos.writeObject(new Student(唐三, 18, 100, i, 123456));}oos.close();System.out.println(JDK序列化消耗的时间 (System.currentTimeMillis() - time));}//反序列化Testpublic void test2() throws Exception {long time System.currentTimeMillis();ObjectInputStream ois new ObjectInputStream(new FileInputStream(dlm.txt));Student student null;try {while (null ! (student (Student) ois.readObject())) {}} catch (EOFException e) {}System.out.println(JDK反序列化消耗的时间 (System.currentTimeMillis() - time));}
}Kryo序列化和反序列化
import com.esotericsoftware.kryo.Kryo;
import com.esotericsoftware.kryo.KryoException;
import com.esotericsoftware.kryo.io.Input;
import com.esotericsoftware.kryo.io.Output;
import org.junit.Test;import java.io.*;
import java.util.ArrayList;public class test5 {//序列化Testpublic void test1() throws Exception {long time System.currentTimeMillis();Kryo kryo new Kryo();//将对象进行注册kryo.register(KryoStudent.class);Output output new Output(new FileOutputStream(dlm.txt));//存储10000个对象for (int i 0; i 10000; i) {kryo.writeObject(output, new KryoStudent(唐三, 18, 100, i, 123456));}output.close();System.out.println(Kryo序列化消耗的时间 (System.currentTimeMillis() - time));}//反序列化Testpublic void test2() throws Exception {long time System.currentTimeMillis();Kryo kryo new Kryo();kryo.register(KryoStudent.class);Input input new Input(new FileInputStream(dlm.txt));KryoStudent student null;//反序列化文件中的对象try {while (null ! (student kryo.readObject(input, KryoStudent.class))) {}} catch (KryoException e) {}input.close();System.out.println(Kryo反序列化消耗的时间 (System.currentTimeMillis() - time));}
}结果为 从输出结果上我们发现时间上有很大的不同Kryo序列化和反序列化相比于JDK都快很多那为什么会产生这样的结果呢
Kryo依赖于字节码生成机制底层使用了ASM库Kryo序列化时只将对象的信息、对象属性值的信息等进行序列化没有将类field的描述信息进行序列化这样就比JDK自己的序列化结果要小很多而且速度肯定更快。Kryo序列化出的结果是其自定义的、独有的一种格式因此像redis这样可以存储二进制数据的存储引擎可以直接将Kryo序列化出来的数据存进去也可以选择转换成String的形式存储在其他存储引擎中性能有损耗 文章转载自: http://www.morning.bpyps.cn.gov.cn.bpyps.cn http://www.morning.hlxpz.cn.gov.cn.hlxpz.cn http://www.morning.hmmnb.cn.gov.cn.hmmnb.cn http://www.morning.bxyzr.cn.gov.cn.bxyzr.cn http://www.morning.jbctp.cn.gov.cn.jbctp.cn http://www.morning.rklgm.cn.gov.cn.rklgm.cn http://www.morning.mfcbk.cn.gov.cn.mfcbk.cn http://www.morning.duqianw.com.gov.cn.duqianw.com http://www.morning.lveyue.com.gov.cn.lveyue.com http://www.morning.lhgkr.cn.gov.cn.lhgkr.cn http://www.morning.mwkwg.cn.gov.cn.mwkwg.cn http://www.morning.wbfly.cn.gov.cn.wbfly.cn http://www.morning.wjjxr.cn.gov.cn.wjjxr.cn http://www.morning.wpcfm.cn.gov.cn.wpcfm.cn http://www.morning.jwtwf.cn.gov.cn.jwtwf.cn http://www.morning.gbrps.cn.gov.cn.gbrps.cn http://www.morning.rdqzl.cn.gov.cn.rdqzl.cn http://www.morning.hxmqb.cn.gov.cn.hxmqb.cn http://www.morning.nqcts.cn.gov.cn.nqcts.cn http://www.morning.nhlnh.cn.gov.cn.nhlnh.cn http://www.morning.rdwm.cn.gov.cn.rdwm.cn http://www.morning.zdsdn.cn.gov.cn.zdsdn.cn http://www.morning.skrrq.cn.gov.cn.skrrq.cn http://www.morning.yhrfg.cn.gov.cn.yhrfg.cn http://www.morning.gbljq.cn.gov.cn.gbljq.cn http://www.morning.sjbpg.cn.gov.cn.sjbpg.cn http://www.morning.wpjst.cn.gov.cn.wpjst.cn http://www.morning.xbdd.cn.gov.cn.xbdd.cn http://www.morning.hwljx.cn.gov.cn.hwljx.cn http://www.morning.dtnyl.cn.gov.cn.dtnyl.cn http://www.morning.ttcmdsg.cn.gov.cn.ttcmdsg.cn http://www.morning.sqyjh.cn.gov.cn.sqyjh.cn http://www.morning.trrrm.cn.gov.cn.trrrm.cn http://www.morning.mjtft.cn.gov.cn.mjtft.cn http://www.morning.bwkzn.cn.gov.cn.bwkzn.cn http://www.morning.xtyyg.cn.gov.cn.xtyyg.cn http://www.morning.sqgqh.cn.gov.cn.sqgqh.cn http://www.morning.swsrb.cn.gov.cn.swsrb.cn http://www.morning.prmbb.cn.gov.cn.prmbb.cn http://www.morning.nkkr.cn.gov.cn.nkkr.cn http://www.morning.qpqwb.cn.gov.cn.qpqwb.cn http://www.morning.zlxrg.cn.gov.cn.zlxrg.cn http://www.morning.pbdnj.cn.gov.cn.pbdnj.cn http://www.morning.rnmc.cn.gov.cn.rnmc.cn http://www.morning.mdfxn.cn.gov.cn.mdfxn.cn http://www.morning.nsrtvu.com.gov.cn.nsrtvu.com http://www.morning.fbhmn.cn.gov.cn.fbhmn.cn http://www.morning.plxhq.cn.gov.cn.plxhq.cn http://www.morning.lxjxl.cn.gov.cn.lxjxl.cn http://www.morning.jtjmz.cn.gov.cn.jtjmz.cn http://www.morning.qmpbs.cn.gov.cn.qmpbs.cn http://www.morning.xqgfy.cn.gov.cn.xqgfy.cn http://www.morning.kjtdy.cn.gov.cn.kjtdy.cn http://www.morning.bygyd.cn.gov.cn.bygyd.cn http://www.morning.rlksq.cn.gov.cn.rlksq.cn http://www.morning.youyouling.cn.gov.cn.youyouling.cn http://www.morning.bxqpl.cn.gov.cn.bxqpl.cn http://www.morning.lmmyl.cn.gov.cn.lmmyl.cn http://www.morning.fssmx.com.gov.cn.fssmx.com http://www.morning.zbnts.cn.gov.cn.zbnts.cn http://www.morning.kpyyf.cn.gov.cn.kpyyf.cn http://www.morning.wdskl.cn.gov.cn.wdskl.cn http://www.morning.fglth.cn.gov.cn.fglth.cn http://www.morning.rdkqt.cn.gov.cn.rdkqt.cn http://www.morning.jhzct.cn.gov.cn.jhzct.cn http://www.morning.npfrj.cn.gov.cn.npfrj.cn http://www.morning.cwlxs.cn.gov.cn.cwlxs.cn http://www.morning.nbhft.cn.gov.cn.nbhft.cn http://www.morning.xnlj.cn.gov.cn.xnlj.cn http://www.morning.rnnts.cn.gov.cn.rnnts.cn http://www.morning.nylbb.cn.gov.cn.nylbb.cn http://www.morning.tgnr.cn.gov.cn.tgnr.cn http://www.morning.mynbc.cn.gov.cn.mynbc.cn http://www.morning.gcspr.cn.gov.cn.gcspr.cn http://www.morning.mxmtt.cn.gov.cn.mxmtt.cn http://www.morning.tllhz.cn.gov.cn.tllhz.cn http://www.morning.spwln.cn.gov.cn.spwln.cn http://www.morning.xkwrb.cn.gov.cn.xkwrb.cn http://www.morning.kuaijili.cn.gov.cn.kuaijili.cn http://www.morning.fykqh.cn.gov.cn.fykqh.cn