ProtoBuf 常用序列化/反序列化API

转自:http://blog.csdn.net/sealyao/article/details/6940245


1、C数组的序列化和反序列化API

[cpp]  view plain  copy
  1. //C数组的序列化和序列化API  
  2. bool ParseFromArray(const void* data, int size);  
  3. bool SerializeToArray(void* data, int size) const;  
  4. //使用  
  5. void set_people()               
  6. {  
  7.     wp.set_name("sealyao");     
  8.     wp.set_id(123456);          
  9.     wp.set_email("sealyaog@gmail.com");  
  10.     wp.SerializeToArray(parray,256);  
  11. }  
  12.   
  13. void get_people()               
  14. {  
  15.     rap.ParseFromArray(parray,256);  
  16.     cout << "Get People from Array:" << endl;  
  17.     cout << "\t Name : " <<rap.name() << endl;  
  18.     cout << "\t Id : " << rap.id() << endl;  
  19.     cout << "\t email : " << rap.email() << endl;  
  20. }  


2、C++ String的序列化和反序列化API

[cpp]  view plain  copy
  1. //C++string序列化和序列化API  
  2. bool SerializeToString(string* output) const;  
  3. bool ParseFromString(const string& data);  
  4. //使用:  
  5. void set_people()               
  6. {  
  7.     wp.set_name("sealyao");     
  8.     wp.set_id(123456);          
  9.     wp.set_email("sealyaog@gmail.com");  
  10.     wp.SerializeToString(&pstring);  
  11. }  
  12.   
  13. void get_people()               
  14. {  
  15.     rsp.ParseFromString(pstring);    
  16.     cout << "Get People from String:" << endl;  
  17.     cout << "\t Name : " <<rsp.name() << endl;  
  18.     cout << "\t Id : " << rsp.id() << endl;  
  19.     cout << "\t email : " << rsp.email() << endl;  
  20. }  

3、文件描述符序列化和反序列化API

[cpp]  view plain  copy
  1.  //文件描述符的序列化和序列化API  
  2.  bool SerializeToFileDescriptor(int file_descriptor) const;  
  3.  bool ParseFromFileDescriptor(int file_descriptor);  
  4.   
  5.  //使用:  
  6. void set_people()  
  7. {  
  8.     fd = open(path,O_CREAT|O_TRUNC|O_RDWR,0644);  
  9.     if(fd <= 0){  
  10.         perror("open");  
  11.         exit(0);   
  12.     }     
  13.     wp.set_name("sealyaog");  
  14.     wp.set_id(123456);  
  15.     wp.set_email("sealyaog@gmail.com");  
  16.     wp.SerializeToFileDescriptor(fd);     
  17.     close(fd);  
  18. }  
  19.   
  20. void get_people()  
  21. {  
  22.     fd = open(path,O_RDONLY);  
  23.     if(fd <= 0){  
  24.         perror("open");  
  25.         exit(0);  
  26.     }  
  27.     rp.ParseFromFileDescriptor(fd);  
  28.     std::cout << "Get People from FD:" << endl;  
  29.     std::cout << "\t Name : " <<rp.name() << endl;  
  30.     std::cout << "\t Id : " << rp.id() << endl;  
  31.     std::cout << "\t email : " << rp.email() << endl;  
  32.     close(fd);  
  33. }  

4、C++  stream 序列化和反序列化API

[cpp]  view plain  copy
  1. //C++ stream 序列化/反序列化API  
  2. bool SerializeToOstream(ostream* output) const;  
  3. bool ParseFromIstream(istream* input);  
  4.   
  5. //使用:  
  6. void set_people()  
  7. {  
  8.     fstream fs(path,ios::out|ios::trunc|ios::binary);  
  9.     wp.set_name("sealyaog");  
  10.     wp.set_id(123456);  
  11.     wp.set_email("sealyaog@gmail.com");  
  12.     wp.SerializeToOstream(&fs);      
  13.     fs.close();  
  14.     fs.clear();  
  15. }  
  16.   
  17. void get_people()  
  18. {  
  19.     fstream fs(path,ios::in|ios::binary);  
  20.     rp.ParseFromIstream(&fs);  
  21.     std::cout << "\t Name : " <<rp.name() << endl;  
  22.     std::cout << "\t Id : " << rp.id() << endl;   
  23.     std::cout << "\t email : " << rp.email() << endl;     
  24.     fs.close();  
  25.     fs.clear();  
  26. }  
  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值