当前位置:网站首页>serialization and deserialization
serialization and deserialization
2022-07-23 18:16:00 【abs(ln(1+NaN))】
In the process of network communication , We may want to pass more than just a string , We may need to pass on more complex structures , For example, structure type , In this case, we need to use serialization and deserialization .
Suppose Zhang San and Li Si are using QQ Chat , Zhang San sent a message to Li Si “ Hello , I am Zhang San. ”, This is the time , It looks like only one string was sent , actually , Zhang San's nickname 、 The time to send messages should be sent to Li Si's host .

Catalog
One 、 What is serialization and deserialization
2、 Realize the decoupling of application layer display and network transmission
3、 ... and 、 How to serialize and deserialize ?
2、 serialize ( Structure Turn it into json character string )
(1) preparation : Declared structure
3、 Deserialization (json character string Turn it into Structure )
One 、 What is serialization and deserialization
Because the structure is not convenient for transmission , Generally, you need to convert the structure into a “ Long string ”, Then send it to the network , This process is called serialization .
Corresponding , When Li Si was receiving , You need to convert this long string into a form that the other party can recognize , Like a structure , This process is called deserialization .

Two 、 Why do I need serialization and deserialization ( Advantages of serialization and deserialization )
1、 Convenient transmission
Between the principles of structure alignment , Structured data is not convenient for network transmission
Maybe you think , Since the whole structure cannot be transmitted , Then pass each member of the structure in the form of string one by one , This is theoretically feasible , But one host may receive data from multiple hosts , This is the time , It is difficult to distinguish which host sent the data .
2、 Realize the decoupling of application layer display and network transmission
Suppose Zhang San sends a string "10 + 20", Let Li Si calculate , If not serialized , But if it is sent directly , Delivery is really convenient , But from the perspective of Li Si , The cost of parsing is too high .

If we convert a structure into a string ( Namely serialization ), When Li Si received it , Then convert back to the structure , In this case , Li Si doesn't need to know how this string is parsed . Because the things in Li Si's and Zhang San's hands are the same , Below as long as Make an agreement , Let Li Si know how to use the passed thing . This realizes the decoupling of network transmission and upper display !!

3、 ... and 、 How to serialize and deserialize ?
To achieve serialization and deserialization , Predecessors have already had corresponding solutions , such as xml、json、protocbuff etc. , Here we mainly introduce how to convert the structure type into json Format , as well as json Transfer structure type .
1、 install jsoncpp-devel
According to different user types , Choose the corresponding installation method by yourself
sudo yum install -y jsoncpp-devel // Ordinary users
yum install -y jsoncpp-devel // The super user After successful installation, enter the following instructions to check whether there is a corresponding header file

2、 serialize ( Structure Turn it into json character string )
(1) preparation : Declared structure
Suppose you want to transfer a structure to the opposite , Let's declare a simple structure first
// Sent to the network , As a request
typedef struct Request{
int x;
char op;
char* msg;
} request_t;(2) Start serializing
To understand the process , I take the animation we are familiar with as an example . SpongeBob has the raw materials ready root, Fry on an iron plate , After some operation , I got a hamburger .

#include <jsoncpp/json/json.h>
#include <iostream>
using namespace std;
int main(){
request_t req = {10,'+',"Hello,world"}; // Initialize structure
Json::Value root; // raw material root
root["datax"] = req.x; // Put it in the designated position on the iron plate
root["dataop"] = req.op;
root["datamsg"] = req.msg;
Json::FastWriter writer;
//Json::StyledWriter writer; // Both of them can be written , As for the difference , You can see the printed results
std::string json_str = writer.write(root); // The hamburger is finished
return 0;
}The test results are as follows :
======================= StyledWriter =======================

======================= FastWriter =======================

3、 Deserialization (json character string Turn it into Structure )
Now the boss stole it Crab boss's exclusive hamburger , Boss ruffian used a high-tech machine to decompose this hamburger , Will be decomposed into the state of raw materials , Put it on a tray , In order to observe what materials are used to make hamburgers .

// The middle quotation mark needs to be used '\' escape
std::string json_str = "{\"datamsg\":\"Hello,world\",\"dataop\":43,\"datax\":10}";
Json::Reader reader;
Json::Value root;
reader.parse(json_str,root); // If converted to root It is not convenient to pass parameters in the future , Fill in req in
request_t req;
req.x = root["datax"].asInt(); //root Each of them key Corresponding value All are Json::Value type
req.op = root["dataop"].asInt();
req.msg = root["datamsg"].asString().c_str();
cout<<req.x<<" "<<req.op<<" "<<req.msg<<endl;The test results are as follows :

边栏推荐
- Problems encountered in the project and Solutions
- DDD:如何领用领域驱动设计来避免写流水账代码
- MPU9250传感器
- go语言中的内存对齐是如何优化程序效率的?
- 开源需要专业化
- Installation and configuration tutorial of mingw-w64
- What about the reason why GOM and GEE set up a black screen and the fact that individual equipment maps are not displayed?
- MySQL uses commands to export and import in Windows
- JS: image URL to Base64 encoding
- js:图片url转base64编码
猜你喜欢

阿里 P7 到底是怎样的水平?

【JZOF】13机器人的运动范围
![[jzoof] 13 plage de mouvement du robot](/img/c3/56ae78f19578ff8ad8d9b824b7d99f.png)
[jzoof] 13 plage de mouvement du robot
![Leetcode: Jianzhi offer II 115. reconstruction sequence [graph theory thinking + in degree consideration + topological sorting]](/img/cc/1438381ccc8d7fe147bb9c5e7e1742.png)
Leetcode: Jianzhi offer II 115. reconstruction sequence [graph theory thinking + in degree consideration + topological sorting]

Explanation of PPPoE protocol and analysis of Wireshark packet capturing during dialing

网分测花岗岩介电常数测试方案

Role definition in USB type-C PD CC logic chip

c语言--通讯录的实现与ScreenToGif

【JZOF】13機器人的運動範圍

How the lock issued by go works (combined with the source code)
随机推荐
分组加密模式 ECB、CBC、PCBC、CFB、OFB、CTR
MySQL经典练习题及答案,常用SQL语句练习50题
JVM – 彻底理解打破双亲委派机制
Use of computed in projects
MySQL uses commands to export and import in Windows
Redis data loss problem
Chi square distribution, analysis of variance
go中高并发下的通信方式:channel管道的底层原理
"Nowadays, more than 99.9% of the code is garbage!"
MySQL 66 questions, 20000 words + 50 pictures, including (answer analysis)
c语言--通讯录的实现与ScreenToGif
Sentinel introduction and microservice integration
go语言中的内存对齐是如何优化程序效率的?
[215] gin framework connects to MySQL database
悲观锁和乐观锁
MySQL 7 kinds of join (Figure)
接口测试概述
Role definition in USB type-C PD CC logic chip
The Little Schemer-周而复始之Y组合子由来
Use of keyup.native in El input