00. 目录

文章目录

  • 00. 目录
  • 01. 数组的序列化和反序列化
  • 02. 字符串序列化和反序列化
  • 03. 文件描述符序列化和反序列化
  • 04. C++ Stream序列化和反序列化
  • 05. 附录

01. 数组的序列化和反序列化

参考代码

#include "addressbook.pb.h"
#include <iostream>
using namespace std;/*
//C数组的序列化和序列化API
//在/usr/local/include/google/目录下,查找包含"SerializeToArray"所有的文件,同时打印所在行
//sudo grep "SerializeToArray" -r /usr/local/include/google/ -nbool SerializeToArray(void* data, int size) const; //序列化
bool ParseFromArray(const void* data, int size);   //反序列化
*/
char buf[1024];
int len;void set_person()
{Person obj;obj.set_name("itcast");obj.set_id(88);*obj.mutable_email() = "itcast@qq.com"; //obj.set_email("itcast@qq.com");len = obj.ByteSize(); //获取长度cout << "len = " << len << endl;obj.SerializeToArray(buf, len);//序列化,obj成员保存在buf中
}void get_person()
{Person obj;obj.ParseFromArray(buf, len); //反序列化,buf的内容设置给obj的成员cout << "name = " << obj.name() << endl;cout << "id = " << obj.id() << endl;cout << "email = " << obj.email() << endl;
}int main()
{// Verify that the version of the library that we linked against is// compatible with the version of the headers we compiled against.GOOGLE_PROTOBUF_VERIFY_VERSION;set_person(); //序列化get_person(); //反序列化// Optional:  Delete all global objects allocated by libprotobuf.google::protobuf::ShutdownProtobufLibrary();return 0;
}

编译和执行结果

deng@itcast:/mnt/hgfs/LinuxHome/day03$ ls
addressbook.pb.cc  addressbook.pb.h  addressbook.proto  a.out  test.cpp
deng@itcast:/mnt/hgfs/LinuxHome/day03$ g++ test.cpp addressbook.pb.cc `pkg-config --libs --cflags protobuf`
deng@itcast:/mnt/hgfs/LinuxHome/day03$ ./a.out
len = 25
name = itcast
id = 88
email = itcast@qq.com
deng@itcast:/mnt/hgfs/LinuxHome/day03$

02. 字符串序列化和反序列化

参考代码

#include "addressbook.pb.h"
#include <iostream>
using namespace std;//bool SerializeToString(string* output) const;
//bool ParseFromString(const string& data); string str; //全局变量void set_person()
{Person obj;obj.set_name("itcast");obj.set_id(88);obj.set_email("itcast@qq.com");//*obj.mutable_email() = "itcast@qq.com";obj.SerializeToString(&str);    //序列化,obj成员的内容设置给str
}void get_person()
{Person obj;obj.ParseFromString(str); //反序列化, str内容设置给obj的成员cout << "name = " << obj.name() << endl;cout << "id = " << obj.id() << endl;cout << "email = " << *obj.mutable_email() << endl;
}int main()
{// Verify that the version of the library that we linked against is// compatible with the version of the headers we compiled against.GOOGLE_PROTOBUF_VERIFY_VERSION;set_person(); //序列化get_person(); //反序列化// Optional:  Delete all global objects allocated by libprotobuf.google::protobuf::ShutdownProtobufLibrary();return 0;
}

编译和执行结果

deng@itcast:/mnt/hgfs/LinuxHome/day03$ ls
addressbook.pb.cc  addressbook.proto  test01.cpp
addressbook.pb.h   a.out              test.cpp
deng@itcast:/mnt/hgfs/LinuxHome/day03$ g++ test.cpp addressbook.pb.cc `pkg-config --libs --cflags protobuf`
deng@itcast:/mnt/hgfs/LinuxHome/day03$ ./a.out
name = itcast
id = 88
email = itcast@qq.com
deng@itcast:/mnt/hgfs/LinuxHome/day03$

03. 文件描述符序列化和反序列化

参考代码

