Reputation: 8923
Is it possible to serialize / deserialize flatbuffers to and from JSON?
What I really want to do is to save flatbuffers as JSON, allow people to change whatever value they want, then read back JSON into a flatbuffer (and use that in some way in the application).
Maybe there is another way to achieve the same effect. We are working in C++.
Upvotes: 16
Views: 21054
Reputation: 347
The flatc
command can be used to convert binary files to JSON files
flatc --json --raw-binary <flatbuffer_schema_file.fbs> -- <binary_file.bin>
for more info check the Flatbuffer guide.
Note that this command may not produce any output. To fix this issue you should declare root type in the flatbuffer schema. To fix this add the bellow line in flatbuffer schema.
root_type FOO;
Flatc documentation offers an additional option for specifying the root type with --root-type T
Upvotes: 0
Reputation: 1078
this is what I use
sample.fbs file containing flatbuffers schema.
table sample
{
firstName: string;
lastName: string;
age: int;
}
root_type sample;
the program that parses JSON to flatbuffers binary and back to JSON
#include <iostream>
#include <string>
#include "flatbuffers/idl.h"
int main()
{
std::string input_json_data = "{ \
firstName: \"somename\", \
lastName: \"someothername\", \
age: 21 \
} \
";
std::string schemafile;
std::string jsonfile;
bool ok = flatbuffers::LoadFile("sample.fbs", false, &schemafile);
if (!ok) {
std::cout << "load file failed!" << std::endl;
return -1;
}
flatbuffers::Parser parser;
parser.Parse(schemafile.c_str());
if (!parser.Parse(input_json_data.c_str())) {
std::cout << "flatbuffers parser failed with error : " << parser.error_ << std::endl;
return -1;
}
std::string jsongen;
if (!GenerateText(parser, parser.builder_.GetBufferPointer(), &jsongen)) {
std::cout << "Couldn't serialize parsed data to JSON!" << std::endl;
return -1;
}
std::cout << "intput json" << std::endl
<< input_json_data << std::endl
<< std::endl
<< "output json" << std::endl
<< jsongen << std::endl;
return 0;
}
produces following output
$ ./build/output/test_json_fb
intput json
{ firstName: "somename", lastName: "someothername", age: 21 }
output json
{
firstName: "somename",
lastName: "someothername",
age: 21
}
created by referring page https://github.com/google/flatbuffers/blob/master/samples/sample_text.cpp
Upvotes: 8
Reputation: 39
http://frogermcs.github.io/json-parsing-with-flatbuffers-in-android/
FlatBuffers library is getting more and more popular recently. If you check last Android Performance Patterns series, Colt McAnlis mentions about it a couple times in different videos. Probably you remember Facebook’s announcement about migration to FlatBuffers. Also some time ago I published post about how to start using it in Android.
java
public class FlatBuffersParser {
static {
System.loadLibrary("FlatBuffersParser");
}
public ByteBuffer parseJson(String json, String schema) {
final byte[] bytes = parseJsonNative(json, schema);
return ByteBuffer.wrap(bytes);
}
private native byte[] parseJsonNative(String json, String schema);
}
**C++ **
#ifndef __MAIN_H__
#define __MAIN_H__
#include <flatbuffers/idl.h>
#include "main.h"
JNIEXPORT jbyteArray JNICALL
Java_frogermcs_io_flatbuffersparser_FlatBuffersParser_parseJsonNative(JNIEnv *env,
jobject instance,
jstring json_,
jstring schema_) {
const char *json = env->GetStringUTFChars(json_, 0);
const char *schema = env->GetStringUTFChars(schema_, 0);
flatbuffers::Parser parser;
bool ok = parser.Parse(schema) && parser.Parse(json);
env->ReleaseStringUTFChars(json_, json);
env->ReleaseStringUTFChars(schema_, schema);
if (ok) {
flatbuffers::uoffset_t length = parser.builder_.GetSize();
jbyteArray result = env->NewByteArray(length);
uint8_t *bufferPointer = parser.builder_.GetBufferPointer();
env->SetByteArrayRegion(result, 0, length, reinterpret_cast<jbyte *>(bufferPointer));
return result;
}
}
#endif // __MAIN_H__
Upvotes: 1
Reputation: 6074
Yes, this is built-in functionality in FlatBuffers. See "Text and Schema parsing" here: https://google.github.io/flatbuffers/flatbuffers_guide_use_cpp.html
Also see examples of that in test.cpp
in ParseAndGenerateTextTest()
, or also registry.h
Upvotes: 13