当前位置:网站首页>使用 protobuf 进行数据序列化
使用 protobuf 进行数据序列化
2022-07-30 14:17:00 【litanyuan】
背景
protobuf 是一种跨平台的序列化结构数据的方法,可用于网络数据传输及存储。
二进制数据读写
①.概述
protobuf 一般用于结构数据的序列化后进行网络传输。
②.数据序列化
使用成员函数 SerializeAsString 进行序列化操作,使用 string 类型保存返回的二进制数据。
protoTest::DemoMsg msg;
msg.set_id(1);
msg.set_name("Zhangsan");
string s = msg.SerializeAsString();
③.数据反序列化
使用成员函数 ParseFromString 进行反序列化操作。
protoTest::DemoMsg msg;
msg.set_id(1);
msg.set_name("Zhangsan");
string s = msg.SerializeAsString();
msg.Clear();
msg.ParseFromString(s);
cout << "id " << msg.id() << endl;
cout << "name " << msg.name() << endl;

文本数据读写
①.概述
protobuf 也可以把结构数据序列化为可识别的文本数据。
②.数据序列化
使用 TextFormat 类可以把结构数据转换为文本字符串。
protoTest::DemoMsg msg;
msg.set_id(1);
msg.set_name("Zhangsan");
string s;
TextFormat::PrintToString(msg, &s);
cout << s << endl;

③.数据反序列化
protoTest::DemoMsg msg;
msg.set_id(1);
msg.set_name("Zhangsan");
string s;
TextFormat::PrintToString(msg, &s);
msg.Clear();
TextFormat::ParseFromString(s, &msg);
cout << "id " << msg.id() << endl;
cout << "name " << msg.name() << endl;

二进制文件读写
①.概述
protobuf 也可以用于结构数据的存储。
②.数据序列化
protoTest::DemoMsg msg;
msg.set_id(1);
msg.set_name("Zhangsan");
ofstream file("demo", ios::binary);
msg.SerializeToOstream(&file);
file.clear();

③.数据反序列化
protoTest::DemoMsg msg;
ifstream in_file("demo", ios::binary);
msg.ParseFromIstream(&in_file);
cout << "id " << msg.id() << endl;
cout << "name " << msg.name() << endl;

文本文件读写
①.概述
protobuf 也可以把结构数据以文本形式保存到文件,可以当作系统的配置文件使用。
②.数据序列化
使用 TextFormat 类 的 Print 方法可以把结构数据转换为文本字符串并保存到文件。
protoTest::DemoMsg msg;
msg.set_id(1);
msg.set_name("Zhangsan");
int fd = open("demo", O_BINARY | O_WRONLY | O_CREAT);
FileOutputStream *output = new FileOutputStream(fd);
TextFormat::Print(msg, output);
delete output; output = nullptr;
close(fd);

③.数据反序列化
使用 TextFormat 类 的 Parse方法可以从文本文件中读取数据到结构。
protoTest::DemoMsg msg;
int fd = open("demo", O_RDONLY);
FileInputStream *input = new FileInputStream(fd);
TextFormat::Parse(input,&msg);
delete input; input = nullptr;
close(fd);
cout << "id " << msg.id() << endl;
cout << "name " << msg.name() << endl;


边栏推荐
猜你喜欢

Understand Chisel language. 28. Chisel advanced finite state machine (2) - Mealy state machine and comparison with Moore state machine

Application of time series database in the field of ship risk management

(一)Multisim安装与入门

3 years of software testing experience, the interview requires a monthly salary of 22K, obviously he has memorized a lot of interview questions...

内容产品进化三板斧:流量、技术、产品形态

Remember an experience of interviewing an outsourcing company, should you go?

Interface automation framework, lm-easytest beta version released, use it quickly~

canal抓取数据

时间序列的数据分析(四):STL分解

What is the relationship between the construction of smart cities and 5G technology in the new era
随机推荐
Digital signal processing course lab report (what foundation is needed for digital signal processing)
Web消息推送之SSE
MongoDB启动报错 Process: 29784 ExecStart=/usr/bin/mongod $OPTIONS (code=exited, status=14)
NFTScan 与 PANews 联合发布多链 NFT 数据分析报告
这个编辑器居然号称快如闪电!
43.【list链表的定义及初始化】
The main content of terrain analysis (the special effect level of the wandering earth)
自动化办公|办公软件和亿图脑图MindMaster快捷键
去腾讯面试,直接让人出门左拐 :幂等性都不知道!
华为无线设备Mesh配置命令
数字信号处理课程实验报告(数字信号处理需要什么基础)
跳槽前,把自己弄成卷王
The use of ccs software (app software that makes money reliably)
自动化测试之数据驱动DDT详细篇
Flask框架——Flask-SQLite数据库
业内人士真心话:只会测试没有前途的,我慌了......
开始学习C语言了
Could not acquire management access for administration
eclipse连接SQL server数据库「建议收藏」
JUC常见的线程池源码学习 02 ( ThreadPoolExecutor 线程池 )