本文作为结束篇,会稍微介绍下怎么反序列化GoogleBuffer数据,并在最后提供本系列文章中所用到的代码整理供下载。
上一篇文章介绍了怎样将数据序列化到了addressbook.data中,那么对于接受方而言该怎么解析出原本的数据呢。同样,protoc编译器生成的代码文件中提供了反序列化的接口,基本上和序列化的函数对应的,如下图所示:
上文中采用了SerializeToOstream、SerializeToString、SerializeToCodedStream来序列化数据的,反序列化反其道行之即可。本文反序列化采用ParseFromArray方式,从某个角度算是对上文的一个补充吧!
反序列化也是分为两个步骤:
1)将数据载入内存或者输入流
2)调用库提供的反序列化接口函数进行反序列化
一、将数据载入
将数据从文件中读出时候,需要注意以二进制的模式打开,且编码格式要指定正确,如下所示:
if ( NULL == g_AddressBook )
{
cerr << " Open addressbook.data failed!\n " << endl;
return ;
}
int lfilesize = 0 ;
fseek( g_AddressBook, 0 ,SEEK_END);
lfilesize = ftell( g_AddressBook );
fseek( g_AddressBook , 0 ,SEEK_SET );
char * buffer = new char [lfilesize + 1 ];
if ( NULL == buffer )
{
cerr << " malloc memory error!\n " ;
return ;
}
memset(buffer, ' \0 ' , sizeof (buffer));
fread( buffer, sizeof ( char ),lfilesize,g_AddressBook);
if ( g_AddressBook )
{
fclose(g_AddressBook);
g_AddressBook = NULL;
}
二、反序列化
上述代码将addressbook.data中的数据载入了buffer中,接着我们就可以将其作为参数传给ParseFromArray来反序列化,并格式化输出到控制台,如下:
addressBook.par
addressBook.Clear();
if ( ! addressBook.ParseFromArray(buffer,lfilesize) )
{
cerr << " Deserial from addressbook.data failed!\n " ;
return ;
}
int personSize = addressBook.person_size();
for ( int i = 0 ;i < personSize; i ++ )
{
Person p = addressBook.person( i );
cout << " Person " << i + 1 << " :\nid\t " << p.id() << " \nname:\t " << p.name() << " \n " ;
int phoneSize = p.phone_size();
for ( int j = 0 ;j < phoneSize;j ++ )
{
Person_PhoneNumber phone = p.phone(j);
cout << " Phone " << j + 1 << " :\nType:\t " ;
switch ( phone.type())
{
case Person_PhoneType_MOBILE:
cout << " Mobile\t\tPhone Number:\t " << phone.number() << endl;
break ;
case Person_PhoneType_HOME:
cout << " Home\t\tPhone Number:\t " << phone.number() << endl;
break ;
case Person_PhoneType_WORK:
cout << " Work\t\tPhone Number:\t " << phone.number() << endl;
break ;
default :
cout << " Unkown\n " ;
break ;
}
}
cout << endl;
}
运行结果如下所示:
好了,相信通过本系列文章,读者应该对Google Protocol Buffer有一定的认识了吧。当然,想要更深入的了解,还是参考Google的官方在线文档吧!
示例代码下载地址:SerialProtocolBuffer示例代码
欢迎转载,转载时请务必保留原文出处:http://www.cnblogs.com/royenhome ,谢谢合作!