Protocol buffer

误发,未完成!本要保存到草稿箱,既然发了,先保留吧。

C++、C#、Dart、Go、Java和Python都可以使用Protocol buffer language。
https://developers.google.com/protocol-buffers/docs/proto
https://developers.google.com/protocol-buffers/docs/javatutorial

Specifying Field Rules

You specify that message fields are one of the following:

  • required: a well-formed message must have exactly one of this field.
    a value for the field must be provided, otherwise the message will be considered “uninitialized”. Trying to build an uninitialized message will throw a RuntimeException. Parsing an uninitialized message will throw an IOException. Other than this, a required field behaves exactly like an optional field.
  • optional: a well-formed message can have zero or one of this field (but not more than one).
    The field may or may not be set. If an optional field value isn’t set, a default value is used. For simple types, you can specify your own default value. Otherwise, a system default is used: zero for numeric types, the empty string for strings, false for bools. For embedded messages, the default value is always the “default instance” or “prototype” of the message, which has none of its fields set. Calling the accessor to get the value of an optional (or required) field which has not been explicitly set always returns that field’s default value.
  • repeated: this field can be repeated any number of times (including zero) in a well-formed message. The order of the repeated values will be preserved.Think of repeated fields as dynamically sized arrays.

reserved关键字:对于不想用的field,如果直接删掉或者注释,那么后续的user就可以使用这个field来更新Message type,一旦他们回退到了旧版本那么就会有问题。所以对于不再用的field,可以使用"reserved"关键字声明,这样后续的user就不能再使用它们。

可以在一个".proto"文件中定义多个"message type",但是最好不要定义太多。
可以在一个"message type"中定义Enum,也可以在message type外定义Enum,这样同一个".proto"文件中的其他"message type"也可以使用,还可以通过"MessageType.EnumType"的形式使用其他"message type"中定义的Enum。

Java tutorial

https://developers.google.com/protocol-buffers/docs/javatutorial

为什么要使用Protocol buffer language

当需要传递结构化的数据时(Structured data),比如联系人数据; Protocol buffer可以提供更具弹性的高效率自动化工具。我们在".proto"文件中按照语法要求定义好数据的结构,protocol buffer compiler会帮忙创建一个对应的class,这个class可以使用高效的二进制格式自动的编码和解码 protocol buffer data。生成的class会为构成protocol buffer的field提供setter和getter方法,还会负责将protocol buffer作为读写单元的细节。更重要的是,protocol buffer格式支持格式的扩展,而代码依然可以读使用旧格式的数据。

与其他方式的对比:

  • Java serialization, Java语言默认支持,所以使用起来很方便,但是开销很大,序列化和反序列化过程需要大量I/O操作; 而且和使用C++和Python等编写的application共享数据时不太合适。
  • 如果是比较简单的data,可以放进string中,这种方式简单灵活,但是辩解码的代码一般都是一次性的(对其他情况不适用),而且解析的时候会有些耗时。
  • 序列化到XML文件中; XML文件是可读的,而且很多开发语言都有相关的包,所以是应用间共享数据时一个好的选择。但是XML文件占用的空间可能会大些,而且解析的时候比较耗时; 另外,XML DOM tree也比较复杂。

“.proto"文件开头,要定义"package”,并为"java_package"赋值; “java_outer_classname"定义了编译成java后的类名字(包含了.proto文件中定义的所有类),如果没有定义,那么会使用proto文件的名字,例如"my_proto.proto"编译后会生成"MyProto.java”。

".proto"文件中尽量使用小写和下划线(lowercase-with-underscores),编译器会按照Java的命名规范“转译”。
不要对生成的class使用继承,如果需要可以wrap生成的类或者直接wrap ".proto"文件。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值