Google Protocol Buffers 语法整理
来源:互联网 发布:淘宝卖家分销平台 编辑:程序博客网 时间:2024/06/05 20:38
1、简单定义一个Message类型
Protocol Buffers 语法文件以 .proto
为文件扩展名。在版本proto3文件头需要包含版本类型 syntax = "proto3";
缺省情况为proto2类型。
/*message 为类标识符SearchRequest 为类名string/int32 为字段类型query/page_number/result_per_page 为字段名称1/2/3 为编号标签,用于序列化后内容标识序号1~15需要1个字节,16~2047需要2个字节最小编号为1,最大编号229-1或536870911。19000~19999系统预留,不可用。*///官方示例syntax = "proto3";message SearchRequest { string query = 1; int32 page_number = 2; int32 result_per_page = 3;}
2、字段描述规则
字段的规则主要分为单数和复数,用于描述字段是否是单个字段还是数组。默认情况下字段定义都是单数 singular
,如果需要定义数组需要在字段类型前面加 repeated
。
3、添加注释
可以在字段定义结束后加 //
注释,该注释会在生成的字段代码中。
//官方示例:message SearchRequest { string query = 1; int32 page_number = 2; // Which page number do we want? int32 result_per_page = 3; // Number of results to return per page.}
4、保留字段
编号定义后,无法更改,如果需要重新定义类型或删除旧的类型,需要在序列化文件中占位原定义的类型。这样才能保证更改后数据正常反序列化,并对以前数据兼容。否则数据格式不正确后,将导致对更改前的数据反序列化出错.
//官方示例:message Foo { reserved 2, 15, 9 to 11; reserved "foo", "bar";}
参考链接
[译]Protobuf 语法指南
Protocol Buffers 3.0 技术手册
阅读Google Protocol Buffers 指南
阅读全文
0 0
- Google Protocol Buffers 语法整理
- (Google)Protocol Buffers概览
- Google Protocol Buffers详解
- Google Protocol Buffers
- Google Protocol Buffers安装
- GOOGLE PROTOCOL BUFFERS
- Google Protocol Buffers浅析
- Google protocol buffers 小结
- Google Protocol Buffers浅析
- Google Protocol Buffers 概述
- Google Protocol Buffers 入门
- Google Protocol Buffers 入门
- Google Protocol Buffers 概述
- Google Protocol Buffers
- Google Protocol Buffers 概述
- Google Protocol Buffers介绍
- Google Protocol Buffers 入门
- Google Protocol Buffers浅析
- pandas与 xlsxwriter
- 1078. 字符串压缩与解压 (20) PAT乙级真题
- sql语句中多个union all的case when优化
- flume的拓扑结构
- 最适合初学者的 IntelliJ IDEA 教程
- Google Protocol Buffers 语法整理
- android长连接心跳机制
- windows下创建/删除文件夹(C/C++ python Matlab)
- iOS 数组~把创建的字典添加到数组中
- assert定义和用法
- shell命令-dd
- Ubuntu下opencv3.3和opencv_contrib的编译安装
- 大数据和云计算技术周报(第3期)
- ORACLE追踪应用执行sql语句——10046事件