The role of serialization

Source: Internet
Author: User

Objective:

This article takes the C + + language as the function of explaining serialization.


What is serialization?

Generally speaking of serialization, it is actually included in deserialization.

In C + +, for example, serialization is the orderly preservation of various properties of complex data structures, such as structs (or classes), into character arrays. Deserialization, however, is the restoration of an ordered array of characters back to complex data structures such as structs (or classes).


Serialization action

1, convenient network transmission

As we all know, the data of the socket is transmitted as a string, and the function of serialization is to convert complex data structures into strings.

2, facilitate the interpretation of the agreement

"Order" in serialization is ordered, and ordered string sequences can be interpreted by most programming languages. and protocol buffers is one of the prominent representatives.

3. Convenient data storage

Convenient data storage This is a good understanding, is to solve one-to-many problems. Without serialization, it may be necessary to store multiple messages, while serialization can be used to synthesize one piece of data for storage, and to facilitate data operation.


Serialization is also a double-edged sword, the advantages are obvious, the shortcomings are equally obvious!

Lack of serialization

1. Performance

This is not to say that serialization is inefficient, but rather human-to-serialization efficiency control. For example, in Windows (VS2013), when we assign values to structs, when struct bytes are less than or equal to 4096, the compiler assigns one by one values to each value of the struct (the value here is not a true structure value, 4 bytes relative to the assembly), and when the struct byte is greater than 4096, The compiler will call memcpy directly. This is because the efficiency of the one by one assignment decreases significantly as the number of bytes increases.

2, storage data is not easy to modify

Suppose the database now holds a serialized string of a, B, and C three values. But now that demand has changed, you need to add a D value. That's a problem, because the compatibility of old data is now to be considered.


For performance issues, you can only strengthen your capabilities to ensure maximum performance. For storing data, my suggestion is to add a version number that is compatible with the different versions of this version number.

I have put my own serialization code into Github:https://github.com/xjm2013/serializer interested to see. The test code and test results are posted below.

#ifndef test_serializer_h#define test_serializer_h#include <vector> #include "serializer.h" namespace Testserializer{class Testdata{public:testdata () {};~testdata () {};boolserialize (serializer &stream) const; Boolunserialize (serializer &stream); struct datastruct{int a;short b;char c;char d;long long e;float f;double g;};D Atastruct m_data_struct;typedef std::vector<int> vector_test;typedef std::vector<std::string> VECTOR_ NAME; Vector_test Role_data; Vector_name Role_name;}; BOOL Testdata::serialize (Serializer &stream) Const{stream. Push (m_data_struct); stream. Push (Role_data.size ()); for (Vector_test::const_iterator ITR = Role_data.begin (); ITR! = Role_data.end (); ++itr) {stream . Push ((*ITR));} Stream. Push (Role_name.size ()); for (Vector_name::const_iterator ITR = Role_name.begin (); ITR! = Role_name.end (); ++itr) {stream . Pushstr (Itr->c_str (), itr->length () + 1);} return true;} BOOL Testdata::unserialize (serializer &stream) {stream. Pop (m_data_struct); unsigned int size= 0;stream. Pop (size), int data = 0;for (unsigned int i = 0; i < size; ++i) {stream. Pop (data); Role_data.push_back (data);} Stream. Pop (size), char *str = 0;unsigned int length = 0;bool ret = false;for (unsigned int i = 0; i < size; ++i) {ret = STREAM.P Opstr (&str, length); if (!ret) {return ret;} Role_name.push_back (str);} return true;} void Normal () {TestData src_data; TestData des_data;src_data.m_data_struct.a = 11;src_data.m_data_struct.b = 22;src_data.m_data_struct.c = 33;src_ DATA.M_DATA_STRUCT.D = 44;SRC_DATA.M_DATA_STRUCT.E = 55;src_data.m_data_struct.f = (float) 66.6;src_data.m_data_ STRUCT.G = -77.7;src_data.role_data.push_back (src_data.role_data.push_back); Src_data.role_name.push_back ( "Xian"); Src_data.role_name.push_back ("Jia"); Src_data.role_name.push_back ("Ming");//Src_data and Des_data would be The Samechar data[1024] = {0}; Serializer Temp1 (data, 1024x768); Src_data. Serialize (TEMP1); Serializer Temp2 (data, 1024x768);d Es_data. Unserialize (TEMP2);}} #endif

Breakpoint View Results:




The role of 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.