google protocl buffer 序列化和還原序列化的一個例子

來源:互聯網
上載者:User

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;
}

 

 

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.