Serialization scheme comparison

1. Background

The previous two articles introduced the use of Google ProtoBuf serialization and the principle of serialization. In fact, there are countless serialization implementation solutions. This article will compare the serialization solutions that have been used before or are familiar with, mainly comparing the generation of binary streams. The size, the original data is as follows.


"id":1,
"name":"qingcai18036",
"email":"[email protected]"

2. JDK native serialization

Let the object to be serialized implement the Serializable interface, and the serialization can be completed by the ObjectOutputStream class. When serializing, it must serialize all the metadata and business objects of the object class into a byte stream, and also keep the inheritance relationship of the class consistent. And serialized, so the serialized binary stream is relatively large.

Advantages: easy to use

Disadvantages: The binary stream after serialization is relatively large and cannot cross languages.

Note: In my impression, the initial version of the HSF framework only supports Java native serialization and Hessian serialization. When we build services and create entity classes, we need to implement the Serializable interface, otherwise the client will report an error when calling, which is not involved in single applications. Remote calling network transmission does not necessarily need to implement the Serializable interface.

3. Hessian serialization

Hessian is a remote call based on binary-RPC, using binary to transmit data. During serialization, Hessian will convert all properties into a Map and generate data like the following
className propertyName1 I intValue propertyName S stringValue
where I represents Integer and S represents String.

Advantages: More efficient than JDK native, and the generated byte stream is smaller.
Disadvantages: Some Java types are not supported.

Note: HSF1.0 seems to have started to support Hessian serialization. In addition, I have the impression that Hessian was used in the interaction between Java and PHP very early on.

4. JSON serialization

JSON serialization is a text serialization framework, and its applications are too wide, including front-end and back-end calls, remote calls based on HTTP, etc.

Advantages: Good text readability

Disadvantages: The extra space overhead for JSON serialization is relatively large. JSON needs to be converted into Java objects through reflection in Java, and the performance is not very good.

JSON serialization tools mainly include Alibaba Fastjson, Google Gson, Jackson, etc.

5. XML serialization

XML is also a text-based serialization framework. Before JSON became popular in 2005, XML was extremely widely used.

XML serialization tools include xStream, jdom, dom4j, etc.

Note: The XML applications I have come into contact with mainly include messages for docking with SF Express WMS system, messages for WeChat official account push messages, and more recently, when I was working on social security projects, the front-end Dephi and back-end Java interaction used SOAP (a XML-based Simple Object Access Protocol)

6. ProtoBuf serialization

Advantages: high efficiency, good compatibility

For specific usage and principles, see    Google ProtoBuf for an introduction to   efficient ProtoBuf.

7. Thrift serialization

It is an RPC framework developed by Facebook. It also has its own serialization solution, which seems to be more troublesome to use than ProtoBuf.

I have never used this. A friend wrote an article. If you are interested, you can learn more about it.

 Thanks Thrift! NodeJS and Python can happily communicate

Advantages: efficient

8. FAST protocol

The FAST protocol is used in stock quotes and achieves ultimate compression.

The data to be transmitted in the stock market is mainly date, time, opening price, highest price, lowest price, and current price. In this way, it can be agreed that 1->date 2->time 3->opening price 4->highest price 5->lowest price 6->Current price

Sample data is as follows

1=20190310 , 2=142900 , 3=13.4 , 4=15.0 ,5=13.0 ,6=13.5

9. Comparison of binary stream size after serialization

XML does not support xStream on JDK16, so there is no data.

Guess you like

Origin blog.csdn.net/2301_76787421/article/details/132938737