在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. How to check for null/empty/whitespace values with a single test?

    SELECT column_name FROM table_name WHERE TRIM(column_name) IS NULL

  2. Linux学习笔记 11

    移除文件 #rm -i file 有信息确认的文件删除 #rm file1 file2 有信息确认的文件删除 #rm -rf file 强制删除file文件

  3. weblogic domain creation

    管理服务器 URL: http://CICI-ThinkPad:7001 Domain Path: D:\Program Files\DEV\Oracle\Middleware\user_projec ...

  4. 自己总结的C#编码规范--6.格式篇

    格式 格式的统一使用可以使代码清晰.美观.方便阅读.为了不影响编码效率,在此只作如下规定: 长度 一个文件最好不要超过500行(除IDE自动生成的类). 一个文件必须只有一个命名空间,严禁将多个命名空 ...

  5. C++多态实现原理详解

    C++的多态性用一句话概括就是:在基类的函数前加上virtual关键字,在派生类中重写该函数,运行时将会根据对象的实际类型来调用相应的函数.如果对象类型是派生类,就调用派生类的函数:如果对象类型是基类 ...

  6. IAR7.51提示秘钥无效IAR 以及 CCDebug驱动(包含win7 64bit)

    今天IAR不识别我的仿真器,然后我感觉驱动有问题,就把之前的驱动卸载了,但是按照以前的方法按章驱动(选择路径到IAR的某个目录),提示找不到驱动... 也不想重新装个IAR了,于是到CSDN上下载了这 ...

  7. Springboot2.x 启动报错:Bean named 'xxxService'... but was actually of type 'com.sun.proxy.$Proxy82'

    Springboot 2.0.5 搭建一个新项目启动后报错:Bean named 'xxxService'... but was actually of type 'com.sun.proxy.$Pr ...

  8. JAVA自学笔记07

    JAVA自学笔记07 1.构造方法 1) 例如:Student s = new Student();//构造方法 System.out.println(s);// Student@e5bbd6 2)功 ...

  9. Docker技术快速精通指南

    doctor专业网站:http://www.dockerinfo.net/ Docker中文文档 csdn 的docker专栏: Docker技术快速精通指南

  10. 《软件性能测试与LoadRunner实战教程》喜马拉雅有声图书上线

    工作忙的同学们有福了,可以听书了. 于涌老师的<软件性能测试与LoadRunner实战教程>喜马拉雅有声图书上线.