Summary of common serialization protocols

Source: Internet
Author: User

Recently made a terminal 5.0 platform, which uses a number of serialized protocols, such as XML,JSON,PROTOBUF, some places to use the feeling pretty good, some places use some inappropriate, and found that many open source is widely used in the serialization protocol, this is a basic prerequisite skills, so I would like to summarize, for the future architecture set The choice to do bedding;

What is a serialization protocol? the serialization protocol is the standard definition of the data carrier, so that the loaded data can be effectively lossless transformed in different languages and specific data on different platforms, and the protocol itself is independent of platform and language; The common serialization protocols are XML, JSON, PROTOBUF; Although there is a lot of such information on the Internet, I also recommend XML Learning--http://www.w3school.com.cn/xml/;json learning--http://baike.baidu.com/link?url= Rpylygvvlzavxpqv64epe33wmnvrpsvzv-apedkbnfafeylusz1p02pprm-oa5b1sbt1fx2h2ut1ie8lyp-7d_, http://www.bejson.com/ ; protobuf study--http://www.ibm.com/developerworks/cn/linux/l-cn-gpb/, http://www.searchtb.com/2012/09/ protocol-buffers.html;

Why use a serialization protocol? because today's application is to use different languages cross-platform communication across the network, so how to make the data interaction in the process of non-destructive and effective interaction is a factor that must be considered, and the serialization protocol is to solve this problem is born;

What factors should we consider when choosing a serialization protocol? Although the serialization protocol has its merits and demerits, the following points are generally considered:

<1> readability, such as XML, JSON readability is good, protobuf serialized data is not readable;

<2> space occupancy; For example, PROTOBUF serialization is the Java serialization of the 1/9;json format is Java serialization 1/2.6;xml compared to Java serialization, space occupancy has a little advantage, but not obvious;

<3> serialization and the response time of deserialization, such as Protobuf one operation at about 1us, one order of magnitude faster than the Java serialization time; The JSON format operates around 5us, about the same time as the Java serialization, slightly faster ; The XML format operates at around 100us, one order of magnitude slower than the Java serialization time;

<4> the complexity of expressing data; I think XML is more expressive of complex data than JSON and PROTOBUF, because XML also has the concept of attributes;

<5> language support; Protobuf native support for--java, Python, C++;json, and XML are basically supported, because it is also relatively simple;

<6> Extensibility; I think the protobuf version is better than JSON and XML, because there is a special required, optional for version forward compatibility;

Specific parameters comparison reference: http://blog.csdn.net/kongls08/article/details/8562280;

These serialization protocols are summarized by the open Source Engine:

<1>protobuf--google Protocol Buffer; Download Path http://code.google.com/p/protobuf/downloads/list

<2>json--c++ language use jsoncpp; Download path http://www.oschina.net/p/jsoncpp

The Java language uses Jackson; Download path Http://www.oschina.net/p/jackson

C language use Jansson; Download path Http://www.oschina.net/p/jansson

(Note: Janson Some interfaces use the resulting memory to be released by the user, otherwise there will be a memory leak)

The rest can be referred to Http://www.oschina.net/project/tag/228/json

<3>xml--c/c++ language use tinyxml; Download path http://www.oschina.net/p/tinyxml

Java language use xml-utilities; Download path http://www.oschina.net/p/xml-utilities

Usage Scenarios:

The advantage of <1>PROTOBUF--PROTOBUF is that the space is small, the serialization deserialization is fast, the disadvantage is that the readability is not good, and the less data communication between networks, the better, the faster the response is better; ; So I think it's more appropriate to use it for cross-network communication;

The advantage of <2>json--json is that the readability is slightly worse than the XML PROTOBUF, the space and the speed is worse than the protobuf, and it is better than XML, while the communication between the LAN and the same machine is not particularly high in performance requirements, it is necessary for readability, so it can be debugged conveniently. ; So my task is suitable for communication between LAN and different systems of the same machine, or cross-language interface data encapsulation;

The advantage of <3>xml--xml is that it is very readable, very fit for human thought, but inefficient, while the stored data is more readable and inefficient, because it is not often read and written; so I think XML is suitable for data storage;

does communication between the internal modules of the system require data to be loaded using a serialization protocol? in our 5.0 terminal design, communication between modules is also selected PROTOBUF communication, the internal data of the system is defined by PROTOBUF; The trouble is that the definition of the data structure is cumbersome, which increases the cost of the conversion, and the benefits are not found yet, but In order to prevent the extension of subsequent data structures; I personally say this is unreasonable, because this obviously brings trouble, and the prevention of subsequent data expansion in the system internal module requirements are not high, through reasonable design data structure can be satisfied; but we also found that some of the open source data formats are assembled using serialization protocols, For example, XMLRPC uses XML to encapsulate the data, and uses the HTTP protocol as the delivery mechanism for remote invocation; Because this is the base component, it is possible to make remote calls across languages and across platforms;

So when we're using the serialization protocol, here are a few things to consider:

<1> whether it is possible to cross-language;

<2> whether it is possible to cross-platform;

The final summary : Do not use the serialization protocol everywhere to load data, according to the situation to choose the appropriate serialization protocol;

Summary of common serialization protocols

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.