标签:
1、下载flatbuffers
从https://github.com/google/flatbuffers地址下载flatbuffers-master.zip文件。
2、编译flatbuffers
解压flatbuffers-master.zip文件文件,使用VS2010打开flatbuffers-master\build\VS2010\FlatBuffers.sln文件进行编译。编译后生成的文件如下:
1. flatc.exe - 编译schema文件、生成代码
2. flatsamplebinary.exe - 测试程序,将二进制转换为C++对象
3. flatsampletext.exe - 测试程序,将json转换为C++对象
4. flattests.exe - 测试flatbuffers
3、json与C++对象相互转换
flatbuffers的测试程序中有json与c++对象相互转换的例子,但是不够方便,在这里我们可以封装一下。
FlatBufferJsonFactory.h
#include "TypeBind.h" // 详见我的另一篇博文"C++模板之类型与数据绑定" class FlatBufferJsonFactory { private: struct ParserInfo { ParserInfo(const std::string& _path) : path(_path), parser(nullptr) {} std::string path; // fbs文件路径 flatbuffers::Parser* parser; // flatbuffers解析器 }; public: // 将json字符串转换为了对象缓冲区 template<class T> std::string parserJson(const std::string& s) { const ParserInfo& info = getParser<T>(); info.parser->Parse(s.c_str(), nullptr); const size_t size = info.parser->builder_.GetSize(); const char* ptr = (const char*)info.parser->builder_.GetBufferPointer(); std::string buffer(ptr, ptr + size); return buffer; } // 将对象缓存转换为json字符串 template<class T> std::string toJson(const void* buffer) { std::string json; const ParserInfo& info = getParser<T>(); flatbuffers::GeneratorOptions options; options.strict_json = true; GenerateText(*info.parser, buffer, options, &json); return json; } // 注册 template<class T> void Register(const std::string& path) { ParserInfo info(path); m_map.bind<T>(info); } template<class T> const ParserInfo& getParser() { ParserInfo& info = m_map.find<T>(); if (!info.parser) { std::string buf; flatbuffers::LoadFile(info.path.c_str(), false, &buf); flatbuffers::Parser* parser = new flatbuffers::Parser(); bool ret = parser->Parse(buf.c_str(), nullptr); info.parser = parser; } return info; } static FlatBufferJsonFactory& Instance() { static FlatBufferJsonFactory* instance = nullptr; if (!instance) instance = new FlatBufferJsonFactory(); return *instance; } private: FlatBufferJsonFactory() {} TypeBind<ParserInfo> m_map; };
编写test.fbs文件
namespace TestApp; struct KV { key: ulong; value: double; } table TestObj { id: ulong; name: string; flag: ubyte = 0; list: [ulong]; kv: KV; } root_type TestObj;
然后利用"flatc.exe"生成.h文件,并使用此.h文件
int _tmain(int argc, _TCHAR* argv[]) { FlatBufferBuilder fbb; // 设置数据 std::vector<uint64_t> v = { 1, 2, 3, 4, 5 }; auto kv = KV(1, 1.0); auto name = fbb.CreateString("123"); auto vec = fbb.CreateVector(v); TestObjBuilder builder(fbb); builder.add_id(100); builder.add_name(name); builder.add_flag(1); builder.add_list(vec); builder.add_kv(&kv); fbb.Finish(builder.Finish()); // 注册 FlatBufferJsonFactory::Instance().Register<TestApp::TestObj>("E:\\c++\\use_flat_buffers\\use_flat_buffers\\test.fbs"); std::string json = FlatBufferJsonFactory::Instance().toJson<TestApp::TestObj>(fbb.GetBufferPointer()); std::string buffer = FlatBufferJsonFactory::Instance().parserJson<TestApp::TestObj>(json); const TestApp::TestObj* obj = GetTestObj(buffer.c_str()); const char* names = obj->name()->c_str(); return 0; }
标签:
原文地址:http://www.cnblogs.com/dongc/p/4896393.html