XML (eXtensible Markup Language)
- 目的是為了能在網際網路上傳送與處理文件作資料交換用。
- XML本身是一種 meta-language 也就是說透過XML可以用來創作新的語言,像說XHTML、WSDL、SOAP…等。
- XML著重在資料的描述與結構,多年前分散式架構崛起時透過XML在網路上交換資料是很方便。
- 後來發現有時只為了傳送很少的資料,使用XML時實際交換資料的記憶體空間,會浪費在XML標籤上。
•舉例來說今天只要傳含有2個數值的陣列,其實只是要傳送0、1其中只有 2 個 bytes是實際要交換的資料,JSON則是用來解決這問題。
<array>
<item index='0'>0</item>
<item index='1'>1</item>
</array>
<array>
<item index='0'>0</item>
<item index='1'>1</item>
</array>
- JSON是一種輕量級且可讀性高的資料轉換中介格式,已成為IETF RFC4627規範內容。
- JSON很適合應用在輕量級的網頁應用,因為JSON是以 Javascript的ECMA-262 3rd Edition的標準為基準,所以基本上就是Javascript的子集合。
- JSON與XML差異
- JSON與XML最大的不同在於XML是一個完整的標記語言,而JSON不是。
- XML利用標記語言的特性提供了絕佳的延展性,在資料儲存、擴充功能及高階檢索方面。
- XML在程式判讀上需要比較多的功夫。主要的原因在於XML的設計理念與JSON不同。
- JSON則由於比XML更加小巧,以及瀏覽器的內建快速解析支援,使得其更適用於網路資料傳輸領域。
- JSON Example
JSON 就是 JavaScript 的變數定義語法。例如下例的 JavaScript 程式碼,定義了一個字串 myString 、一個陣列變數 myArray 、一個物件變數 myObject。
var myString = "hello";
var myArray = [0,1,2,3,4,5,6,7,8,9,10];
var myObject = {"name": "john", "title":"nothing"};
var JSONmyArray = '[0,1]'; // put an array by JSON format.
var myArray = eval('(' + JSONmyArray + ')'); //利用eval()來將JSON字串轉成物件
window.alert('Value of myArray[0] is ' + myArray[0]);
XML是一種很好的表示方法,不過有時會顯得囉唆,JSON就是用來簡化這種表示法,用較少的字元達到相同效果。
Google Protocol Buffer
- Protocl Buffer是一種序列化表示資料靈活、高效能的自動化方法。類似於XML但表示更簡潔、執行速度更快、書寫更簡單。
- 資料可以只定義一次,然後使用Protocl Buffer產生的程式碼,在多場場合使用多種的語言非常容易地存取定義的資料。
- Protocl Buffer是目前Google內遠端使用的資訊傳遞的事實標準,在Google整個程式碼中,定義了超過12183個Protocl Buffer檔的48162條訊息,廣泛用於遠端使用的RPC系統和許多儲存系統的持久化的資料定義中。
為什麼不使用XML? Google使用新開發的Protocol Buffer來進行資料的表示,在序列化、結構化資料有很多明顯的優點:
- 更簡單
- 只有XML形式大小的1/3~1/10
- 快20~100倍
- 更清晰
- 能產生更容易進行程式設計的資料存取程式碼。
You write a .proto file like this:
message Person {
Then you compile it with protoc, the protocol buffer compiler, to
produce code in C++, Java, or Python.
message Person {
required int32 id = 1; // required必選類型
required string name = 2;
optional string email = 3; // optional 可選類型}
Then you compile it with protoc, the protocol buffer compiler, to
produce code in C++, Java, or Python.
Then, if you are using C++, you use that code like this:
Person person;
person.set_id(123);
person.set_name("Bob");
person.set_email("bob@example.com");
fstream out("person.pb", ios::out | ios::binary | ios::trunc);
person.SerializeToOstream(&out);
out.close();
Or like this:
Person person;
person.set_id(123);
person.set_name("Bob");
person.set_email("bob@example.com");
fstream out("person.pb", ios::out | ios::binary | ios::trunc);
person.SerializeToOstream(&out);
out.close();
Or like this:
Person person;
fstream in("person.pb", ios::in | ios::binary);
cout << "ID: " << person.id() << endl;
cout << "name: " << person.name() << endl;
if (person.has_email()) {
fstream in("person.pb", ios::in | ios::binary);
if (!person.ParseFromIstream(&in)) {
cerr << "Failed to parse person.pb." << endl;
}exit(1);
cout << "ID: " << person.id() << endl;
cout << "name: " << person.name() << endl;
if (person.has_email()) {
cout << "e-mail: " << person.email() << endl;}
- 缺點
- XML相比雖然功能簡單卻無法表達複雜的概念。
- XML已經成為多種領域標準的編寫工具,Protobuf只是Google公司內部使用的工具,在通用性上還差很多。
- XML易於閱讀和理解可以被人直接讀取編輯,在這一點上Protobuf不行,它以二進制的方式儲存,除非你有.proto定義檔,否則你沒法直接讀出 Protobuf的任何內容。
沒有留言:
張貼留言