google protocl buffer 序列化和還原序列化的一個例子
需要先定義協議檔案 relation.proto,檔案內容如下:
package mooon.rpc;
option cc_generic_services = true;
message ReqUserInfo
{
required uint32 userid = 1;
required string mask = 2;
}
message UserLanguage
{
required uint32 userid = 1;
optional string languageid = 2;
optional string createdate = 3;
optional string languagename = 4;
optional string languagefirstletter = 5;
optional string orderby = 6;
}
message UserLanguages
{
repeated UserLanguage userlanague = 1;
}
message RespUserInfo
{
required uint32 result = 1;
optional string msg = 2;
repeated UserLanguages userlanguage = 3;
}
service GetUserInfo
{
rpc userinfo (ReqUserInfo) returns (RespUserInfo);
}
/*
使用 google protocol buffer 提供的工具
protoc relation.proto --cpp_out .
構造
relation.pb.h 和 relation.pb.cc
*/
下面是pb_strem.cpp的主要實現,示範了序列化和還原序列化的過程
#include <sstream>
#include <iostream>
#include <google/protobuf/io/coded_stream.h>
#include <google/protobuf/wire_format_lite_inl.h>
#include <google/protobuf/descriptor.h>
#include <google/protobuf/io/zero_copy_stream_impl.h>
#include <google/protobuf/io/zero_copy_stream.h>
#include <google/protobuf/io/coded_stream.h>
#include <google/protobuf/stubs/common.h>
#include <google/protobuf/message_lite.h>
#include <google/protobuf/message.h>
#include "relation.pb.h"
using namespace std;
using namespace example;
using namespace example::rpc;
using namespace::google::protobuf::io;
/*
使用 google protocol buffer 提供的工具
protoc relation.proto --cpp_out .
構造
relation.pb.h 和 relation.pb.cc
*/
const std::string decode()
{
::example::rpc::RespUserInfo* response = new ::example::rpc::RespUserInfo();
::example::rpc::UserLanguages *Languages = response->add_userlanguage();
const int count = 3;
//類比構造多個對象
for(int i = 0;i < count;++i)
{
::example::rpc::UserLanguage *Language = Languages->add_userlanague();
Language->set_userid(i);
std::stringstream index;
index<<i;
Language->set_languageid("ID_" + index.str());
Language->set_createdate("date");
Language->set_languagename("name" + index.str());
Language->set_languagefirstletter("A");
Language->set_orderby(index.str());
std::stringstream s;
s<<"userid:"<<Language->userid()<<" name:"<<Language->languagename();
std::cout<<s.str()<<endl;
}
std::string serialize = "";
if(!Languages->SerializePartialToString(&serialize))
{
serialize = "";
std::cout<<"SerializePartialToString fail"<<endl;
}
delete response;
response = NULL;
return serialize;
}
void encode(const std::string &value)
{
::example::rpc::RespUserInfo* response = new ::example::rpc::RespUserInfo();
::example::rpc::UserLanguages *Languages = response->add_userlanguage();
::google::protobuf::io::CodedInputStream input((::google::protobuf::uint8*)value.c_str(),value.size());
if(Languages->MergePartialFromCodedStream(&input))
{
for(int i = 0;i < Languages->userlanague_size();++i)
{
const UserLanguage language = Languages->userlanague(i);
std::stringstream s;
s<<"userid:"<<language.userid()<<" name:"<<language.languagename();
std::cout<<s.str()<<endl;
}
}
else
{
std::cout<<"MergePartialFromCodedStream fail"<<endl;
}
std::cout<<"***************************"<<endl;
::example::rpc::UserLanguages languages = response->userlanguage(0);
for(int i = 0;i < languages.userlanague_size();++i)
{
const UserLanguage language = languages.userlanague(i);
std::stringstream s;
s<<"userid:"<<language.userid()<<" name:"<<language.languagename();
std::cout<<s.str()<<endl;
}
delete response;
response = NULL;
}
int _tmain(int argc, _TCHAR* argv[])
{
std::string s = decode();
std::cout<<"***************************"<<endl;
encode(s);
::system("pause");
return 0;
}