【Protocol Buffer】Protocol Buffer入门教程(四):序列化和反序列化

版权声明:本文为博主原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。
本文链接: https://blog.csdn.net/dengjin20104042056/article/details/102327892

00. 目录

01. 数组的序列化和反序列化

参考代码

#include "addressbook.pb.h"
#include <iostream>
using namespace std;

/*
//C数组的序列化和序列化API
//在/usr/local/include/google/目录下,查找包含"SerializeToArray"所有的文件,同时打印所在行
//sudo grep "SerializeToArray" -r /usr/local/include/google/ -n

bool SerializeToArray(void* data, int size) const; //序列化
bool ParseFromArray(const void* data, int size);   //反序列化
*/
char buf[1024];
int len;

void set_person()
{
    Person obj;
    obj.set_name("itcast");
    obj.set_id(88);
    *obj.mutable_email() = "[email protected]"; //obj.set_email("[email protected]");

    len = obj.ByteSize(); //获取长度
    cout << "len = " << len << endl;

    obj.SerializeToArray(buf, len);//序列化,obj成员保存在buf中
}

void get_person()
{
    Person obj;
    obj.ParseFromArray(buf, len); //反序列化,buf的内容设置给obj的成员

    cout << "name = " << obj.name() << endl;
    cout << "id = " << obj.id() << endl;
    cout << "email = " << obj.email() << endl;
}

int main()
{
    // Verify that the version of the library that we linked against is
    // compatible with the version of the headers we compiled against.
    GOOGLE_PROTOBUF_VERIFY_VERSION;

    set_person(); //序列化
    get_person(); //反序列化

    // Optional:  Delete all global objects allocated by libprotobuf.
    google::protobuf::ShutdownProtobufLibrary();

    return 0;
}

编译和执行结果

deng@itcast:/mnt/hgfs/LinuxHome/day03$ ls
addressbook.pb.cc  addressbook.pb.h  addressbook.proto  a.out  test.cpp
deng@itcast:/mnt/hgfs/LinuxHome/day03$ g++ test.cpp addressbook.pb.cc `pkg-config --libs --cflags protobuf` 
deng@itcast:/mnt/hgfs/LinuxHome/day03$ ./a.out 
len = 25
name = itcast
id = 88
email = [email protected]
deng@itcast:/mnt/hgfs/LinuxHome/day03$ 

02. 字符串序列化和反序列化

参考代码

#include "addressbook.pb.h"
#include <iostream>
using namespace std;


//bool SerializeToString(string* output) const;  
//bool ParseFromString(const string& data); 

string str; //全局变量

void set_person()
{
    Person obj;
    obj.set_name("itcast");
    obj.set_id(88);
    obj.set_email("[email protected]");
    //*obj.mutable_email() = "[email protected]";

    obj.SerializeToString(&str);    //序列化,obj成员的内容设置给str
}

void get_person()
{
    Person obj;
    obj.ParseFromString(str); //反序列化, str内容设置给obj的成员

    cout << "name = " << obj.name() << endl;
    cout << "id = " << obj.id() << endl;
    cout << "email = " << *obj.mutable_email() << endl;
}

int main()
{
    // Verify that the version of the library that we linked against is
    // compatible with the version of the headers we compiled against.
    GOOGLE_PROTOBUF_VERIFY_VERSION;

    set_person(); //序列化
    get_person(); //反序列化

    // Optional:  Delete all global objects allocated by libprotobuf.
    google::protobuf::ShutdownProtobufLibrary();

    return 0;
}

编译和执行结果

deng@itcast:/mnt/hgfs/LinuxHome/day03$ ls
addressbook.pb.cc  addressbook.proto  test01.cpp
addressbook.pb.h   a.out              test.cpp
deng@itcast:/mnt/hgfs/LinuxHome/day03$ g++ test.cpp addressbook.pb.cc `pkg-config --libs --cflags protobuf` 
deng@itcast:/mnt/hgfs/LinuxHome/day03$ ./a.out 
name = itcast
id = 88
email = [email protected]
deng@itcast:/mnt/hgfs/LinuxHome/day03$ 

03. 文件描述符序列化和反序列化

参考代码

#include "addressbook.pb.h"
#include <iostream>

#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <unistd.h>

using namespace std;


//bool SerializeToFileDescriptor(int file_descriptor) const;  
//bool ParseFromFileDescriptor(int file_descriptor); 

