purpose of serialization
- cross-platform storage
- network transmission
way of serialization
jdk native
Serialization and deserialization
@Test
public void test01() throws Exception {
//序列化
FileOutputStream fileOutputStream = new FileOutputStream("D:\\IDEA_file\\Learn_8\\src\\file\\person.dat");
ObjectOutputStream objectOutputStream = new ObjectOutputStream(fileOutputStream);
objectOutputStream.writeBoolean(true);//boolean -> Boolean;
Person person = new Person(10, "小明");
//序列化对象
objectOutputStream.writeObject(person);
//关闭外层流
objectOutputStream.close();
}
@Test
public void test02() throws Exception {
//反序列化
FileInputStream fileInputStream = new FileInputStream("D:\\IDEA_file\\Learn_8\\src\\file\\person.dat");
ObjectInputStream objectInputStream = new ObjectInputStream(fileInputStream);
boolean b = objectInputStream.readBoolean();
System.out.println(b);
Object o = objectInputStream.readObject();
System.out.println(o);
//向下转型
Person p=(Person) o;
System.out.println(p.toString());
objectInputStream.close();
}
Require
The serialized class implements the Serializable interface
For example:
public final class String
implements java.io.Serializable
public class Person implements Serializable
serialVersionUID
Function:
mark the version of a class, if we do not specify the version number, whenever we modify or add a field or method of a class, jdk will automatically generate an id number based on parameters such as attributes and methods
If the id number during deserialization is inconsistent with the id number during serialization, deserialization will fail
java.io.InvalidClassException: file.Person; local class incompatible: stream classdesc serialVersionUID = 7330609361009257993, local class serialVersionUID = -3573391668384454971
Therefore, we generally specify the version number ourselves, and this field must be static, final and type:
ANY-ACCESS-MODIFIER static final long serialVersionUID = 42L;
cannot be inherited
public class Person implements Serializable {
private static final long serialVersionUID = 1L;
}
- When compatible upgrade, do not modify the version number
- When incompatible upgrades, modify the version number
[!Note] tip
avoid being serialized
static
static
Because variables do not belong to any object (Object), they transient
will not be serialized no matter whether they are modified with keywords or not.
transient
If some fields do not want to be serialized,
For variables that do not want to be serialized, use transient
keyword decoration.
transient
The role of the keyword is to prevent serialization of variables modified with this keyword in the instance; when the object is deserialized, the transient
value of the modified variable will not be persisted and restored.
A few more notes about transient
:
transient
Only variables can be modified, classes and methods cannot be modified.transient
Modified variables, after deserialization, the variable value will be set to the default value of the type. For example, if it is a modifiedint
type, then the result after deserialization is0
.
public class Person implements Serializable {
private static final long serialVersionUID = 1L;
Integer age;
String name;
static Integer total=10;
transient Integer tall=178;
}
json
fastjson
@Test
public void test1(){
User user = new User("张三",1);
System.out.println(user);
System.out.println("-----fastjson-----");
// 将Java对象转换为json字符串
String jsonString=JSON.toJSONString(user);
System.out.println(jsonString);
// 将json反序列化为Java对象
User user1 = JSON.parseObject(jsonString,User.class);
System.out.println(user1);
}
package com.gdb.mvc.controller;
import java.io.Serializable;
/**
* @Author gao
* @Description TODO
* @Date 2023/6/11 15:52
* @Version 1.0
*/public class User {
String name;
Integer age;
@Override
public String toString() {
return "User{" +
"name='" + name + '\'' +
", age=" + age +
'}';
}
public User(String name, Integer age) {
this.name = name;
this.age = age;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public Integer getAge() {
return age;
}
public void setAge(Integer age) {
this.age = age;
}
}