FlatBuffers是为Google发布的一个跨平台,提供多种语言接口,注重性能和资源使用的序列化类库。目前该类库提供C++、C#、C、GO、Java、JavaScript、PHP、Python语言接口。该序列化库多用于移动端手游数据传输以及特定的对性能要求较高的应用。
本文只介绍在C++中的使用步骤,原理部分不阐述。
一、安装flatc
flatc是从来将schema文件转化为C++头文件的工具,需要使用CMake来编译FlatBuffers。因此第一步先安装CMake工具(如没有)。
git clone https://github.com/google/flatbuffers.git
进入flatbuffers根目录,执行
cmake -G "Unix Makefiles"
make
make install
查看flatc是否安装成功
./flatc --version
二、编写schema文件
用schema文件来描述我们要序列化的类,语法和c/c++很相似。比如我们用C++定义类是这样写:
class Point
{
float x;
float y;
};
class Node
{
std::string name;
Point position;
};
class Layer: public Node
{
Node* friend;
std::vector<Node*> children;
std::vector<Point *> transform;
};
那么用schema可以这样编写:
namespace Layer;
table Point_Fb
{
x:float;
y:float;
}
table Node_Fb
{
name:string;
position:Point_Fb;
}
table Layer_Fb
{
super:Node_Fb;
friend:Node_Fb;
children:[Node_Fb];
transform:[Point_Fb];
}
root_type Layer_Fb;
然后把这个文件保存为.fbs后缀的文件,如Layer_Fb.fbs
三、生成_generated.h文件
这里我把Layer_Fb.fbs文件放在了flatc同级目录下,并且制定在这个目录生成结果
flatc -c -o ./ ./Layer_Fb.fbs
执行成功后,可以看到目录下会多一个Layer_Fb_generated.h文件
四、配置工程依赖
首先将flatbuffers目录下的libflatbuffers.a和include目录,然后将生成的Layer_Fb_generated.h头文件加到工程中。
注意:XCode中还需配置Build Setting -> Header Search Paths为flatbuffers的上级目录,也就是include,否则引用的头文件会找不到
五、序列化
void serialize() {
flatbuffers::FlatBufferBuilder build_data;
// super
auto position_fb = Layer::CreatePoint_Fb(build_data, 1, 1);
auto super_fb = Layer::CreateNode_Fb(build_data, build_data.CreateString("father"), position_fb);
// friend
auto friend_fb = Layer::CreateNode_Fb(build_data, build_data.CreateString("tom"), Layer::CreatePoint_Fb(build_data, 2, 2));
// children
std::vector<flatbuffers::Offset<Layer::Node_Fb>> nodeVector;
auto child1 = Layer::CreateNode_Fb(build_data, build_data.CreateString("son"), Layer::CreatePoint_Fb(build_data, 3, 3));
auto child2 = Layer::CreateNode_Fb(build_data, build_data.CreateString("daughter"), Layer::CreatePoint_Fb(build_data, 4, 4));
nodeVector.push_back(child1);
nodeVector.push_back(child2);
auto children_fb = build_data.CreateVector(nodeVector);
// layer
auto layer_fb = Layer::CreateLayer_Fb(build_data, super_fb, friend_fb, children_fb);
build_data.Finish(layer_fb);
}
六、保存序列化文件
buffer可以保存到本地或直接通过网络发送。以下保存到本地的二进制文件中,GetBufferPointer()用来获取buffer的内存指针,GetSize()获取内存大小,最后一个参数用来指定是否生成二进制文件。
flatbuffers::SaveFile("layer.bin", reinterpret_cast<char*>(build_data.GetBufferPointer()), build_data.GetSize(), true);
七、反序列化
void deserialize() {
// load binary file
std::string binaryfile;
bool ok = flatbuffers::LoadFile("layer.bin", false, &binaryfile);
if (!ok) {
return;
}
flatbuffers::FlatBufferBuilder builder_out;
builder_out.PushBytes(reinterpret_cast<unsigned char*>(const_cast<char*>(binaryfile.c_str())), binaryfile.size());
cout<<"deserialize size:"<<build_data.GetSize()<<endl;
// verify
flatbuffers::Verifier layer_verify(builder_out.GetCurrentBufferPointer(), build_data.GetSize());
bool verify_flag = Layer::VerifyLayer_FbBuffer(layer_verify);
if (!verify_flag) {
return;
}
// read
auto layer_fb = Layer::GetLayer_Fb(builder_out.GetCurrentBufferPointer());
cout<<"father name:"+layer_fb->super()->name()->str()<<endl;
cout<<"friend name:"+layer_fb->friend_()->name()->str()<<endl;
for (int i=0; i<layer_fb->children()->Length(); i++) {
string name = layer_fb->children()->Get(i)->name()->str();
cout<<"child "<<i<<":"+name<<endl;
}
}
完整代码下载
参考文章:
https://google.github.io/flatbuffers/flatbuffers_guide_writing_schema.html
https://www.cnblogs.com/relvin/p/5540063.html
https://www.jianshu.com/p/6eb04a149cd8