当前位置: 首页 > news >正文

C++中将FlatBuffers序列化为JSON

​FlatBuffers是google发布的一个高效的序列化库,最近需要将系统中的flatbuffer的二进制数据转换为JSON以便其他系统使用,发现flatbuffer原生支持与JSON结构之间的转换。

方法一:使用GenerateText()函数

加载schema文件内容构造flatbuffers::Parser解析器,最后通过GenerateText()函数解析为JSON字符串。stackoverflow中有一段代码贴来,示例如下:

bool MyFBSchemaWrapper::asJson(std::string& jsonOutput)
{//**This is the section I don't like having to dostd::string schemaFile;if (flatbuffers::LoadFile((std::string(getenv("FBS_FILE_PATH")) + "MyFBSchema.fbs").c_str(), false, &schemaFile)){flatbuffers::Parser parser;const char *includePaths[] = { getenv("FBS_FILE_PATH");parser.Parse(schemaFile.c_str(), includePaths);//**End bad sectionparser.opts.strict_json = true;flatbuffers::FlatBufferBuilder fbBuilder;auto testItem1 = fbBuilder.CreateString("test1");auto testItem2 = fbBuilder.CreateString("test2");MyFBSchemaBuilder myBuilder(fbBuilder);myBuilder.add_item1(testItem1);myBuilder.add_item2(testItem2);FinishMyFBSchemaBuffer(fbBuilder, myBuilder.finish());auto result = GenerateText(parser, fbBuilder.GetBufferPointer(), &jsonOutput);return true;}return false;
}

其中:Parser和GenerateText都定义在在flatbuferrs/idl.h中,如下:

  // Parse the string containing either schema or JSON data, which will// populate the SymbolTable's or the FlatBufferBuilder above.// include_paths is used to resolve any include statements, and typically// should at least include the project path (where you loaded source_ from).// include_paths must be nullptr terminated if specified.// If include_paths is nullptr, it will attempt to load from the current// directory.// If the source was loaded from a file and isn't an include file,// supply its name in source_filename.// All paths specified in this call must be in posix format, if you accept// paths from user input, please call PosixPath on them first.bool Parse(const char* _source, const char** include_paths = nullptr,const char* source_filename = nullptr);
// Generate text (JSON) from a given FlatBuffer, and a given Parser
// object that has been populated with the corresponding schema.
// If ident_step is 0, no indentation will be generated. Additionally,
// if it is less than 0, no linefeeds will be generated either.
// See idl_gen_text.cpp.
// strict_json adds "quotes" around field names if true.
// These functions return nullptr on success, or an error string,
// which may happen if the flatbuffer cannot be encoded in JSON (e.g.,
// it contains non-UTF-8 byte arrays in String values).
extern bool GenerateTextFromTable(const Parser& parser, const void* table,const std::string& tablename,std::string* text);
extern const char* GenerateText(const Parser& parser, const void* flatbuffer,std::string* text);
extern const char* GenerateTextFile(const Parser& parser,const std::string& path,const std::string& file_name);

方法二:使用FlatBuffers中提供的mini reflection机制

这种方法要加上参数(–reflect-types)重新编译头文件,如:

# 将原来的命令(flatc -o ./ -cpp *.fbs)加上--reflect-types
flatc -o ./ --reflect-types -cpp *.fbs

使用FlatBufferToString函数

FlatBufferToString()函数定义在flatbuferrs/minireflect.h中,如下:

inline std::string FlatBufferToString(const uint8_t* buffer,const TypeTable* type_table,bool multi_line = false,bool vector_delimited = true,const std::string& indent = "",bool quotes = false) {ToStringVisitor tostring_visitor(multi_line ? "\n" : " ", quotes, indent,vector_delimited);IterateFlatBuffer(buffer, type_table, &tostring_visitor);return tostring_visitor.s;
}

REF.

  1. https://github.com/google/flatbuffers/tree/master
  2. https://stackoverflow.com/questions/60176212/serializing-a-flatbuffer-object-to-json-without-its-schema-file
  3. https://dbaileychess.github.io/flatbuffers/flatbuffers_guide_using_schema_compiler.html
  4. https://flatbuffers.dev/languages/cpp/#mini-reflection
http://www.dtcms.com/a/610693.html

相关文章:

  • 营销网站制作平台有哪些企业网站特色建设
  • pyinstaller 打包报错hook-matplotlib.backends.py
  • 盐城网站建设建站羽毛球最新赛事
  • 如何用dw做网站wordpress自动上传图片
  • 楼宇间网络拓扑测绘 从原理到精准部署
  • 汇编语言编译器存在哪 | 探讨编译器的设计与优化挑战
  • Torch核心数据结构Tensor(张量)
  • 什么是AI?AI新手终极指南(2025)
  • 22.与人类对齐的背景与标准
  • 周口城乡建设局网站外包公司工伤找谁赔偿
  • 【XR开发系列】理解游戏世界的基石 - 场景、物体与组件
  • MySQL 是怎么存储 NULL 的
  • 磁共振成像原理(理论)35:快速梯度回波成像 (Fast Gradient-Echo Imaging)
  • 【前传交换机 PTP】FibroLAN Falcon RX
  • Cursor 使用记录:C/C++ 开发者
  • 建设彩票网站一站式网站建设行业
  • fomo3d网站开发app软件开发培训班
  • 动态住宅IP和静态住宅IP哪个更好
  • 营销型网站 策划运营网站免认证域名
  • 网页设计个人网站建设工程交易中心是什么机构
  • Docker 核心命令速查表(精细分类版)
  • leetcode 2536 子矩阵元素加1
  • 如何做企业网站建设怎么清空wordpress媒体库
  • 做网站流程内容美食网站建设总结
  • 面对网络攻击告警 IP地址如何实现自动化封禁
  • 专栏介绍:AMD KFD BO设计深度剖析——解锁GPU存储核心技术
  • Kimi K2 Thinking:兼顾Agent和推理的六边形战士
  • 打字游戏——测一测你的反应速度
  • SpringBoot17-addresourcehandler()方法
  • 网站模版建设教程贵阳网站建设哪家公司好