mirror of
https://github.com/google/flatbuffers.git
synced 2026-06-30 04:50:04 +00:00
JSON text output now optionally doesn't output linefeeds either.
Change-Id: Iedac80ee00b27a97c21c7b4ac7c9792e5bafbcc8 Tested: On Windows
This commit is contained in:
@@ -292,6 +292,8 @@ class Parser {
|
|||||||
|
|
||||||
// Generate text (JSON) from a given FlatBuffer, and a given Parser
|
// Generate text (JSON) from a given FlatBuffer, and a given Parser
|
||||||
// object that has been populated with the corresponding schema.
|
// object that has been populated with the corresponding schema.
|
||||||
|
// If ident_step is 0, no indentation will be generated. Additionally,
|
||||||
|
// if it is less than 0, no linefeeds will be generated either.
|
||||||
// See idl_gen_text.cpp.
|
// See idl_gen_text.cpp.
|
||||||
extern void GenerateText(const Parser &parser,
|
extern void GenerateText(const Parser &parser,
|
||||||
const void *flatbuffer,
|
const void *flatbuffer,
|
||||||
|
|||||||
@@ -25,6 +25,12 @@ namespace flatbuffers {
|
|||||||
static void GenStruct(const StructDef &struct_def, const Table *table,
|
static void GenStruct(const StructDef &struct_def, const Table *table,
|
||||||
int indent, int indent_step, std::string *_text);
|
int indent, int indent_step, std::string *_text);
|
||||||
|
|
||||||
|
// If indentation is less than 0, that indicates we don't want any newlines
|
||||||
|
// either.
|
||||||
|
const char *NewLine(int indent_step) {
|
||||||
|
return indent_step >= 0 ? "\n" : "";
|
||||||
|
}
|
||||||
|
|
||||||
// Print (and its template specialization below for pointers) generate text
|
// Print (and its template specialization below for pointers) generate text
|
||||||
// for a single FlatBuffer value into JSON format.
|
// for a single FlatBuffer value into JSON format.
|
||||||
// The general case for scalars:
|
// The general case for scalars:
|
||||||
@@ -39,9 +45,13 @@ template<typename T> void PrintVector(const Vector<T> &v, Type type,
|
|||||||
int indent, int indent_step,
|
int indent, int indent_step,
|
||||||
std::string *_text) {
|
std::string *_text) {
|
||||||
std::string &text = *_text;
|
std::string &text = *_text;
|
||||||
text += "[\n";
|
text += "[";
|
||||||
|
text += NewLine(indent_step);
|
||||||
for (uoffset_t i = 0; i < v.Length(); i++) {
|
for (uoffset_t i = 0; i < v.Length(); i++) {
|
||||||
if (i) text += ",\n";
|
if (i) {
|
||||||
|
text += ",";
|
||||||
|
text += NewLine(indent_step);
|
||||||
|
}
|
||||||
text.append(indent + indent_step, ' ');
|
text.append(indent + indent_step, ' ');
|
||||||
if (IsStruct(type))
|
if (IsStruct(type))
|
||||||
Print(v.GetStructFromOffset(i * type.struct_def->bytesize), type,
|
Print(v.GetStructFromOffset(i * type.struct_def->bytesize), type,
|
||||||
@@ -49,7 +59,7 @@ template<typename T> void PrintVector(const Vector<T> &v, Type type,
|
|||||||
else
|
else
|
||||||
Print(v.Get(i), type, indent + indent_step, indent_step, nullptr, _text);
|
Print(v.Get(i), type, indent + indent_step, indent_step, nullptr, _text);
|
||||||
}
|
}
|
||||||
text += "\n";
|
text += NewLine(indent_step);
|
||||||
text.append(indent, ' ');
|
text.append(indent, ' ');
|
||||||
text += "]";
|
text += "]";
|
||||||
}
|
}
|
||||||
@@ -155,7 +165,8 @@ static void GenFieldOffset(const FieldDef &fd, const Table *table, bool fixed,
|
|||||||
static void GenStruct(const StructDef &struct_def, const Table *table,
|
static void GenStruct(const StructDef &struct_def, const Table *table,
|
||||||
int indent, int indent_step, std::string *_text) {
|
int indent, int indent_step, std::string *_text) {
|
||||||
std::string &text = *_text;
|
std::string &text = *_text;
|
||||||
text += "{\n";
|
text += "{";
|
||||||
|
text += NewLine(indent_step);
|
||||||
int fieldout = 0;
|
int fieldout = 0;
|
||||||
StructDef *union_sd = nullptr;
|
StructDef *union_sd = nullptr;
|
||||||
for (auto it = struct_def.fields.vec.begin();
|
for (auto it = struct_def.fields.vec.begin();
|
||||||
@@ -164,7 +175,10 @@ static void GenStruct(const StructDef &struct_def, const Table *table,
|
|||||||
FieldDef &fd = **it;
|
FieldDef &fd = **it;
|
||||||
if (struct_def.fixed || table->CheckField(fd.value.offset)) {
|
if (struct_def.fixed || table->CheckField(fd.value.offset)) {
|
||||||
// The field is present.
|
// The field is present.
|
||||||
if (fieldout++) text += ",\n";
|
if (fieldout++) {
|
||||||
|
text += ",";
|
||||||
|
text += NewLine(indent_step);
|
||||||
|
}
|
||||||
text.append(indent + indent_step, ' ');
|
text.append(indent + indent_step, ' ');
|
||||||
text += fd.name;
|
text += fd.name;
|
||||||
text += ": ";
|
text += ": ";
|
||||||
@@ -191,7 +205,7 @@ static void GenStruct(const StructDef &struct_def, const Table *table,
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
text += "\n";
|
text += NewLine(indent_step);
|
||||||
text.append(indent, ' ');
|
text.append(indent, ' ');
|
||||||
text += "}";
|
text += "}";
|
||||||
}
|
}
|
||||||
@@ -207,7 +221,7 @@ void GenerateText(const Parser &parser, const void *flatbuffer,
|
|||||||
0,
|
0,
|
||||||
indent_step,
|
indent_step,
|
||||||
_text);
|
_text);
|
||||||
text += "\n";
|
text += NewLine(indent_step);
|
||||||
}
|
}
|
||||||
|
|
||||||
} // namespace flatbuffers
|
} // namespace flatbuffers
|
||||||
|
|||||||
Reference in New Issue
Block a user