Protobuf3 序列化
在message_lite.h中定义了SerializeToString ,SerializeToArray ,SerializeToCodedStream ,SerializeToZeroCopyStream 其它序列化到IO流、序列化到文件等接口在它的子类message.h文件中提供。
另外,在util/json_util.h文件中定义了protobuf与json相互转换的接口(需要注意的是:当json字符串字段的值等于protobuf中字段的默认值,message->json则此字段不会出现在json中)。
#include <iostream>
#include <fstream>
#include <istream>
#include <string> #include <google/protobuf/io/coded_stream.h>
#include <google/protobuf/io/zero_copy_stream.h>
#include <google/protobuf/io/zero_copy_stream_impl.h>
#include <google/protobuf/util/json_util.h> #include <fcntl.h>
#include <unistd.h> #include "game.pb.h" void serializeToString(pt::rsp_login& rsp)
{
//序列化到字符串和从字符串解析数据
std::string str{};
rsp.SerializeToString(&str);
std::cout << "serialize to string:" << str << std::endl; pt::rsp_login rsp2{};
rsp2.ParseFromString(str);
std::cout << "parse from string size:" << rsp2.ByteSize() << std::endl; auto temp_str = rsp.SerializeAsString();
std::cout << "serialize as string:" << temp_str << std::endl;
} void serializeToArray(pt::rsp_login& rsp)
{
//序列化到数组和从数组解析数据
std::vector<uint8_t> buff;
buff.resize(rsp.ByteSize());
rsp.SerializeToArray(buff.data(), buff.size()); pt::rsp_login rsp2{};
if (!rsp2.ParseFromArray(buff.data(), (int)buff.size())) {
std::cout << "parse error\n";
}
std::cout << "parse from array size:" << rsp2.ByteSize() << std::endl;
} void serializeToCodeStream(pt::rsp_login& rsp)
{
using namespace google::protobuf::io;
int fd1 = open("myfile", O_CREAT | O_RDWR);
if (fd1 == -) {
return;
}
ZeroCopyOutputStream *raw_output = new FileOutputStream(fd1);
google::protobuf::io::CodedOutputStream coded_output(raw_output);
rsp.SerializeToCodedStream(&coded_output);
delete raw_output;
close(fd1); pt::rsp_login rsp2{};
int fd2 = open("myfile", O_RDONLY);
if (fd2 == -) {
return;
}
ZeroCopyInputStream *raw_input = new FileInputStream(fd2);
google::protobuf::io::CodedInputStream coded_input(raw_input);
rsp2.ParseFromCodedStream(&coded_input);
delete raw_input;
close(fd2);
} void serializeToStream(pt::rsp_login& rsp)
{
std::fstream fs1("stream.info", std::ios::out | std::ios::binary);
rsp.SerializeToOstream(&fs1);
fs1.close(); pt::rsp_login rsp2{};
std::fstream fs2("stream.info", std::ios::in | std::ios::binary);
rsp2.ParseFromIstream(&fs2);
fs2.close(); std::cout << "parse in stream:" << rsp2.ByteSize() << std::endl;
} void serializeToFile(pt::rsp_login& rsp)
{
//序列化到文件
std::ofstream ofile;
auto fd = open("game.info", O_CREAT | O_TRUNC | O_RDWR, );
if (fd <= ) {
std::cout << "open file error" << std::endl;
return;
}
rsp.SerializeToFileDescriptor(fd);
close(fd); pt::rsp_login rsp2{};
fd = open("game.info", O_RDONLY);
if (fd <= ) {
std::cout << "serialize to file error" << std::endl;
return;
}
rsp2.ParseFromFileDescriptor(fd);
close(fd); std::cout << "parse from file rsp2.size:" << rsp2.ByteSize() << std::endl;
} void jsonMessageConvent(pt::rsp_login& rsp)
{
//如果字段值=protobuf中的类型默认值,则此字段不会转换到Json字符串中
std::string json{};
google::protobuf::util::MessageToJsonString(rsp, &json);
std::cout << "message to json:" << json << std::endl; //json字符串中字段的值=protobuf中字段类型默认值,则此json字段不会转换到protobuf消息中
pt::rsp_login rsp2{};
google::protobuf::util::JsonStringToMessage(json, &rsp2);
std::cout << "json to message size:" << rsp2.ByteSize() << std::endl;
} int main()
{
pt::rsp_login rsp{};
rsp.set_ret(pt::rsp_login_RET_SUCCESS);
auto user_info = rsp.mutable_user_info();
user_info->set_nickname("dsw");
user_info->set_icon("345DS55GF34D774S");
user_info->set_coin();
user_info->set_location("zh"); for (int i = ; i < ; i++) {
auto record = rsp.add_record();
record->set_time("2017/4/13 12:22:11");
record->set_kill(i * );
record->set_dead(i * );
record->set_assist(i * );
} serializeToString(rsp);
serializeToArray(rsp);
serializeToCodeStream(rsp); serializeToStream(rsp);
serializeToFile(rsp); jsonMessageConvent(rsp); return ;
}
Protobuf3 序列化的更多相关文章
- 记一次protobuf和hbase自带protobuf版本冲突的解决
使用protobuf生产模板代码,使用的版本是: <dependency> <groupId>com.google.protobuf</groupId> <a ...
- protobuf protocol-buffers 序列化数据 gobs pickling string XML 用C实现的cPickle比pickle快1000倍 protobuf2 protobuf3 差异
场景: 浏览器请求--->python数据生成--->python-生成excel--->浏览器下载excel 目标: 重构为 浏览器请求--->python数据生成---&g ...
- Netty4.x整合SpringBoot2.x使用Protobuf3详解
前言 本篇文章主要介绍的是SpringBoot整合Netty以及使用Protobuf进行数据传输的相关内容.Protobuf会介绍下用法,至于Netty在netty 之 telnet HelloWor ...
- Protocol Buffers(1):序列化、编译与使用
目录 序列化与反序列化 Protocol Buffers概览 Protocol Buffers C++ 编译 Protocol Buffers C++ 使用 Protocol Buffers的可读性 ...
- Protobuf3 语法指南
目录 [−] 定义一个消息类型 指定字段类型 分配标识号 指定字段规则 添加更多消息类型 添加注释 保留标识符(Reserved) 从.proto文件生成了什么? 标量数值类型 默认值 枚举 使用其他 ...
- Centos 7安装protobuf3.6.1
新版本 google protobuf-3.6.1是现在最新版本,添加了新的特性,看说明 下载地址 https://github.com/protocolbuffers/protobuf/releas ...
- 十一.Protobuf3可选项
Protobuf3 可选项 .proto文件中可以声明许多选项.选项不会改变声明的整体含义,但可能会影响在特定上下文中处理声明的方式.可用选项的完整列表在google/protobuf/descrip ...
- 九.Protobuf3特殊类型
Protobuf3 Any类型 Any消息类型允许您将消息作为嵌入类型,而不需要它们 .proto定义.Any包含任意序列化的消息(字节),以及一个URL,该URL充当该消息的全局唯一标识符并解析为该 ...
- 八.Protobuf3更新消息类型(添加新的字段)
Protobuf3 更新消息类型 如果现有的消息类型不满足你的所有需求——例如,你希望消息格式有一个额外的字段——但是你仍然希望使用用旧格式创建的代码,别担心!在不破坏任何现有代码的情况下更新消息类型 ...
随机推荐
- Xamarin Essentials教程振动Vibration
Xamarin Essentials教程振动Vibration 振动是提醒用户的有效方式,尤其是声音提示效果不明显的场景中,如吵杂的环境中,手机放到包中.在很多的游戏中,振动还用来模拟游戏特效,如 ...
- 前面的内容 也是要去掉白名单 和 8.8.8.8这种非问题IP的 高风险 么? (目前我们没有获取客户的中风险、低风险数据,可以处理掉高风险)
前面的内容 也是要去掉白名单 和 8.8.8.8这种非问题IP的 高风险 么? (目前我们没有获取客户的中风险.低风险数据,可以处理掉高风险) == 整体把关.不清楚细节,所以只能从整体决策.做 ...
- 解决SpringBoot的@Autowired无法注入问题
问题:@Autowired无法自动注入 思路:SpringBoot项目的Bean装配默认规则是根据Application类所在的包位置从上往下扫描!"Application类"是指 ...
- node cluster模块,仿多线程并发调用,
worker.js var cluster = require('cluster')function fibo(n) { return n == 0 ? 0 : n > 1 ? fibo(n - ...
- node 将本地项目docker化
vi test.sh docker rm -f kao3 || echo kao3 not exists;docker run -itd \--privileged=true \-v ~/logs:/ ...
- ReactNative用指定的真机/模拟器运行项目
使用模拟器运行项目: 命令行中React native项目目录下键入react-native run-ios会启动iOS模拟器, 默认是使用iPhone6,如果想要试用其他版本的模拟器则需要在reac ...
- [SDOI2017]树点涂色
Description: Bob有一棵\(n\)个点的有根树,其中1号点是根节点.Bob在每个点上涂了颜色,并且每个点上的颜色不同. 定义一条路径的权值是:这条路径上的点(包括起点和终点)共有多少种不 ...
- HTTP断点续传
一.概述 所谓断点续传,其实只是指下载,也就是要从文件已经下载的地方开始继续下载.在以前版本的HTTP协议是不支持断点的,HTTP/1.1开始就支持了.一般断点下载时才用到Range和Conten ...
- css3中linear-gradient()的使用
用线性渐变创建图像. 如果想创建以对角线方式渐变的图像,可以使用 to top left 这样的多关键字方式来实现. 示例代码: linear-gradient(#fff, #333); linear ...
- 将Django部署到Linux
https://cloud.tencent.com/developer/labs/lab/10372