Protobuf3 系列二 定义复杂的proto文件
2017-04-25 19:56
253 查看
定义复杂的对象
这是Protobuf3的系列二: 如何在protobuf中定义更复杂的对象proto文件
除了定义string, int等基础对象外(protobuf的基础数据结构和Java变量的对应关系见文章末尾),还可以在proto中定义更复杂的对象,详细如下:定义List列表:值可以是普通变量,也可以复杂对象
message ComplexObject { repeated string sons = 4; // List列表 repeated Result result = 6; // 复杂的对象List } // 定义一个新的对象 message Result { string url = 1; string title = 2; repeated string snippets = 3; }
定义枚举成员
message ComplexObject { ... Gender gender = 5; // Enum值 ... } enum Gender { MAN = 0; WOMAN = 1; }
定义Any对象:用于定义任意的值
message ComplexObject { repeated google.protobuf.Any any = 7; // Any对象 }
定义Map对象:key,value都可以是复杂对象
message ComplexObject { map<string, MapVaule> map = 8; // 定义Map对象 } // 定义Map的value值 message MapVaule { string mapValue = 1; }
通过 reserved保留 Assigning Tags和filed name用于将来扩展用
message ComplexObject { reserved 12, 15, 9 to 11; // 预留将来使用的Assigning Tags, reserved "foo", "bar"; // 预留将来使用的filed name }
以上完整proto文件
// 如果使用此注释,则使用proto3; 否则使用proto2 syntax = "proto3"; // 引入外部的proto对象 import "google/protobuf/any.proto"; // 生成类的包名 option java_package = "com.hry.spring.proto.complex"; //生成的数据访问类的类名 option java_outer_classname = "MyComplexObjectEntity"; message ComplexObject { // Message里每个成员变量都有一个唯一的数字标志( Assigning Tags) int32 id = 1;// singular, 默认值,表示成员只有0个或者1个 string name = 2;// string email = 3;// repeated string sons = 4; // repeated 列表 Gender gender = 5; // Enum值 repeated Result result = 6; // 新的对象List repeated google.protobuf.Any any = 7; // Any对象 map<string, MapVaule> map = 8; // 定义Map对象 // reserved reserved 12, 15, 9 to 11; // 预留将来使用的Assigning Tags, reserved "foo", "bar"; // 预留将来使用的filed name } enum Gender { MAN = 0; WOMAN = 1; } // 定义一个新的对象 message Result { string url = 1; string title = 2; repeated string snippets = 3; } // 定义Map的value值 message MapVaule { string mapValue = 1; }
生成Java类
生成的方法详细见Protobuf3 的第一个Java demo。生成的MyComplexObjectEntity类见代码测试
测试类ComplexObject.Builder builder = ComplexObject.newBuilder(); // 对应proto里: int32 id = 1; builder.setId(100); // 对应proto里:string name = 2;// builder.setName("name"); // 对应proto里:string email = 3; builder.setEmail("email"); // 对应proto里:repeated string sons = 4; 列表 builder.addSons("Son1"); builder.addSons("Son2"); // 对应proto里:Gender gender = 5; // Enum值 // builder.setGenderValue(Gender.MAN_VALUE); builder.setGender(Gender.MAN); // 对应proto里:repeated Result result = 6; // 新的Result对象List Result.Builder r = Result.newBuilder(); r.setTitle("title"); builder.addResult(r.build()); // 对应proto里:repeated google.protobuf.Any any = 7; // Any对象 Any.Builder any = Any.newBuilder(); any.setTypeUrl("typeUrl"); builder.addAny(any.build()); // 对应proto里:map<string, MapVaule> map_field = 8; MapVaule.Builder mapValue = MapVaule.newBuilder(); mapValue.setMapValue("mapValue"); builder.putMap("mapKey", mapValue.build()); // 生成最终的对象 ComplexObject cob = builder.build(); System.out.println("before :"+ cob.toString()); System.out.println("===========ComplexObject Byte=========="); for(byte b : cob.toByteArray()){ System.out.print(b); } System.out.println(); System.out.println(cob.toByteString()); System.out.println("================================"); //模拟接收Byte[],反序列化成Person类 byte[] byteArray =cob.toByteArray(); ComplexObject cob2 = ComplexObject.parseFrom(byteArray); System.out.println("after :" + cob2.toString());
结果
before :id: 100 name: "name" email: "email" sons: "Son1" sons: "Son2" result { title: "title" } any { type_url: "typeUrl" } map { key: "mapKey" value { mapValue: "mapValue" } } ===========ComplexObject Byte========== 810018411097109101265101109971051083448311111049344831111105050718511610511610810158910711612111210185114108662010610997112751011211810108109971128697108117101 <ByteString@1b0375b3 size=69> ================================ after :id: 100 name: "name" email: "email" sons: "Son1" sons: "Son2" result { title: "title" } any { type_url: "typeUrl" } map { key: "mapKey" value { mapValue: "mapValue" } }
protobuf的基础数据结构和Java变量的对应关系
代码
github相关文章推荐
- caffe 实战系列:proto文件格式以及含义解析:如何定义网络,如何设置网络参数(以AlexNet为例) 2016.3.30
- caffe 实战系列:proto文件格式以及含义解析:如何定义网络,如何设置网络参数(以AlexNet为例)
- Hibernate 资源文件中定义复杂的自定义sql,可灵活用于自定义的复杂增删改查
- 用protobuf定义消息及处理
- protobuf文件嵌套结构实例
- win环境下实现导出protobuf文件的lua版本
- protobuf 一个.proto 文件的简单讲解
- 使用protobuf定义消息
- Protobuf一键生成代码bat文件
- vs2013编译google protobuf生成的消息文件错误。
- 基于netty的项目中使用protobuf,巧妙定义proto完成不同消息的编码和解码处理 .
- protobuf的配置原始文件.proto中,使用修饰符repeated、required、optional生成的java文件的区别
- 用protobuf定义消息及处理
- 对protobuf进行ndk编译 config.h文件修改部分
- protobuf 生成C++文件
- 使用protobuf定义消息
- Sharepoint学习笔记—Ribbon系列-- 9.如何让用户定义的Ribbon引用外部Javascript文件
- SharePoint【Ribbon系列】-- 09.如何让用户定义的Ribbon引用外部Javascript文件
- linux平台下进行thrift和protobuf的IDL文件的编译缺少类库的解决办法
- 使用protobuf编写配置文件以及读写