将各种语言的 FlatBuffer 转换为 JSON
Convert FlatBuffer to JSON from various languages
FlatBuffer 是否允许将二进制 fbs 文件与 JSON 相互转换(当然模式将是已知的)?
我的想法是在 FlatBuffer 中定义管道和过滤器架构的结构模式。 FlatBuffer 文件也将在管道之间交换。但是,某些过滤器中的某些工具将要求我传递从 FlatBuffer 文件转换而来的普通旧 json 对象。我有多种语言可以支持(C++、Python、Java、JS)。
我找到了一个 javascript 库,它似乎是这样做的:
https://github.com/evanw/node-flatbuffers/
不过好像放弃了,我对官方支持的方式比较感兴趣
只有 C++ 提供了这种开箱即用的功能。
对于其他语言,您可以包装 C++ parser/generator,然后调用它(参见 Java 的示例:http://frogermcs.github.io/json-parsing-with-flatbuffers-in-android/)。
@evanw 是 FlatBuffers 中 JS 端口的原作者,所以你说的这个项目可能还可以用,但我觉得他不会再积极维护了。
或者,如果此 运行 在服务器上并且您可以 运行 command-line 实用程序,则可以使用 flatc
二进制文件通过以下方式为您进行转换一个文件。
理想情况下,所有语言都有自己的本机解析器,但需要进行大量重复工作。虽然与 C/C++ 交互是一件痛苦的事情,但它的优点是为您提供了一个非常快速的解析器。
使用 Flat C 版本 (FlatCC) 很容易将 flatbuffer 缓冲区转换为 JSON。
请参考 flatcc 源路径中的示例测试:flatcc-master/test/json_test.
生成所需的 json 帮助程序头文件使用:
flatcc_d -a --json <yourData.fbs>
会生成yourData_json_printer.h。在您的程序中包含此头文件。
修改以下代码以适应 <yourData>
。 buffer 是你从另一端接收到的 flatbuffer 输入。
也不要使用 sizeof() 从 buffer 中获取 flatbuffer 的 bufferSize。在调用之前打印缓冲区大小
函数
void flatbufToJson(const char *buffer, size_t bufferSize) {
flatcc_json_printer_t ctx_obj, *ctx;
FILE *fp = 0;
const char *target_filename = "yourData.json";
ctx = &ctx_obj;
fp = fopen(target_filename, "wb");
if (!fp) {
fprintf(stderr, "%s: could not open output file\n", target_filename);
printf("ctx not ready for clenaup, so exit directly\n");
return;
}
flatcc_json_printer_init(ctx, fp);
flatcc_json_printer_set_force_default(ctx, 1);
/* Uses same formatting as golden reference file. */
flatcc_json_printer_set_nonstrict(ctx);
//Check and modify here...
//the following should be re-written based on your fbs file and generated header file.
<yourData>_print_json(ctx, buffer, bufferSize);
flatcc_json_printer_flush(ctx);
if (flatcc_json_printer_get_error(ctx)) {
printf("could not print data\n");
}
fclose(fp);
printf("######### Json is done: \n");
}
FlatBuffer 是否允许将二进制 fbs 文件与 JSON 相互转换(当然模式将是已知的)?
我的想法是在 FlatBuffer 中定义管道和过滤器架构的结构模式。 FlatBuffer 文件也将在管道之间交换。但是,某些过滤器中的某些工具将要求我传递从 FlatBuffer 文件转换而来的普通旧 json 对象。我有多种语言可以支持(C++、Python、Java、JS)。
我找到了一个 javascript 库,它似乎是这样做的: https://github.com/evanw/node-flatbuffers/
不过好像放弃了,我对官方支持的方式比较感兴趣
只有 C++ 提供了这种开箱即用的功能。
对于其他语言,您可以包装 C++ parser/generator,然后调用它(参见 Java 的示例:http://frogermcs.github.io/json-parsing-with-flatbuffers-in-android/)。
@evanw 是 FlatBuffers 中 JS 端口的原作者,所以你说的这个项目可能还可以用,但我觉得他不会再积极维护了。
或者,如果此 运行 在服务器上并且您可以 运行 command-line 实用程序,则可以使用 flatc
二进制文件通过以下方式为您进行转换一个文件。
理想情况下,所有语言都有自己的本机解析器,但需要进行大量重复工作。虽然与 C/C++ 交互是一件痛苦的事情,但它的优点是为您提供了一个非常快速的解析器。
使用 Flat C 版本 (FlatCC) 很容易将 flatbuffer 缓冲区转换为 JSON。
请参考 flatcc 源路径中的示例测试:flatcc-master/test/json_test.
生成所需的 json 帮助程序头文件使用:
flatcc_d -a --json <yourData.fbs>
会生成yourData_json_printer.h。在您的程序中包含此头文件。
修改以下代码以适应
<yourData>
。 buffer 是你从另一端接收到的 flatbuffer 输入。 也不要使用 sizeof() 从 buffer 中获取 flatbuffer 的 bufferSize。在调用之前打印缓冲区大小 函数void flatbufToJson(const char *buffer, size_t bufferSize) { flatcc_json_printer_t ctx_obj, *ctx; FILE *fp = 0; const char *target_filename = "yourData.json"; ctx = &ctx_obj; fp = fopen(target_filename, "wb"); if (!fp) { fprintf(stderr, "%s: could not open output file\n", target_filename); printf("ctx not ready for clenaup, so exit directly\n"); return; } flatcc_json_printer_init(ctx, fp); flatcc_json_printer_set_force_default(ctx, 1); /* Uses same formatting as golden reference file. */ flatcc_json_printer_set_nonstrict(ctx); //Check and modify here... //the following should be re-written based on your fbs file and generated header file. <yourData>_print_json(ctx, buffer, bufferSize); flatcc_json_printer_flush(ctx); if (flatcc_json_printer_get_error(ctx)) { printf("could not print data\n"); } fclose(fp); printf("######### Json is done: \n"); }