google protobuf之一个使用的简单类型进行序列化和反序列化的实例

  • 先写一个文件尾缀为proto文本文件,message.proto,内容如下:
syntax = "proto3"; 
//指定使用的proto版本,这里是protobuf3

//import "user.proto";
//这里可以包含其他proto文件,可以用user文件中的message定义字段
//生成的message.pb.h中会有#include "user.pb.h"一句代码
//相当于自动include其他proto生成的头文件,自然也可以
//使用user中的类型定义message.proto中的某个类的字段

package chat.proto.message;  
//生成c++代码后,相当于嵌套三层的namespace,即chat::proto::message

//每一个message对应即将生成源文件中的一个类,即chat::proto::message::LoginRequest
message LoginRequest{ 
    bytes name = 1; //bytes和string使用没有什么区别,这里也可以使用string
    bytes pwd = 2;
}

message LoginReesponse{
    int32 errcode = 1;
    bytes errmsg = 2;
    bool success = 3;
}
  • 然后使用protoc工具,将上面的proto文件生成对应的头文件和源文件:
protoc message.proto --cpp_out=./
执行完成后将生成两个文件,分别是message.pb.h和message.pb.cc

protoc:用来生成源码的转换工具;
message.proto:就是我们要将哪些proto转换成源文件
--cpp_out:转换成c++代码
./:生成的message.pb.cc和message.pb.h就存放在当前文件夹
  • 序列化:将数据转换成字节流;
  • 反序列化:将数据从字节流中提取转换出来
  • 使用实例:
#include <iostream>
#include <string>
#include "message.pb.h"
using namespace std;
using namespace chat::proto::message;

int main(){
    LoginRequest lre;
    lre.set_name("king");
    lre.set_pwd("1234567");

    //数据序列化,序列化成字节流/字符流
    string str("xxx");
    cout<<str.c_str()<<endl; 
    lre.SerializeToString(&str);
    cout<<str.c_str()<<endl; //实际开发可以通过网络发送出去
    //bool SerializeToString(std::string* output) const;

    //数据的反序列化,将数据从字节流中解析出来
    LoginRequest lrf;
    lrf.ParseFromString(str);
    cout<<lrf.name()<<endl;
    cout<<lrf.pwd()<<endl;
    //inline bool ParseFromString(const std::string& data)
    return 0;
}
g++ main.cpp message.pb.cc user.pb.cc -lprotobuf
./a.out

output:

xxx

king1234567
king
1234567
  • protobuff两个重要常用的方法
  // Serialize the message and store it in the given string.  All required
  // fields must be set.
 bool SerializeToString(std::string* output) const;
  将一个proto对象序列化成字节流,存储在字符串output中。成功返回true.

inline bool ParseFromString(const std::string& data);
将自已存放proto对象数据的字符串进行解析,也就是将数据从字符串中取出来。成功返回true.
  • 1
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
在中使用XML向后台发起请求实现protobuf数据的序列化反序列化,可以按照以下步骤进行: 1. 安装protobufjs库 在Vue项目中使用npm或yarn安装protobufjs库,具体命令为: ```bash npm install protobufjs --save ``` 或者 ```bash yarn add protobufjs ``` 2. 定义protobuf数据结构 在Vue项目中定义protobuf数据结构,可以使用protobufjs提供的API进行定义。例如: ```javascript // 定义protobuf数据结构 import protobuf from 'protobufjs' const root = protobuf.Root.fromJSON(jsonDescriptor) const MessageType = root.lookupType('example.Message') // 创建protobuf实例 const message = MessageType.create({ id: 1, name: 'example' }) ``` 其中,jsonDescriptor是protobuf数据结构的JSON描述对象。 3. 序列化protobuf数据 使用protobufjs提供的API将protobuf数据序列化为XML数据,例如: ```javascript // 将protobuf数据序列化为XML数据 const xml = MessageType.encode(message).finish() ``` 其中,encode方法将protobuf数据序列化为二进制数据,finish方法返回一个Uint8Array类型的二进制数据。 4. 发起XML请求 使用axios等库向后台发起XML请求,例如: ```javascript // 发起XML请求 axios.post('/api/example', xml, { headers: { 'Content-Type': 'text/xml' } }) .then(response => { // 处理响应数据 const xml = response.data const buffer = new Uint8Array(xml) const decodedMessage = MessageType.decode(buffer) console.log(decodedMessage) }) .catch(error => { // 处理错误信息 console.error(error) }) ``` 其中,post方法向后台发起POST请求,xml参数是序列化后的XML数据,headers参数指定Content-Type为text/xml。 5. 反序列化protobuf数据 在处理后台响应数据时,使用protobufjs提供的API将XML数据反序列化protobuf数据,例如: ```javascript // 将XML数据反序列化protobuf数据 const buffer = new Uint8Array(xml) const decodedMessage = MessageType.decode(buffer) console.log(decodedMessage) ``` 以上是在Vue项目中使用XML向后台发起请求实现protobuf数据的序列化反序列化的一般步骤,具体实现还需要根据项目需求进行调整。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值