#include "addressbook.pb.h"
#include <iostream>#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <unistd.h>using namespace std;//bool SerializeToFileDescriptor(int file_descriptor) const;
//bool ParseFromFileDescriptor(int file_descriptor); void set_person()
{Person obj;obj.set_name("itcast");obj.set_id(1);obj.set_email("itcast@qq.com");//*obj.mutable_email() = "itcast@qq.com";//O_CREAT: 新建文件, O_TRUNC:清空文件,O_RDWR:读写int fd = open("./pb.itcast", O_CREAT | O_TRUNC | O_RDWR, 0644);if (fd <= 0){perror("open");exit(0);}obj.SerializeToFileDescriptor(fd);  //序列化,obj成员的内容写入fd所关联的文件中close(fd); //关闭文件
}void get_person()
{int fd = open("./pb.itcast", O_RDONLY); //O_RDONLY: 只读方式if (fd <= 0){perror("open");exit(0);}Person obj;obj.ParseFromFileDescriptor(fd); //反序列化, fd文件内容设置给obj的成员close(fd); //关闭文件cout << "name = " << obj.name() << endl;cout << "id = " << obj.id() << endl;cout << "email = " << *obj.mutable_email() << endl;
}int main()
{// Verify that the version of the library that we linked against is// compatible with the version of the headers we compiled against.GOOGLE_PROTOBUF_VERIFY_VERSION;set_person(); //序列化get_person(); //反序列化// Optional:  Delete all global objects allocated by libprotobuf.google::protobuf::ShutdownProtobufLibrary();return 0;
}

编译和执行结果

deng@itcast:/mnt/hgfs/LinuxHome/day03$ ls
addressbook.pb.cc  addressbook.proto  pb.itcast   test02.cpp
addressbook.pb.h   a.out              test01.cpp  test.cpp
deng@itcast:/mnt/hgfs/LinuxHome/day03$ g++ test.cpp addressbook.pb.cc `pkg-config --libs --cflags protobuf`
deng@itcast:/mnt/hgfs/LinuxHome/day03$ ./a.out
name = itcast
id = 1
email = itcast@qq.com
deng@itcast:/mnt/hgfs/LinuxHome/day03$

04. C++ Stream序列化和反序列化

参考代码

#include "addressbook.pb.h"
#include <iostream>
#include <fstream>using namespace std;//bool SerializeToOstream(ostream* output) const;
//bool ParseFromIstream(istream* input);  void set_person()
{Person obj;obj.set_name("itcast");obj.set_id(1);obj.set_email("itcast@qq.com");//*obj.mutable_email() = "itcast@qq.com";fstream output("pb.itcast", ios::out | ios::trunc | ios::binary);bool flag = obj.SerializeToOstream(&output);//序列化if (!flag){cerr << "Failed to write file." << endl;return;}output.close();//关闭文件
}void get_person()
{Person obj;fstream input("./pb.itcast", ios::in | ios::binary);obj.ParseFromIstream(&input);  //反序列化input.close(); //关闭文件cout << "name = " << obj.name() << endl;cout << "id = " << obj.id() << endl;cout << "email = " << *obj.mutable_email() << endl;
}int main()
{// Verify that the version of the library that we linked against is// compatible with the version of the headers we compiled against.GOOGLE_PROTOBUF_VERIFY_VERSION;set_person(); //序列化get_person(); //反序列化// Optional:  Delete all global objects allocated by libprotobuf.google::protobuf::ShutdownProtobufLibrary();return 0;
}

编译和执行结果

deng@itcast:/mnt/hgfs/LinuxHome/day03$ ls
addressbook.pb.cc  addressbook.proto  pb.itcast   test02.cpp  test.cpp
addressbook.pb.h   a.out              test01.cpp  test03.cpp
deng@itcast:/mnt/hgfs/LinuxHome/day03$ g++ test.cpp addressbook.pb.cc `pkg-config --libs --cflags protobuf`
deng@itcast:/mnt/hgfs/LinuxHome/day03$ ./a.out
name = itcast
id = 1
email = itcast@qq.com
deng@itcast:/mnt/hgfs/LinuxHome/day03$

05. 附录

参考博客:https://blog.csdn.net/sealyao/article/details/6940245

测试代码下载:测试代码

