-
Protocol Buffers
鎖定
Protocol Buffers作用
、Ruby、PHP、JavaScript八種編程語言,還可以找到大量的幾乎涵蓋所有語言的第三方拓展包。
通過它,你可以定義你的數據的結構,並生成基於各種語言的代碼。這些你定義的數據流可以輕鬆地在傳遞並不破壞你已有的程序。並且你也可以更新這些數據而現有的程序也不會受到任何的影響。
Protocol Buffers經常被簡稱為protobuf。
Protocol Buffers樣例
通過定義一些數據和結構放在一個 .proto 文件之中。每一個protocol buffer 信息都是一小段結構,包含了一些名字。下面是一個.proto 文件內容的樣例。
[pre]message Point {
required int32 x = 1;
required int32 y = 2;
optional string label = 3;
}
message Line {
required Point start = 1;
required Point end = 2;
optional string label = 3;
}
message Polyline {
repeated Point point = 1;
optional string label = 2;
}
Protocol Buffers優點
為什麼不使用XML?
1. 更簡單
2. 數據描述文件只需原來的1/10至1/3
3. 解析速度是原來的20倍至100倍
4. 減少了二義性
5. 生成了更容易在編程中使用的數據訪問類
6、支持多種編程語言
舉例
我們可以舉出例子對這兩者進行比較。
XML
<person>
<name>John Doe</name>
<email>jdoe@</email>
</person>
Protocol buffers
message person { name: "John Doe" ; email: "jdoe@"}
Protocol Buffers應用
google在2008年7月7號將其作為開源項目對外公佈。