Google推出Protocol Buffers:爭奪網(wǎng)絡(luò)時代數(shù)據(jù)格式
北京時間7月8日消息,據(jù)國外媒體報道,谷歌本周一發(fā)布了該公司內(nèi)部使用的開放源代碼數(shù)據(jù)描述語言Protocol Buffers。Protocol Buffers與XML相似,但更簡單、更小、更快。
谷歌開放源代碼項(xiàng)目經(jīng)理克里斯·迪邦納(Chris DiBona)在一篇博文中寫道,“我們在網(wǎng)絡(luò)上傳輸或在磁盤上存儲的幾乎所有結(jié)構(gòu)化信息都采用了這種語言。我們認(rèn)為Protocol Buffers可能對其他人也有用,因此我們決定將它發(fā)布為開放源代碼軟件。”
谷歌軟件工程師肯頓·瓦爾達(dá)(Kenton Varda)在公司的開放源代碼博客上發(fā)表文章稱,谷歌使用數(shù)千種不同的數(shù)據(jù)格式,其中大多數(shù)都是結(jié)構(gòu)化數(shù)據(jù)格式。XML無法勝任對這些海量結(jié)構(gòu)化數(shù)據(jù)編碼的重任,谷歌于是開發(fā)了Protocol Buffers。
瓦爾達(dá)將Protocol Buffers比作是一種界面描述語言,但沒有界面描述語言的復(fù)雜性。他說,Protocol Buffers的主要設(shè)計目標(biāo)之一是簡潔。對Protocol Buffers進(jìn)行解析的速度也很快,比XML要快出至少一個量級。
谷歌的文檔稱,與具有可比性的XML文件相比,Protocol Buffers文件的尺寸要小3-10倍,解析速度要快20-100倍。
谷歌發(fā)布的免費(fèi)文件包括采用Java、Python和C++編程語言編寫的Protocol Buffers編譯器源代碼。
谷歌在一份文檔中表示,該公司還計劃將許多其它軟件項(xiàng)目發(fā)布為開放源代碼軟件。因?yàn)檫@些項(xiàng)目會用到Protocol Buffers,因此谷歌決定首先將它發(fā)布為開放源代碼軟件。
在Web 2.0 時代,XML格式由于AJAX的風(fēng)行以及RSS的普及而異軍突起。不過隨著Python和Ruby On Rails的走紅,以及各種API的發(fā)布,YAML,JSON也逐漸成名。此次,Google推出了Protocol Buffers,是想讓廣大編程者方便地使用Google網(wǎng)絡(luò)傳輸數(shù)據(jù)的格式。
什么是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的作用,就是將以上格式的數(shù)據(jù)類型,自動生成Java, Python, and C++的代碼,然后以下一系列代碼就可以直接調(diào)用了:(C++中)
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); fstream input("myfile", ios::in | ios::binary);
Person person;
person.ParseFromIstream(&input);
cout << "Name: " << person.name() << endl;
cout << "E-mail: " << person.email() << endl;
相信所有C++編程者都為定義set,get之類的函數(shù)感到煩人過吧,而Google做的就是幫助你省去這些麻煩,構(gòu)造更利于網(wǎng)絡(luò)傳輸?shù)臄?shù)據(jù)結(jié)構(gòu)。
與XML的比較 優(yōu)勢
比較:
cout << "Name: " << person.name() << endl;
cout << "E-mail: " << person.email() << endl;
cout << "Name: "
<< person.getElementsByTagName("name")->item(0)->innerText()
<< endl;
cout << "E-mail: "
<< person.getElementsByTagName("email")->item(0)->innerText()
<< endl; 劣勢
Protocol Buffer主頁 Protocol Buffer下載
出處:http://mjgforever.cnblogs.com/
本文版權(quán)歸作者和博客園共有,歡迎轉(zhuǎn)載,但未經(jīng)作者同意必須保留此段聲明。
posted on 2008-07-10 08:23 mjgforever 閱讀(525) 評論(0) 收藏 舉報
浙公網(wǎng)安備 33010602011771號