Java Basic serialization

Source: Internet
Author: User

Reference Documentation:
Serialization and deserialization: http://kb.cnblogs.com/page/515982/
Serialization api:http://blog.csdn.net/abc6368765/article/details/51365838 in the JDK
How to serialize in JDK: http://www.cnblogs.com/redcreen/articles/1955307.html

What is serialization & why to Serialize
Serialization (serialization) transforms the state information of an object into a process that can be stored or transmitted in a form. During serialization, an object writes its current state to a temporary or persistent store. Later, the object can be recreated by reading or deserializing the state of the object from the store

Mainstream serialization framework

Following the serialized data format, the mainstream serialization framework can be divided into four main categories:
JSON class

Very popular, Jackson.
Google's Gson. Gson is currently the most fully functional JSON parsing artifact , without Reliance, running directly on the JDK. Performance less than Fastjson
Messagepack of class JSON
Ali's Fastjson. Performance is the best. No dependencies, no extra jars for exceptions, can run directly on the JDK

Binary class

Veteran powerhouse Hessian (formerly used to like it)
Full-featured and powerful FST
Rising Star Kryo

XML class

StAX (streaming API for XML)
Thoughwork's XStream

RPC Classes (both installed and compiled)

Protobuf
Thrift
Apache Avro

Judging the merits of a coding framework is mainly from the following aspects

    1. Support for cross-language support for rich languages
    2. Encoded Bitstream storage space
    3. Encoding and decoding performance
    4. Whether the class library is small and the API is easy to use

Disadvantages of Java serialization

    1. Cannot cross language. This should be the most deadly problem with Java serialization. Because Java serialization is a privately owned protocol within Java, other languages are not supported, causing other languages to be deserialized, which seriously hinders its application
    2. The code stream after the sequence is too large. Java serialization is 5 times times larger than binary encoding
    3. Serialization performance is too low. The performance of Java serialization is only 6.17 times times the binary encoding

Serialization APIs in the JDK class library

Implementation of serialization

Only objects of classes that implement the Serializable and externalizable interfaces can be serialized. The Externalizable interface inherits from the serializable interface, and the class that implements the Externalizable interface controls the serialization behavior entirely by itself, whereas classes that implement only serializable interfaces can be serialized in the default way
ObjectOutputStream represents an object output stream, and its writeobject (object obj) method serializes the Obj object specified by the parameter and writes the resulting sequence of bytes to a target output stream
ObjectInputStream represents an object input stream, its readobject () method reads a sequence of bytes from a source input stream, deserializes them into an object, and returns

The role of Serialversionuid

S?e?r?i?a?l? V?e?r?s?i?o? n? U? I? D?:????????????, all the classes that implement the Serializable interface have a static variable that represents the serialized version identifier.
There are two ways to build Serialversionuid:

Serialversionuid is 1L
Private static final long serialversionuid = 1L;
On a compatible basis, you can keep the old version number, if it is incompatible, or if you want it to be incompatible, increment the version number manually.

Serialversionuid is the hash value generated by the class name, interface name, method and property, and so on:
Private static final long serialversionuid = 4603642343377807741L;
Is the hash value generated by the structure of the class. Adding or subtracting a property, method, etc. can cause this value to change. I think this approach applies to scenarios where developers think that each time a class is modified, a new version number needs to be generated and not backwards compatible, and the operation is to delete the original SERIALVESIONUID declaration statement, and then automatically generate

Example:
The user class is serialized into Kafka, and if you do not specify Serialversionuid, the user class adds a new age property that, when deserialized, throws an exception and refuses to load. Because the user class was changed, a new serialversionuid was generated, inconsistent with the serialized version of Serialversionuid stored in Kafka. So the deserialization conflicts

Serialization of static variables

Serialization holds the state of an object, static variables belong to the state of the class, so serialization does not save static variables

The serialization of the parent class

After the parent class implements the interface, the properties of all derived classes are serialized. Child class implements the interface, the parent class's property value is missing

Transient keywords

The function of the transient keyword is to control the sequence of variables, add the keyword before the variable declaration, you can prevent the variable from being serialized into the file, after being deserialized, the value of the transient variable is set to the initial value, such as the int type is 0, the object type is null

Java Basic serialization

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.