ProtoBuf 常用序列化/反序列化API 转
标签:des blog http 使用 strong 文件
http://blog.csdn.net/sealyao/article/details/6940245
1、C数组的序列化和反序列化API
- //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:"
- cout "\t Name : "
- cout "\t Id : "
- cout "\t email : "
- }
2、C++ String的序列化和反序列化API
- //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:"
- cout "\t Name : "
- cout "\t Id : "
- cout "\t email : "
- }
3、文件描述符序列化和反序列化API
- //文件描述符的序列化和序列化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
- 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
- perror("open");
- exit(0);
- }
- rp.ParseFromFileDescriptor(fd);
- std::cout "Get People from FD:"
- std::cout "\t Name : "
- std::cout "\t Id : "
- std::cout "\t email : "
- close(fd);
- }
4、C++ stream 序列化和反序列化API
- //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 "\t Name : "
- std::cout "\t Id : "
- std::cout "\t email : "
- fs.close();
- fs.clear();
- }
-
ProtoBuf 常用序列化/反序列化API 转,搜素材,soscw.com
ProtoBuf 常用序列化/反序列化API 转
标签:des blog http 使用 strong 文件
原文地址:http://www.cnblogs.com/huashiyiqike/p/3833607.html
评论