首页 >> 民生新闻

Google推出ProtocolBuff节能

民生新闻  2020-11-19 03:10 字号: 大 中 小

在Web 2.0 时代,XML格式由于AJAX的风行以及RSS的普及而异军突起。不过随着Python和Ruby On Rails的走红,以及各种API的发布,YAML,JSON也逐渐成名。此次,Google推出了Protocol Buffers,是想让广大编程者方便地使用Google络传输数据的格式。

什么是Protocol Buffers?

这是Protocol Buffers主页上的一段代码:

message Person {

required string name = 1;

required int32 id = 2;

optional string email = 3;

enum PhoneType {

MOBILE = 0;

HOME = 1;

WORK = 2;

} message PhoneNumber {

required string number = 1;

optional PhoneType type = 2 [default = HOME];

}

repeated PhoneNumber phone = 4;

}

而Protocol Buffers的作用,就是将以上格式的数据类型,自动生成Java, Python, and C++的代码,然后以下一系列代码就可以直接调用了:(C++中)

Person person;

t_name("John Doe");

t_id(1234);

t_email("jdoe@");

fstream output("myfile", ios::out | ios::binary);

rializeToOstream(output); fstream input("myfile", ios::in | ios::binary);

Person person;

rseFromIstream(input);

cout "Name: " () endl;

cout "E-mail: " ail() endl;

相信所有C++编程者都为定义set,get之类的函数感到烦有64次“背靠背靠背”人过吧,而Google做的就是帮助你省去这些麻烦,构造更利于络传输的数据结构。

与XML的比较 优势 更简单 比XML小3到10倍体积 比XML快20到100倍 更不容易引起歧义 自动生成可编程的类代码

比较:

cout "Name: " () endl;

cout "E-mail: " ail() endl;

cout "Name: "

tElementsByTagName("name")-item(0)-innerText()

endl;

cout "E-mail: "

tElementsByTagName("email")就是通过一个叫定量阀的器件-item(0)-innerText()

endl; 劣势

没有层次,所以无法和HTML标记语言打交道 如果没有message的定义,根本无法知道message的意思,而XML是自解释型的。

Protocol Buffer主页 Protocol Buffer下载

黄石哪里治疗白癜风好
孩子不爱吃饭怎么调理
鹤壁专业白癜风治疗医院
推荐资讯