serialization

Serialization is the process of converting an object’s state information into a form that can be stored or transmitted. During serialization, an object writes its current state to a temporary or persistent store. Later, you can recreate the object by reading or deserializing its state from the store.

Java serialization

Serialization and deserialization are built into the Java language and are implemented through the Serializable interface.

Serializable

public class Account implements Serializable {
	private String name;
    private String pwd;
}
Copy the code

Sequence and deserialization

public class TestSerializable {

    public static void serialize(Account account, String fileName) throws IOException {
        // Save the Account object to a file
        FileOutputStream fos = new FileOutputStream(fileName);
        ObjectOutputStream oos = new ObjectOutputStream(fos);
        oos.writeObject(account);
        oos.flush();
        oos.close();
        fos.close();
    }

    public static Account deserialization(String file) throws IOException, ClassNotFoundException {
        // Read the contents of the Account
        FileInputStream fis = new FileInputStream(file);
        ObjectInputStream ois = new ObjectInputStream(fis);
        Account account = (Account) ois.readObject();
        ois.close();
        fis.close();
        return account;
    }

    public static void main(String[] args) {
        Account account = new Account();
        account.setName("Zhang");
        String fileName = "a";
        / / the serialization
        try {
            serialize(account,fileName);
        } catch (IOException e) {
            e.printStackTrace();
        }
        // deserialize
        try {
            System.out.println(deserialization(fileName).getName());
        } catch(IOException | ClassNotFoundException e) { e.printStackTrace(); }}}Copy the code

Serialization compatibility

Serialization compatibility refers to the impact of structural changes to the object (such as adding, deleting, modifying attributes, modifying attribute modifiers, etc.) on serialization. To be able to recognize changes to an object’s structure, Serializable uses the serialVersionUID field to identify the object’s structure. By default, it is automatically generated based on the object’s data structure, and its value changes when the structure changes. The virtual machine checks the value of serialVersionUID during deserialization. If the serialVersionUID in the bytecode is not the same as the serialVersionUID of the type to be converted, deserialization cannot take place properly. For better compatibility, it is best to set the value of serialVersionUID to a fixed value at serialization time.

public class Account implements Serializable {
	private static final long serialVersionUID = 1L;
	private String name;
    private String pwd;
}
Copy the code

Which scenarios need to be serialized

Generally speaking, if your object requires network transfer or persistence (object directly converted to byte transfer), you should implement the Serializable interface whenever your object requires byte transfer. For example, if you use Dubbo to call the interface using RPC, the interface parameters must implement the Serializable interface.

If you’re just dealing with the network as a string, you don’t need to implement the Serializable interface.