当前位置:网站首页>使用 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;
边栏推荐
- 深入浅出零钱兑换问题——背包问题的套壳
- Interface automation framework, lm-easytest beta version released, use it quickly~
- 关于String的一些思考
- Cookie simulation login "recommended collection"
- How to use Databricks for data analysis on TiDB Cloud | TiDB Cloud User Guide
- Chapter6 : Has Artificial Intelligence Impacted Drug Discovery?
- Some thoughts on String
- LoRaWAN网关源码分析(V1.0.2)
- 如何在 TiDB Cloud 上使用 Databricks 进行数据分析 | TiDB Cloud 使用指南
- redis6.0 源码学习(五)ziplist
猜你喜欢
记面试外包公司的一次经历,到底该不该去?
Huawei's 7-year-experienced software testing director, gives some advice to all friends who want to change careers to learn software testing
双碳目标下:农田温室气体排放模拟
A Small Case About Containers
百家号取消接口发文功能:插外链获权重被堵死
这个编辑器居然号称快如闪电!
新一代开源免费的终端工具,太酷了
Recommended open source tools: MegPeak, a high-performance computing tool
我为何从开发人员转做测试,3年软件测试工程师,带你聊聊这其中的秘辛
Start learning C language
随机推荐
PyQt5快速开发与实战 9.1 使用PyInstaller打包项目生成exe文件
BI-SQL丨WHILE
使用bat脚本以json格式导出mongo数据库中指定表数据
#第九章 子查询课后习题
sql中ddl和dml(sql与access的区别)
华为再发「天才少年」召集令!曾放弃360万年薪的他也来首秀
Flink实时数仓完结
容器排序案例
八年测试经验,为何惨遭领导痛批:你写的测试文档还不如刚来的应届生
Why did I switch from developer to testing, 3 years software testing engineer, tell you the secret of this
JSON common annotations
What is the relationship between the construction of smart cities and 5G technology in the new era
那些破釜沉舟入局Web3.0的互联网精英都怎么样了?
VLAN实验
SLF4J的使用
手把手教你写让人眼前一亮的软件测试简历,收不到面试邀请算我输
Allure进阶-动态生成报告内容
Androd 跳转到google应用市场
Recommended open source tools: MegPeak, a high-performance computing tool
【Vue.js 3.0源码】KeepAlive 组件:如何让组件在内存中缓存和调度?