在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 序列化的更多相关文章

  1. 记一次protobuf和hbase自带protobuf版本冲突的解决

    使用protobuf生产模板代码,使用的版本是: <dependency> <groupId>com.google.protobuf</groupId> <a ...

  2. protobuf protocol-buffers 序列化数据 gobs pickling string XML 用C实现的cPickle比pickle快1000倍 protobuf2 protobuf3 差异

    场景: 浏览器请求--->python数据生成--->python-生成excel--->浏览器下载excel 目标: 重构为 浏览器请求--->python数据生成---&g ...

  3. Netty4.x整合SpringBoot2.x使用Protobuf3详解

    前言 本篇文章主要介绍的是SpringBoot整合Netty以及使用Protobuf进行数据传输的相关内容.Protobuf会介绍下用法,至于Netty在netty 之 telnet HelloWor ...

  4. Protocol Buffers(1):序列化、编译与使用

    目录 序列化与反序列化 Protocol Buffers概览 Protocol Buffers C++ 编译 Protocol Buffers C++ 使用 Protocol Buffers的可读性 ...

  5. Protobuf3 语法指南

    目录 [−] 定义一个消息类型 指定字段类型 分配标识号 指定字段规则 添加更多消息类型 添加注释 保留标识符(Reserved) 从.proto文件生成了什么? 标量数值类型 默认值 枚举 使用其他 ...

  6. Centos 7安装protobuf3.6.1

    新版本 google protobuf-3.6.1是现在最新版本,添加了新的特性,看说明 下载地址 https://github.com/protocolbuffers/protobuf/releas ...

  7. 十一.Protobuf3可选项

    Protobuf3 可选项 .proto文件中可以声明许多选项.选项不会改变声明的整体含义,但可能会影响在特定上下文中处理声明的方式.可用选项的完整列表在google/protobuf/descrip ...

  8. 九.Protobuf3特殊类型

    Protobuf3 Any类型 Any消息类型允许您将消息作为嵌入类型,而不需要它们 .proto定义.Any包含任意序列化的消息(字节),以及一个URL,该URL充当该消息的全局唯一标识符并解析为该 ...

  9. 八.Protobuf3更新消息类型(添加新的字段)

    Protobuf3 更新消息类型 如果现有的消息类型不满足你的所有需求——例如,你希望消息格式有一个额外的字段——但是你仍然希望使用用旧格式创建的代码,别担心!在不破坏任何现有代码的情况下更新消息类型 ...

随机推荐

  1. fdisk

    fdisk管理分区 参数                 作用                                   m                                 ...

  2. 网络篇:linux下select、poll、epoll之间的区别总结

    select.poll.epoll之间的区别总结 select,poll,epoll都是IO多路复用的机制.I/O多路复用就通过一种机制,可以监视多个描述符,一旦某个描述符就绪(一般是读就绪或者写就绪 ...

  3. C# 使用三层架构实例演示-winForm 窗体登录功能

    ---------------------------------------------------------------------------------------------------华 ...

  4. Error watching file for changes: EMFILE

    运行reactnative项目时在编译过程中报错 Error watching file for changes: EMFILE 故障原因: 是升级后watchman不可用了,需要重装watchman ...

  5. CSS设置文字不能被选中

    /*设置文字不能被选中     以下为css样式*/ -webkit-user-select:none; -moz-user-select:none; -ms-user-select:none; us ...

  6. NodeJS 连接接MySQL

    NodeJS 连接接MySQL MySQL是常用数据库,作为后端模块,nodejs可以提供了mysql接口 安装 $ npm install mysql 测试代码 var mysql = requir ...

  7. jmeter接口测试实例5-文件上传

    Jmeter实例5:文件上传 添加http协议.添加IP.路径.方法.选择files upload文件名称tab,输入绝对路径,参数名称,运行: 上传成功

  8. xpath分析 html文件抽正文的过程

    使用Py3的HTMLParser解析模块解析HTML的时候,出现:no moudle named 'markupbase' 错误. 用xpath打算分析html里面的新闻.根据运行程序后的报错的信息, ...

  9. 2018-8-16JWTtoken用户登录认证思路分析9502751

    2018-8-16JWTtoken用户登录认证思路分析9502751 JWT token在商城中的实现 class UserView(CreateAPIView): serializer_class ...

  10. linux 修改时间和时区

    linux系统时间有两个,一个是硬件时间,即BIOS时间,就是我们进行CMOS设置时看到的时间,另一个是系统时间,是linux系统Kernel时间.当Linux启动时,系统Kernel会去读取硬件时钟 ...