本文整理汇总了C++中Person::SerializeToString方法的典型用法代码示例。如果您正苦于以下问题:C++ Person::SerializeToString方法的具体用法?C++ Person::SerializeToString怎么用?C++ Person::SerializeToString使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类Person
的用法示例。
在下文中一共展示了Person::SerializeToString方法的4个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C++代码示例。
示例1: main
int main()
{
using namespace x;
Person p;
p.set_name("tom");
p.set_id(88);
p.set_email("[email protected]");
std::string str;
p.SerializeToString(&str);//将对象序列化到字符串,除此外还可以序列化到fstream等
printf("%s\n", str.c_str());
Person x;
x.ParseFromString(str);//从字符串反序列化
printf("x.name=%s\n", x.name().c_str());//这里的输出将是tom,说明反序列化正确
return 0;
}
示例2: main
int main()
{
Person p;
p.set_name("tom");
p.set_id(88);
p.set_email("[email protected]");
std::string str;
p.SerializeToString(&str);
printf("%s\n", str.c_str());
Person x;
x.ParseFromString(str);
printf("x.name = %s\n", x.name().c_str());
printf("x.email= %s\n", x.email().c_str());
return 0;
}
示例3: makeRequest
Request makeRequest(Request_Header_Type type, const Id &id, const
Person &person, bool overwrite, const std::string &trace) {
TRACE(std::cout, "");
Request request;
Request_Header *hdr = request.mutable_header();
if (!trace.empty() && trace.size()) {
hdr->set_trace(trace);
}
hdr->set_datastoredriverid(PERSON_STORE);
Request_Payload *pyld = request.mutable_payload();
std::string k;
id.SerializeToString(&k);
pyld->set_key(k);
switch (type) {
case Request_Header_Type_LOOKUP:
{
hdr->set_type(Request_Header_Type_LOOKUP);
break;
}
case Request_Header_Type_INSERT:
{
hdr->set_type(Request_Header_Type_INSERT);
std::string v;
person.SerializeToString(&v);
pyld->set_value(v);
pyld->set_overwrite(overwrite);
break;
}
case Request_Header_Type_REMOVE:
{
hdr->set_type(Request_Header_Type_REMOVE);
break;
}
default:
throw std::runtime_error("Only one of lookup/insert/remove"
" operations supported");
};
return request;
}
示例4: b
//.........这里部分代码省略.........
p += d2.size();
strcpy(p, tail.c_str());
p += tail.size();
r.m_body = string(buf, p - buf);
QA* qa = dat.Match(&r, true, "");
RRMessage resp;
bool res = qa->Answer(&resp, &r);
if (! res)
{
EXPECT_EQ(0, 1);
return ;
}
AddressBook ab;
{
Person* p = ab.add_person();
p->set_name("zhangsan");
p->set_id(3);
p->set_email("[email protected]");
Person::PhoneNumber* pp = p->add_phone();
pp->set_number("010-11");
pp->set_type(Person_PhoneType_WORK);
pp = p->add_phone();
pp->set_number("123");
pp->set_type(Person_PhoneType_MOBILE);
}
{
Person* p = ab.add_person();
p->set_name("lisi");
p->set_id(4);
}
string e;
ab.SerializeToString(&e);
EXPECT_EQ("abcd"+e, resp.m_body);
EXPECT_EQ(true, res);
}
{
RRMessage r;
//0x03abc4,defgzzz
string data = "abc";
string d2 = "defgg";
string tail = "zzz";
int length = data.size();
char buf[128];
char *p = buf;
memcpy(p, &length, sizeof(int));
p += sizeof(int);
strcpy(p, data.c_str());
p += data.size();
p += sprintf(p, "%d,", (int)d2.size());
strcpy(p, d2.c_str());
p += d2.size();
strcpy(p, tail.c_str());
p += tail.size();
r.m_body = string(buf, p - buf);
QA* qa = dat.Match(&r, true, "");
if (! qa)
{
EXPECT_EQ(0, 1);
return ;
}
RRMessage resp;
bool res = qa->Answer(&resp, &r);