阅读 140

protobuf官网,protobuffer更好

1、什么是protocol buffers

2、protocol buffers的工作流程

3、protobuffer和xml、json的区别

1、protocol buffers protocol buffers是一种灵活、高效、自动化的用于序列化结构化数据的协议,与json和xml相比,protocol buffers序列化的码流较小,速度更快如果一次(使用3358www.Sina.com/文件的定义)序列化的数据结构的定义,即使使用不同语言) protobuf提供的生成工具.proto,也可以使用不同的数据流这意味着更新. proto文件的内容。

安装方法如下。

首先从GitHub下载相应版本的protobuffer并解压缩,然后执行以下操作:

$ cdprotobuf-version $./configure $ make $ makecheck $ make install 2、protocol buffers工作流程protoc个proproce

消息人员{ requiredstringname=1; required int32 id=2; 选项字符串电子邮件=3; enum PhoneType { MOBILE=0; 主页=1; 工作=2; } message phone number { requiredstringnumber=1; optionalphonetypetype=2[ default=home ]; } repeated PhoneNumber phone=4; } (1)首先,你需要通过在.proto文件中定义protocol buffer的message类型来指定你想要序列化的数据结构:

(2)message数据格式中需要知道的:必须赋值,且不能为空。 否则,此消息将被视为“统一”。 build「uninitialized”消息抛出运行时间扩展异常,而分析“uninitialized”消息时抛出IOException异常。 除此之外," required "字段和" optional "字段没有差别。

required:字段可以赋值,也可以不赋值。 如果未分配值,则会分配默认值。

optional:此字段可以重复任何次数,包括0次。 重复数据的顺序保存在协议缓冲器中。 可以将此字段视为可以自动设置size的数组。

protobuffer支持的数据类型如下:

. prototypenotesctypejavatypepythontype [2] doubledoubledoublefloatfloatfloatfloatint 32使用可变长度编码,并且可能具有负值。 如果你的域有负值,请用sint32代替int32intint64使用可变长度编码。 对于负值的效率低,如果域可能具有负值,则使用sint64代替int64长/长[3] uint 32,使用可变长度编码uint 32 int [1] int /长[3] uint 64 int64longint/long[3]fixed32始终为4个字节,如果数值始终大于228,则该类型比uint32更高效

。uint32int[1]intfixed64总是8个字节,如果数值总是比总是比256大的话,这个类型会比uint64高效。uint64long[1]int/long[3]sfixed32总是4字节int32intintsfixed64总是8字节int64longint/long[3]bool boolbooleanbooleanstring一个字符串必须是UTF-8编码或者7-bit ASCII编码的文本。stringStringstr/unicode[4]bytes可能包含任意顺序的字节数据。stringByteStringstr

   (3)一旦定义了你的message,你就可以根据你所使用的语言(如JAVA、C++、Python等)使用protocol buffer提供的编译工具protoc来编译.proto文件生成数据访问类。这些类为每一个字段都提供了简单的访问器(比如name()和set_name()),同时还提供了将整个结构化数据序列化为原始字节数据以及从原始字节数据反序列化为结构化数据的方法(C++中称之为函数)。例如,如果你使用的语言是C++,运行编译器编译一个message的实例如下:

//xxx.protomessage Order{ required uint64 uid = 1; required float cost = 2; optional string tag = 3;} //xxx.pb.h<pre name="code" class="cpp">class Order : public ::google::protobuf::Message { public: ... // accessors ------------------------------------------------------- // required uint64 uid = 1;  inline bool has_uid() const;  inline void clear_uid();  static const int kUidFieldNumber = 1;  inline ::google::protobuf::uint64 uid() const;  inline void set_uid(::google::protobuf::uint64 value);   // required float cost = 2;  inline bool has_cost() const;  inline void clear_cost();  static const int kCostFieldNumber = 2;  inline float cost() const;  inline void set_cost(float value);   // optional string tag = 3;  inline bool has_tag() const;  inline void clear_tag();  static const int kTagFieldNumber = 3;  inline const ::std::string& tag() const;  inline void set_tag(const ::std::string& value);  inline void set_tag(const char* value);  inline void set_tag(const char* value, size_t size);  inline ::std::string* mutable_tag();  inline ::std::string* release_tag();  inline void set_allocated_tag(::std::string* tag);   // @@protoc_insertion_point(class_scope:Order) private:  inline void set_has_uid();  inline void clear_has_uid();  inline void set_has_cost();  inline void clear_has_cost();  inline void set_has_tag();  inline void clear_has_tag(); ::google::protobuf::uint32 _has_bits_[1];   ::google::protobuf::uint64 uid_;  ::std::string* tag_;  float cost_;}; 对于每一个message的data member,protobuf会自动生成相关的处理函数,对于每一个字段主要的处理函数有:has_uid(), clear_uid(), uid(), set_uid(),它们分别用于判断该字段是否被设置,清除该字段设置记录,获得该字段,设置该字段。

  (4)针对上述例子中的person类,之后你可能会写下如下类似的代码(序列化):

Person person;person.set_name("John Doe");person.set_id(1234);person.set_email("jdoe@example.com");fstream output("myfile", ios::out | ios::binary);person.SerializeToOstream(&output);

    之后,你可以将你的message读回(译注:反序列化): 

fstream input("myfile", ios::in | ios::binary);Person person;person.ParseFromIstream(&input);cout << "Name: " << person.name() << endl;cout << "E-mail: " << person.email() << endl; 3、protobuffer和xml、json的区别

  (1)protobuffer和xml

     相对于XML,protocol buffers在序列化结构数据时拥有许多先进的特性:
      1)更简单
      2)序列化后字节占用空间比XML少3-10倍
      3)序列化的时间效率比XML快20-100倍
      4)具有更少的歧义性
      5)自动生成数据访问类方便应用程序的使用

     事物总有两面性,和XML相比protocol buffers并不总是更好的选择,例如:
     1)protocol buffers并不适合用来描述一个基于文本的标记型文档(比如HTML),因为你无法轻易的交错文本的结构。
     2)另外,XML具有很好的可读性和可编辑性;而protocol buffers,至少在它们的原生形式上并不具备这个特点。
     3)XML同时也是可扩展、自描述的。而一个protocol buffer只有在具有message 定义(在.proto文件中定义)时才会有意义。

  (2)protobuffer和json

      1)JSON因为有一定的格式,并且是以字符存在的,在数据量上还有可以压缩的空间。protobuf是二进制的、结构化的,所以比json的数据量更小,也更对象化 ;
      2)protobuf不是像json直接明文的,这个是定义对象结构,然后由protbuf库去把对象自动转换成二进制,用的时候再自动反解过来的。传输对我们是透明的!我们只管传输的对象就可以了。

    总的来说,前端和后端交互会采用json,而后端各个模块的交互,你可以随便选择;对于HTTP协议的交互,用的比较多的是json,而 tcp协议,用的比较多的是protobuffer。

参考:https://www.cnblogs.com/chenyangyao/p/5422044.html


文章分类
代码人生
版权声明:本站是系统测试站点,无实际运营。本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容, 请发送邮件至 XXXXXXo@163.com 举报,一经查实,本站将立刻删除。
相关推荐