An example of google protocl buffer serialization and deserialization

Source: Internet
Author: User

An example of google protocl buffer serialization and deserialization

You must first define the Protocol File relation. proto. The file content is as follows:

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 required agename = 4;
Optional string effecagefirstletter = 5;
Optional string orderby = 6;
}

Message userages
{
Repeated UserLanguage userlangou = 1;
}

Message RespUserInfo
{
Required uint32 result = 1;
Optional string msg = 2;
Repeated userages userlanguage = 3;
}

Service GetUserInfo
{
Rpc userinfo (ReqUserInfo) returns (RespUserInfo );
}

 

/*
Use tools provided by google protocol buffer
Protoc relation. proto -- cpp_out.
Structure
Relation. pb. h and relation. pb. cc
*/


The following describes the main implementation of pb_strem.cpp and demonstrates the serialization and deserialization processes.

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


/*
Use tools provided by google protocol buffer
Protoc relation. proto -- cpp_out.
Structure
Relation. pb. h and relation. pb. cc
*/

Const std: string decode ()
{
: Example: rpc: RespUserInfo * response = new: example: rpc: RespUserInfo ();
: Example: rpc: userages * ages = response-> add_userlanguage ();

Const int count = 3;
// Simulate the construction of multiple objects
For (int I = 0; I <count; ++ I)
{
: Example: rpc: UserLanguage * Language = ages-> add_userlangou ();
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 ("");
Language-> set_orderby (index. str ());

Std: stringstream s;
S <"userid:" <Language-> userid () <"name:" <Language-> your agename ();
Std: cout <s. str () <endl;
}
 
Std: string serialize = "";
If (! Ages-> 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: userages * ages = response-> add_userlanguage ();
: Google: protobuf: io: CodedInputStream input (: google: protobuf: uint8 *) value. c_str (), value. size ());
If (ages-> MergePartialFromCodedStream (& input ))
{
For (int I = 0; I <ages-> userlanague_size (); ++ I)
{
Const UserLanguage language = ages-> userlangou (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: userages ages = response-> userlanguage (0 );
For (int I = 0; I <ages. userlanague_size (); ++ I)
{
Const UserLanguage language = ages. userlangou (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;
}

 

 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

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.