Serialization and deserialization of Java objects

The goal of object serialization is to save the object on disk or transfer it over the network. The implementation mechanism is to allow objects to be converted into platform independent binary streams.

The serialization mechanism of objects in Java is to convert allowed objects into byte sequences. These byte sequences can separate Java objects from programs, so they can be saved on disk or transmitted between networks.

Object serialization is to write a Java object into the IO stream; Correspondingly, deserialization is to recover a Java object from the IO stream.

Implement serialization

If you want to serialize a Java object, the class of the object needs to be serializable. To make the class serializable, the class needs to implement the following two interfaces:

Serializable serialization

Implementing the serializable interface is very simple. Just let Java implement the serializable interface without implementing any methods.

Once a class implements the serializable interface, the object of the class is serializable. Objectoutputstream can be used to serialize the objects of the class. The implementation steps are as follows:

Here is an example:

The above code realizes that a person object is saved in a text file object Txt. The running program generates an object on disk D Txt file. The following are the contents of the document:

There is garbled code (caused by byte flow and character flow), but it still does not affect us to distinguish whether it is the object we save.

Next, you need to deserialize and read the person object from disk. The corresponding class to be used for deserialization is objectinputstream. The deserialization steps are as follows:

Next, reconstruct our code to realize deserialization, as follows:

There are several points to note about object serialization and deserialization:

Using transient

In some special scenarios, such as bank account objects, for confidentiality reasons, you do not want to serialize the deposit amount. Or members of some reference types of the class are not serializable. In this case, the transient keyword can be used to modify member variables that do not want to be serialized or cannot be serialized.

Continue to adjust our code for demonstration:

If the school value is not null, notserializableexception will be reported without adding the transient ID to the school member of the teacher class. The exception information is as follows:

When the transient ID is not added to the salary member of the teacher class, the salary value will be output truthfully. After adding, only the default initial value of salary, that is, 0.0, will be output.

It should be noted that transient can only modify attributes (files), not classes or methods.

Custom Serialization

Transient provides a concise way to completely isolate the member attributes modified by transient from the serialization mechanism. This is good, but Java also provides a custom serialization mechanism to let developers have more freedom to control how to serialize individual member attributes or not to serialize some attributes (the same effect as transient).

In the classes that need custom serialization and deserialization, the following methods need to be provided:

First, let's talk about the first two methods, writeobject and readObject. These two methods have the same names as the corresponding methods in objectoutputstream and objectinputstream. In fact, although these two methods are private, However, it is still called by the external class objectoutputstream (or objectinputstream) in the serialization (or deserialization) stage. Just take serialization as an example. Objectoutputstream will be reflected in the class of the object to be serialized before executing its writeobject method (a little tongue twisty, isn't it) find out whether there is a custom writeobject method. If there is, the custom writeobject method will be called first. Because getprivatemethod is used to find the reflection method, the scope of the custom writeobject method should be set to private. The serialization and of objects can be completely controlled through the custom writeobject and readObject methods Deserialization.

Here is the sample code:

The following are the output results:

About readobjectnodata, I found the following description on the Internet:

Readobjectnodata seems to me like an exception handling mechanism to return the correct value when the serialized stream is incomplete.

Using writereplace and readresolve

Writereplace and readresolve are more thorough serialization mechanisms, which can even replace the serialized target object with other objects.

However, unlike writeobject and readObject, they do not have to be used together and should be used separately as far as possible. If used together, only writereplace will take effect.

The code can explain everything. First, writereplace:

The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>