【Protocol Buffer】Protocol Buffer入门教程(四):序列化和反序列化相关推荐

  1. LittleVGL (LVGL)干货入门教程四之制作和使用中文汉字字库

    LittleVGL (LVGL)干货入门教程四之制作和使用中文汉字字库 前言: 阅读前,请确保你至少拥有以下条件: 已实现显示API(教程一已实现, 链接:LittleVGL (LVGL)入门教程一之 ...

  2. Python+Opencv图像处理新手入门教程(四):视频内容的读取与导出

    一步一步来吧 上一节: Python+Opencv图像处理新手入门教程(三):阈值与二值化 1.Intro 今天这节我们主要看怎么利用opencv读取并处理视频中的内容. 2.VideoCapture ...

  3. (原创)LEON3入门教程(四):基于AMBA APB总线的七段数码管IP核设计

    摘要:这一小节将介绍下如何设计用户自定义的APB IP,并将IP嵌入到SOPC中去.一个APB IP核的主要分为三个部分:逻辑单元.寄存器单元和接口单元.所设计的IP是一个简单的七段数码管显示IP,只 ...

  4. docker 镜像修改的配置文件自动还原_原创 | 全网最实在的docker入门教程四

    作者:潘吉祥 上一篇我们学习了如何使用Dockerfile制作自己的镜像,不过这种方式更像纯粹的运维方式,作为开发者来说,未免有些小繁琐,一个不小心写错些命令就执行失败,我们还不知道错误在哪,这着实有 ...

  5. python画图颜色表示大小变化_python画图(线条颜色、大小、类型:点、虚线等)(图文详细入门教程四)...

    初衷 本人由于平常写论文需要输出一些结果图,但是苦于在网上搜python画图时,详细的教程非常多,但是就是找不到能马上解决自己问题那一行代码,所以打算写一些适合需求简单的朋友应急用的教程,应急就必须方 ...

  6. SpringCloud 入门教程(四): 分布式环境下自动发现配置服务

    前一章, 我们的Hello world应用服务,通过配置服务器Config Server获取到了我们配置的hello信息"hello world". 但自己的配置文件中必须配置co ...

  7. docker registry push 覆盖_原创 | 全网最实在的docker入门教程四

    原创头条号:码农code之路,作者:潘吉祥,转载请标明出处 上一篇我们学习了如何使用Dockerfile制作自己的镜像,不过这种方式更像纯粹的运维方式,作为开发者来说,未免有些小繁琐,一个不小心写错些 ...

  8. Spring Boot入门教程(四十):微信支付集成-刷卡支付

    分享一个朋友的人工智能教程.比较通俗易懂,风趣幽默,感兴趣的朋友可以去看看. 一:准备工作 使用微信支付需要先开通服务号,然后还要开通微信支付,最后还要配置一些开发参数,过程比较多. 申请服务号(企业 ...

  9. Spring Boot入门教程(四十一):微信支付集成-扫码支付

    分享一个朋友的人工智能教程.比较通俗易懂,风趣幽默,感兴趣的朋友可以去看看. 一:准备工作 使用微信支付需要先开通服务号,然后还要开通微信支付,最后还要配置一些开发参数,过程比较多. 申请服务号(企业 ...

最新文章

  1. 【Python教程】 re 模块中findall() 函数返回值展现方式的用法详解
  2. 《黑天鹅》读书笔记(part2)--我们从重复中学习,但忽略了从未发生过的事件
  3. SAP Spartacus 自定义 Component 的使用 - SimpleResponsiveBannerComponent
  4. linux外部命令帮助,Linux的命令帮助
  5. 前端之 jQuery 入门
  6. oracle 两表两列数据对比_【SQL】根据两列信息,整合两张表数据
  7. linux redis 工具,linux下redis安装 + 工具 putty
  8. XCodeGhost 病毒检查方法
  9. linux内核2.6.3x--Executable file formats / Emulations、 Networking support
  10. 字节跳动 录屏功能_非常值得推荐,字节跳动出品的一款协同办公软件。
  11. WGS84坐标系-地心地固坐标系-东北天坐标系
  12. 计算机网络 --- 网络编程
  13. matlab离散系统的频率响应,离散系统的频率响应分析
  14. stupid代码提交到github
  15. 新浪微博PC端登陆js分析及Python实现微博post登陆
  16. 微积分入门(持续更新)
  17. 针对linux系统中/usr/src/kernels中找不到内核源码的问题
  18. clamav --reload 加载病毒库源码分析
  19. Spring Boot使用ApplicationEvent来实现事件发布订阅功能(美女一个都不能少,都要通知到) - 第420篇
  20. AutoCAD2016安装破解教程

热门文章

  1. seajs打包部署工具spm的使用总结
  2. android IntentService
  3. VC++ 中 try-catch-finally 语句 如何在获取正常信息是写一些操作语句
  4. gRaphael——JavaScript 矢量图表库:两行代码实现精美图表
  5. c#对象集合去重_C# List 对象去重
  6. ajax请求获取服务器数据,jquery.ajax发布从app引擎服务器获取数据的请求
  7. webpack中使用jquery
  8. 大话目标检测经典模型(RCNN、Fast RCNN、Faster RCNN)
  9. struts2原理(转)
  10. TCP/IP详解学习笔记(2)-数据链路层