Protobuf Any

原文

一下案例是C++版本
1,编写.proto文件
base.proto

syntax = "proto3";  //指定proto编译器版本为3  默认是2
import "google/protobuf/any.proto";  //使用Any必须要导入Any.proto

enum Type
{
    FACE = 0;
    PLATE = 1;
}

message Base
{
    Type type = 1;
  int32 page_number = 2;
  int32 result_per_page = 3;
  repeated google.protobuf.Any object = 4;

}

message Face
{
    string name = 1;
}

message Plate
{
    string email = 1;
}

2,生成对应的.cc和.h

protoc  --proto_path=./ --cpp_out=./ ./base.proto 

3,在工程中使用
把生成的.cc和.h文件拷贝到工程目录下,并在使用的地方添加头文件代码如下

#include <iostream>

using namespace std;
#include<fstream>

//input .h
#include"base.pb.h"

int main(int argc, char *argv[])
{
    string filename("prototest1.text"); //序列化保存到本地文件
    fstream output(filename,ios::out | ios::trunc| ios::binary);
    Base base;
    base.set_type(Type::FACE);
    base.set_page_number(2);
    base.set_result_per_page(66);
    Face face;
    face.set_name("lvlvlv");
    base.add_object()->PackFrom(face); //为Any对象赋值
    Plate plate;
    plate.set_email("123456@163.com");
    base.add_object()->PackFrom(plate);
    base.SerializeToOstream(&output);//序列化到本地文件
    output.close();
    cout << "Hello World!" << endl;

    string filename2("prototest1.text");
    fstream input(filename2,ios::in |ios::binary);
    Base base2;
    base2.ParseFromIstream(&input); //从本地文件中反序列化过来
    input.close();
    cout<<base2.page_number()<<"  "<<base2.result_per_page()<<endl;

//从Any对象中恢复到具体对象
        for(const ::google::protobuf::Any& object : base2.object()) 
        {
            if(object.Is<Face>()) //判断对象类型
            {
                Face face;
                object.UnpackTo(&face);
                cout<<"name "<<face.name()<<endl;
            }else if(object.Is<Plate>())
            {
                Plate plate;
                object.UnpackTo(&plate);
                cout<<plate.email()<<endl;
            }
        }


    return 0;
}
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
在JavaScript中使用Protobuf的Any类型需要进行一些特定的操作。Any类型允许在消息中存储任意类型的值,但在JavaScript中没有原生支持。下面是一种实现Any类型的方法: 1. 首先,确保你已经安装了Google的protobuf库。可以使用npm命令来安装: ```shell npm install google-protobuf ``` 2. 在你的JavaScript文件中引入所需的protobuf库: ```javascript const protobuf = require("google-protobuf"); ``` 3. 创建一个新的Any类型对象并设置其值: ```javascript const anyValue = new protobuf.Any(); anyValue.pack(MessageType, message); ``` 在上述代码中,`MessageType` 是你要存储的消息类型,`message` 是一个具体的消息实例。通过调用`pack()`方法,将消息类型和实例打包到Any对象中。 4. 将Any对象序列化为字节流: ```javascript const bytes = anyValue.serializeBinary(); ``` 通过调用`serializeBinary()`方法,将Any对象转换为字节流。 5. 反序列化字节流为Any对象: ```javascript const anyValue = new protobuf.Any(); anyValue.deserializeBinary(bytes); ``` 通过调用`deserializeBinary()`方法,将字节流转换为Any对象。 6. 获取Any对象中存储的消息类型和实例: ```javascript const messageType = anyValue.getTypeName(); const message = anyValue.unpack(MessageType); ``` 通过调用`getTypeName()`方法,获取消息类型的名称。通过调用`unpack()`方法,将Any对象解包为具体的消息实例。 这是一个简单的示例,用于在JavaScript中使用Protobuf的Any类型。请根据你的实际需求进行相应的调整和扩展。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值