void set_person()
{
    Person obj;
    obj.set_name("itcast");
    obj.set_id(1);
    obj.set_email("[email protected]");
    //*obj.mutable_email() = "[email protected]";

    //O_CREAT: 新建文件, O_TRUNC:清空文件,O_RDWR:读写
    int fd = open("./pb.itcast", O_CREAT | O_TRUNC | O_RDWR, 0644);
    if (fd <= 0)
    {
        perror("open");
        exit(0);
    }

    obj.SerializeToFileDescriptor(fd);  //序列化,obj成员的内容写入fd所关联的文件中

    close(fd); //关闭文件
}

void get_person()
{
    int fd = open("./pb.itcast", O_RDONLY); //O_RDONLY: 只读方式
    if (fd <= 0)
    {
        perror("open");
        exit(0);
    }

    Person obj;
    obj.ParseFromFileDescriptor(fd); //反序列化, fd文件内容设置给obj的成员
    close(fd); //关闭文件

    cout << "name = " << obj.name() << endl;
    cout << "id = " << obj.id() << endl;
    cout << "email = " << *obj.mutable_email() << endl;
}

int main()
{
    // Verify that the version of the library that we linked against is
    // compatible with the version of the headers we compiled against.
    GOOGLE_PROTOBUF_VERIFY_VERSION;

    set_person(); //序列化
    get_person(); //反序列化

    // Optional:  Delete all global objects allocated by libprotobuf.
    google::protobuf::ShutdownProtobufLibrary();

    return 0;
}

编译和执行结果

deng@itcast:/mnt/hgfs/LinuxHome/day03$ ls
addressbook.pb.cc  addressbook.proto  pb.itcast   test02.cpp
addressbook.pb.h   a.out              test01.cpp  test.cpp
deng@itcast:/mnt/hgfs/LinuxHome/day03$ g++ test.cpp addressbook.pb.cc `pkg-config --libs --cflags protobuf`   
deng@itcast:/mnt/hgfs/LinuxHome/day03$ ./a.out 
name = itcast
id = 1
email = [email protected]
deng@itcast:/mnt/hgfs/LinuxHome/day03$ 

04. C++ Stream序列化和反序列化

参考代码

#include "addressbook.pb.h"
#include <iostream>
#include <fstream>


using namespace std;

//bool SerializeToOstream(ostream* output) const;  
//bool ParseFromIstream(istream* input);  

void set_person()
{
    Person obj;
    obj.set_name("itcast");
    obj.set_id(1);
    obj.set_email("[email protected]");
    //*obj.mutable_email() = "[email protected]";

    fstream output("pb.itcast", ios::out | ios::trunc | ios::binary);

    bool flag = obj.SerializeToOstream(&output);//序列化
    if (!flag)
    {
        cerr << "Failed to write file." << endl;
        return;
    }

    output.close();//关闭文件
}

void get_person()
{
    Person obj;
    fstream input("./pb.itcast", ios::in | ios::binary);
    obj.ParseFromIstream(&input);  //反序列化
    input.close(); //关闭文件

    cout << "name = " << obj.name() << endl;
    cout << "id = " << obj.id() << endl;
    cout << "email = " << *obj.mutable_email() << endl;
}

int main()
{
    // Verify that the version of the library that we linked against is
    // compatible with the version of the headers we compiled against.
    GOOGLE_PROTOBUF_VERIFY_VERSION;

    set_person(); //序列化
    get_person(); //反序列化

    // Optional:  Delete all global objects allocated by libprotobuf.
    google::protobuf::ShutdownProtobufLibrary();

    return 0;
}

编译和执行结果

deng@itcast:/mnt/hgfs/LinuxHome/day03$ ls
addressbook.pb.cc  addressbook.proto  pb.itcast   test02.cpp  test.cpp
addressbook.pb.h   a.out              test01.cpp  test03.cpp
deng@itcast:/mnt/hgfs/LinuxHome/day03$ g++ test.cpp addressbook.pb.cc `pkg-config --libs --cflags protobuf`     
deng@itcast:/mnt/hgfs/LinuxHome/day03$ ./a.out 
name = itcast
id = 1
email = [email protected]
deng@itcast:/mnt/hgfs/LinuxHome/day03$ 

05. 附录

参考博客:https://blog.csdn.net/sealyao/article/details/6940245

测试代码下载:测试代码

猜你喜欢

转载自blog.csdn.net/dengjin20104042056/article/details/102327892
今日推荐