summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorAnthony Liot <wolfviking0@yahoo.com>2019-04-05 11:51:29 -0700
committerWouter van Oortmerssen <aardappel@gmail.com>2019-04-05 11:51:29 -0700
commit249f3b37149daabbf4954dc8d615e97238b692b9 (patch)
tree5f2020fb1121dd5a1bf7e159a676b9dfa50c5765
parent2d67de3151a3389ff0ecf1578a61db6f0a9cf028 (diff)
downloadflatbuffers-249f3b37149daabbf4954dc8d615e97238b692b9.tar.gz
flatbuffers-249f3b37149daabbf4954dc8d615e97238b692b9.tar.bz2
flatbuffers-249f3b37149daabbf4954dc8d615e97238b692b9.zip
Add method GenerateTextFromTable issue #5249 (#5266)
* Create a function GenerateGenerateTextFromTable in order to create a json from any Table Signed-off-by: Anthony Liot <anthony.liot@gmail.com> * Update the test to failed if loadfile or parser return false Signed-off-by: Anthony Liot <anthony.liot@gmail.com> * Fix snake_case name typo + space before &/* Signed-off-by: Anthony Liot <anthony.liot@gmail.com> * use auto Signed-off-by: Anthony Liot <anthony.liot@gmail.com> * Use clang-format on the added code Signed-off-by: Anthony Liot <anthony.liot@gmail.com>
-rw-r--r--include/flatbuffers/idl.h4
-rw-r--r--src/idl_gen_text.cpp17
-rw-r--r--tests/test.cpp48
3 files changed, 69 insertions, 0 deletions
diff --git a/include/flatbuffers/idl.h b/include/flatbuffers/idl.h
index 05d666cb..40dfba40 100644
--- a/include/flatbuffers/idl.h
+++ b/include/flatbuffers/idl.h
@@ -834,6 +834,10 @@ extern std::string MakeCamel(const std::string &in, bool first = true);
// strict_json adds "quotes" around field names if true.
// If the flatbuffer cannot be encoded in JSON (e.g., it contains non-UTF-8
// byte arrays in String values), returns false.
+extern bool GenerateTextFromTable(const Parser &parser,
+ const void *table,
+ const std::string &tablename,
+ std::string *text);
extern bool GenerateText(const Parser &parser,
const void *flatbuffer,
std::string *text);
diff --git a/src/idl_gen_text.cpp b/src/idl_gen_text.cpp
index fb75a795..4c61ff99 100644
--- a/src/idl_gen_text.cpp
+++ b/src/idl_gen_text.cpp
@@ -263,6 +263,23 @@ static bool GenStruct(const StructDef &struct_def, const Table *table,
}
// Generate a text representation of a flatbuffer in JSON format.
+bool GenerateTextFromTable(const Parser &parser, const void *table,
+ const std::string &table_name, std::string *_text) {
+ auto struct_def = parser.LookupStruct(table_name);
+ if (struct_def == nullptr) {
+ return false;
+ }
+ auto text = *_text;
+ text.reserve(1024); // Reduce amount of inevitable reallocs.
+ auto root = static_cast<const Table *>(table);
+ if (!GenStruct(*struct_def, root, 0, parser.opts, _text)) {
+ return false;
+ }
+ text += NewLine(parser.opts);
+ return true;
+}
+
+// Generate a text representation of a flatbuffer in JSON format.
bool GenerateText(const Parser &parser, const void *flatbuffer,
std::string *_text) {
std::string &text = *_text;
diff --git a/tests/test.cpp b/tests/test.cpp
index 6da1dff9..04c014d8 100644
--- a/tests/test.cpp
+++ b/tests/test.cpp
@@ -1702,6 +1702,53 @@ void InvalidFloatTest() {
TestError("table T { F:float; } root_type T; { F:null }", invalid_msg);
}
+void GenerateTableTextTest() {
+ std::string schemafile;
+ std::string jsonfile;
+ bool ok =
+ flatbuffers::LoadFile((test_data_path + "monster_test.fbs").c_str(),
+ false, &schemafile) &&
+ flatbuffers::LoadFile((test_data_path + "monsterdata_test.json").c_str(),
+ false, &jsonfile);
+ TEST_EQ(ok, true);
+ auto include_test_path =
+ flatbuffers::ConCatPathFileName(test_data_path, "include_test");
+ const char *include_directories[] = {test_data_path.c_str(),
+ include_test_path.c_str(), nullptr};
+ flatbuffers::IDLOptions opt;
+ opt.indent_step = -1;
+ flatbuffers::Parser parser(opt);
+ ok = parser.Parse(schemafile.c_str(), include_directories) &&
+ parser.Parse(jsonfile.c_str(), include_directories);
+ TEST_EQ(ok, true);
+ // Test root table
+ const Monster *monster = GetMonster(parser.builder_.GetBufferPointer());
+ std::string jsongen;
+ auto result = GenerateTextFromTable(parser, monster, "MyGame.Example.Monster",
+ &jsongen);
+ TEST_EQ(result, true);
+ // Test sub table
+ const Vec3 *pos = monster->pos();
+ jsongen.clear();
+ result = GenerateTextFromTable(parser, pos, "MyGame.Example.Vec3", &jsongen);
+ TEST_EQ(result, true);
+ TEST_EQ_STR(
+ jsongen.c_str(),
+ "{x: 1.0,y: 2.0,z: 3.0,test1: 3.0,test2: \"Green\",test3: {a: 5,b: 6}}");
+ const Test &test3 = pos->test3();
+ jsongen.clear();
+ result =
+ GenerateTextFromTable(parser, &test3, "MyGame.Example.Test", &jsongen);
+ TEST_EQ(result, true);
+ TEST_EQ_STR(jsongen.c_str(), "{a: 5,b: 6}");
+ const Test *test4 = monster->test4()->Get(0);
+ jsongen.clear();
+ result =
+ GenerateTextFromTable(parser, test4, "MyGame.Example.Test", &jsongen);
+ TEST_EQ(result, true);
+ TEST_EQ_STR(jsongen.c_str(), "{a: 10,b: 20}");
+}
+
template<typename T>
void NumericUtilsTestInteger(const char *lower, const char *upper) {
T x;
@@ -2602,6 +2649,7 @@ int FlatBufferTests() {
IsAsciiUtilsTest();
ValidFloatTest();
InvalidFloatTest();
+ GenerateTableTextTest();
return 0;
}