当前位置:网站首页>序列化和反序列化
序列化和反序列化
2022-07-23 15:51:00 【abs(ln(1+NaN))】
在网络通信的过程中,我们要传递的可能不仅仅只是一个字符串,我们可能需要传更为复杂的结构,比如结构体类型,这种时候我们要用到序列化和反序列化。
假设张三和李四在用QQ聊天,张三给李四发了条消息“你好,我是张三”,这个时候,看起来像是只发了一个字符串,实际上,张三的昵称、发送消息的时间都要发送到李四的主机上。

目录
一、什么是序列化和反序列化
因为结构体不便于传输,一般需要将结构体转化为一个“长的字符串”,然后再送入网络,这个过程我们称为序列化。
相对应的,李四那边在接收的时候,需要将这个长字符串转化为对方能够识别的形式,如结构体,这个过程我们称为反序列化。

二、为什么需要序列化和反序列化(序列化和反序列化的优点)
1、方便传输
介于结构体对齐原则,结构化的数据不便于用于网络传输
或许你觉得,既然整个结构体传输不行,那就将结构体的每个成员以字符串的形式一个一个传过去,这么做理论上可行,但是一台主机可能会收到多个主机发来的数据,这个时候,就很难区分是哪个主机发的数据。
2、实现应用层显示和网络传输的解耦
假设张三发送一个字符串 "10 + 20",让李四去计算,如果不经过序列化,而是直接发送的话,传递确实很方便,但是站在李四的角度,解析的成本可太高了。

如果我们将一个结构体转化为字符串(即序列化),李四接收的时候,再转化回结构体,这样的话,李四无需知道这个字符串是如何解析的。因为李四和张三手里的东西是一样的,下面只要定好协议,让李四知道传过来的东西怎么使用即可。这就实现了网络传输和上层显示的解耦!!

三、如何序列化和反序列化?
为了实现序列化和反序列化,前人已经有了相应的处理方案,比如xml、json、protocbuff等,这里主要介绍将结构体类型转化为json格式,以及json转结构体类型。
1、安装jsoncpp-devel
根据不同的用户类型,自行选择对应的安装方式
sudo yum install -y jsoncpp-devel //普通用户
yum install -y jsoncpp-devel //超级用户安装成功输入如下指令查看是否有对应的头文件

2、序列化(结构体 转化成 json字符串)
(1) 准备工作:声明结构体
假设现在要将一个结构体传输给对面,下面就先声明一个简单的结构体
//送入网络的,我们作为请求
typedef struct Request{
int x;
char op;
char* msg;
} request_t;(2) 开始序列化
为了理解这个过程,我以我们熟知的动画为例。海绵宝宝准备好了原材料root,放到铁板上煎,经过一些操作,就得到了一个汉堡。

#include <jsoncpp/json/json.h>
#include <iostream>
using namespace std;
int main(){
request_t req = {10,'+',"Hello,world"}; //初始化结构体
Json::Value root; //原材料root
root["datax"] = req.x; //放到铁板上的指定位置
root["dataop"] = req.op;
root["datamsg"] = req.msg;
Json::FastWriter writer;
//Json::StyledWriter writer; //这两种写法都可以,至于区别,可以看看打印的结果
std::string json_str = writer.write(root); //汉堡制作完成
return 0;
}测试结果如下:
======================= StyledWriter =======================

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

3、反序列化(json字符串 转化成 结构体)
现在痞老板偷到了 蟹老板的独家汉堡,痞老板用高科技的机器分解了这个汉堡,将分解成了原材料的状态,放在托盘上,以便于观察到底用了哪些材料来制作汉堡。

//中间的引号需要使用'\'转义
std::string json_str = "{\"datamsg\":\"Hello,world\",\"dataop\":43,\"datax\":10}";
Json::Reader reader;
Json::Value root;
reader.parse(json_str,root); //若转化为root以后不便于参数传递,下面填到req里
request_t req;
req.x = root["datax"].asInt(); //root中每个key对应的value都是Json::Value类型
req.op = root["dataop"].asInt();
req.msg = root["datamsg"].asString().c_str();
cout<<req.x<<" "<<req.op<<" "<<req.msg<<endl;测试结果如下:

边栏推荐
- [215] Gin Framework connection to MySQL Database
- 【216】go语言标准库包名
- Seal player IP and machine code and unlock the blocked tutorial
- jps详解
- 本周一问 | Vivado 综合阶段什么约束生效?
- Installation and configuration tutorial of mingw-w64
- Leetcode: Jianzhi offer II 115. reconstruction sequence [graph theory thinking + in degree consideration + topological sorting]
- MySQL8.0.23四次重装都失败在 'Writing configuration file'
- How does memory alignment in go optimize program efficiency?
- MySQL master-slave synchronization delay solution
猜你喜欢

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

Multithreaded programming

Role definition in USB type-C PD CC logic chip

Seal player IP and machine code and unlock the blocked tutorial

Machine learning (9) - Feature Engineering (3) (supplementary)

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

go中的協程原理詳解

变分法 (Calculus of Variations)
![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]

“如今,99.9% 以上的代码都是垃圾!”
随机推荐
Open source needs specialization
Installation and configuration tutorial of mingw-w64
As a background developer, you must know two kinds of filters
DB9串口和RJ45串口
开源需要专业化
ctf MISC 学习总结「建议收藏」
为什么香港服务器可以免备案
ContextLoaderListener vs DispatcherServlet
Keras II classification problem
Did not find the necessary patch file 'newopui.pak'?
Li Hongyi, machine learning 7. Conclusion (summary)
On Monday, I asked | what constraints will take effect in the comprehensive stage of vivado?
go中的协程原理详解
Eric6 installation problem
Static distribution and dynamic distribution in trust
Redis data loss problem
go中的協程原理詳解
jps详解
Dyn keyword in rust
Common operations of idea debug