当前位置:网站首页>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 :

边栏推荐
- 封玩家IP和机器码以及解开被封的教程
- rhcsa笔记五
- Une erreur s'est produite lors de la configuration du login du moteur Gom: aucun correctif requis n'a été trouvé!
- (十一)STM32——IO引脚复用与映射
- [216] go language standard library package name
- MySQL master-slave synchronization delay solution
- 【215】gin框架连接mysql数据库
- 强化学习---马尔可夫决策过程 MP MRP MDP
- [215] Gin Framework connection to MySQL Database
- 信息论 (Information Theory): Introduction and information measures
猜你喜欢

go中高并发下的锁是如何工作的(结合源码)

配置Gom引擎登錄器出現錯誤提示:沒有發現必備補丁文件!

MySQL8.0.23四次重装都失败在 'Writing configuration file'

@Entity 里面的 JPA 注解

Go medium high parallel communication mode: the underlying principle of channel pipeline

Simply understand why the first EVM equivalent zkevm polygon is fully betting

(十一)STM32——IO引脚复用与映射

Lin Zhiying is still in the intensive care unit and will undergo a second round of surgery: the police said he did not wear a seat belt

Sentinel installation diagram

【JZOF】13机器人的运动范围
随机推荐
DB9串口和RJ45串口
Docker installs redis and starts it as a configuration file
Keras II classification problem
Test and development experience
几种运维工具的对比
【215】gin框架连接mysql数据库
Eric6 installation problem
Common operations of idea debug
【215】gin框架連接mysql數據庫
(十一)STM32——IO引脚复用与映射
Machine learning (9) - Feature Engineering (3) (supplementary)
Three barriers in the workplace: annual salary of 300000, 500000 and 1million
Major optimization of openim - Message loading on demand, consistent cache, uniapp Publishing
Mutual certification of product compatibility between tapdata and Youxuan database
物联网之Zigbee系统开发一(zigbee简介)[通俗易懂]
[jzof] 13 motion range of robot
Leetcode: Jianzhi offer II 115. reconstruction sequence [graph theory thinking + in degree consideration + topological sorting]
[215] Gin Framework connection to MySQL Database
How the lock issued by go works (combined with the source code)
jps详解