ProtoBuf常用序列化/反序列化API转-创新互联
http://blog.csdn.net/sealyao/article/details/69402451、C数组的序列化和反序列化API
成都创新互联长期为超过千家客户提供的网站建设服务,团队从业经验10年,关注不同地域、不同群体,并针对不同对象提供差异化的产品和服务;打造开放共赢平台,与合作伙伴共同营造健康的互联网生态环境。为淅川企业提供专业的
成都网站设计、网站制作,
淅川网站改版等技术服务。拥有十年丰富建站经验和众多成功案例,为您定制开发。
[cpp] view plaincopy
- //C数组的序列化和序列化API
- bool ParseFromArray(const void* data, int size);
- bool SerializeToArray(void* data, int size) const;
- //使用
- void set_people()
- {
- wp.set_name("sealyao");
- wp.set_id(123456);
- wp.set_email("sealyaog@gmail.com");
- wp.SerializeToArray(parray,256);
- }
- void get_people()
- {
- rap.ParseFromArray(parray,256);
- cout << "Get People from Array:" << endl;
- cout << " Name : " <
- cout << " Id : " << rap.id() << endl;
- cout << " email : " << rap.email() << endl;
- }
2、C++ String的序列化和反序列化API
[cpp] view plaincopy
- //C++string序列化和序列化API
- bool SerializeToString(string* output) const;
- bool ParseFromString(const string& data);
- //使用:
- void set_people()
- {
- wp.set_name("sealyao");
- wp.set_id(123456);
- wp.set_email("sealyaog@gmail.com");
- wp.SerializeToString(&pstring);
- }
- void get_people()
- {
- rsp.ParseFromString(pstring);
- cout << "Get People from String:" << endl;
- cout << " Name : " <
- cout << " Id : " << rsp.id() << endl;
- cout << " email : " << rsp.email() << endl;
- }
3、文件描述符序列化和反序列化API
[cpp] view plaincopy
- //文件描述符的序列化和序列化API
- bool SerializeToFileDescriptor(int file_descriptor) const;
- bool ParseFromFileDescriptor(int file_descriptor);
- //使用:
- void set_people()
- {
- fd = open(path,O_CREAT|O_TRUNC|O_RDWR,0644);
- if(fd <= 0){
- perror("open");
- exit(0);
- }
- wp.set_name("sealyaog");
- wp.set_id(123456);
- wp.set_email("sealyaog@gmail.com");
- wp.SerializeToFileDescriptor(fd);
- close(fd);
- }
- void get_people()
- {
- fd = open(path,O_RDONLY);
- if(fd <= 0){
- perror("open");
- exit(0);
- }
- rp.ParseFromFileDescriptor(fd);
- std::cout << "Get People from FD:" << endl;
- std::cout << " Name : " <
- std::cout << " Id : " << rp.id() << endl;
- std::cout << " email : " << rp.email() << endl;
- close(fd);
- }
4、C++ stream 序列化和反序列化API
[cpp] view plaincopy
- //C++ stream 序列化/反序列化API
- bool SerializeToOstream(ostream* output) const;
- bool ParseFromIstream(istream* input);
- //使用:
- void set_people()
- {
- fstream fs(path,ios::out|ios::trunc|ios::binary);
- wp.set_name("sealyaog");
- wp.set_id(123456);
- wp.set_email("sealyaog@gmail.com");
- wp.SerializeToOstream(&fs);
- fs.close();
- fs.clear();
- }
- void get_people()
- {
- fstream fs(path,ios::in|ios::binary);
- rp.ParseFromIstream(&fs);
- std::cout << " Name : " <
- std::cout << " Id : " << rp.id() << endl;
- std::cout << " email : " << rp.email() << endl;
- fs.close();
- fs.clear();
- }
网站题目:ProtoBuf常用序列化/反序列化API转-创新互联
本文链接:
http://dzwzjz.com/article/dcddep.html