Can I serialize / dserialize flatbuffers to / from JSON?
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
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
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__