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>
This commit is contained in:
Anthony Liot
2019-04-05 11:51:29 -07:00
committed by Wouter van Oortmerssen
parent 2d67de3151
commit 249f3b3714
3 changed files with 69 additions and 0 deletions

View File

@@ -262,6 +262,23 @@ static bool GenStruct(const StructDef &struct_def, const Table *table,
return true;
}
// 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) {