資料交換格式:Protocol Buffer
Protocol Buffer(簡稱Protobuf或PB)是由Google推出的一種資料交換格式,與傳統的XML和JSON不同的是,它是一種二進位制格式,免去了文字格式轉換的各種困擾,並且轉換效率也是非常快,由於它的跨平臺、跨程式語言的特點,讓它越來越普及,尤其是網路資料交換方面日趨成為一種主流.
1.編譯原始碼,生成PB編譯器
編譯原始碼主要的目的就是在本地生成PB的編譯器,下載好protobuf-objectivec-3.0.0-alpha-4.tar.gz檔案,執行以下的命令進行編譯:
tar -xzvf protobuf-objectivec-3.0.0-alpha-4.tar.gz
cd protobuf-3.0.0-alpha-4
./configure
make
make check
sudo make install
如果編譯順利的話,便可以使用protoc命令了,以後便可以用這個命令將.proto檔案轉換為不同語言的原始碼檔案.
2.為工程新增PB依賴庫
需要在專案中使用ProtocolBuffer,需要將依賴庫新增到專案中來,剛才下載的原始碼objectivec目錄中已經包含一個可編譯libProtocolBuffers.a靜態庫的工程,直接引入工程即可,當然ProtocolBuffer專案也支援cocoapods的方式引入,在你的Podfile中新增:
platform :ios, '7.1'
pod "Protobuf", "~> 3.0.0-alpha-4"
3.使用PB編譯器編譯.proto檔案
為了驗證我們的ProtocolBuffer環境已經搭建好了,以下就來小試牛刀,建立類似以下的test.proto檔案:
message Person
{
required string name = 1; //姓名
required int32 sex = 2; //性別
required int32 age = 3; //年齡
}
然後通過以下的命令生成生成Model的原始碼檔案:
protoc --objc_out=./ ./test.proto
在當前目錄便可以看到Test.pbobjc.h和Test.pbobjc.m這兩個檔案了(需要注意的是生成的程式碼是MRC的,如果引入ARC工程中記得新增-fno-objc-arc的標籤).
4.在專案中使用PB完成序列化&反序列化
將上個步驟中生成的原始碼新增到工程之中,你就可以直接使用他們了,使用起來非常的方便,示例如下:
// 建立物件
Person *person = [Person new];
person.name = @"TanHao";
person.sex = 1;
person.age = 28;
// 序列化為Data
NSData *data = [person data];
// 反序列化為物件
Person *person2 = [Person parseFromData:data error:NULL];
NSLog(@"name:%@ sex:%d age:%d",person2.name,person2.sex,person2.age);
參考:http://www.tanhao.me/code/150911.html/
https://www.ibm.com/developerworks/cn/linux/l-cn-gpb/
https://developers.google.com/protocol-buffers/docs/reference/java-generated
http://www.jianshu.com/p/8c6c009bc500
相關文章
- Protocol Buffer技術詳解(資料編碼)Protocol
- TensorFlow中結構化資料工具Protocol BufferProtocol
- Protocol Buffer 使用指北Protocol
- TarsGo支援Protocol BufferGoProtocol
- TarsGo 支援 protocol bufferGoProtocol
- 解密gRPC: Protocol Buffer解密RPCProtocol
- iOS中使用Protocol BufferiOSProtocol
- Protocol Buffer Basics: C++ProtocolC++
- JSON資料交換格式有幾種?JSON
- protocol buffer 安裝與使用Protocol
- 谷歌將在 Kotlin 中支援 Protocol Buffers 資料格式谷歌KotlinProtocol
- protocol buffer的高效編碼方式Protocol
- Google Protocol Buffer 的使用和原理GoProtocol
- 在.NET使用JSON作為資料交換格式JSON
- Spring Cloud OpenFeign整合Protocol BufferSpringCloudProtocol
- Protocol Buffer序列化Java框架-ProtostuffProtocolJava框架
- Protocol buffer---Protobuf3開發指南Protocol
- Netty使用Google Protocol Buffer完成伺服器高效能資料傳輸NettyGoProtocol伺服器
- google protocol buffer——protobuf的編碼原理二GoProtocol
- 交換資料
- google protocol buffer——protobuf的基本使用和模型分析GoProtocol模型
- gRPC in ASP.NET Core 3.0 -- Protocol Buffer(1)RPCASP.NETProtocol
- Protocol Buffer技術詳解(語言規範)Protocol
- google protocol buffer——protobuf的使用特性及編碼原理GoProtocol
- google protocol buffer——protobuf的問題及改進一GoProtocol
- google protocol buffer——protobuf的問題和改進2GoProtocol
- 前端後臺以及遊戲中使用Google Protocol Buffer詳解前端遊戲GoProtocol
- Google Protocol buffer 學習筆記.下篇-動態編譯GoProtocol筆記編譯
- 【太極創客esp8266自學筆記】資料交換格式:JOSN筆記
- 數倉的兩種輕量級資料交換格式:json與jsonbJSON
- RabbitMQ訊息佇列的小夥伴: ProtoBuf(Google Protocol Buffer) [轉]MQ佇列GoProtocol
- SARIF:靜態分析結果交換格式
- 如何使用 Protobuf 做資料交換
- MysqlsqoopHDFS 資料交換實驗MySqlOOP
- 如何在 PHP 中處理 Protocol Buffers 資料PHPProtocol
- Buffer Cache Size(資料緩衝區)
- 前後端資料交換互動後端
- Tuxedo資料buffer基本型別介紹UX型別