diff --git a/include/flatbuffers/idl.h b/include/flatbuffers/idl.h index bac31a4d0..1ff528d4b 100644 --- a/include/flatbuffers/idl.h +++ b/include/flatbuffers/idl.h @@ -292,6 +292,8 @@ class Parser { // Generate text (JSON) from a given FlatBuffer, and a given Parser // 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. extern void GenerateText(const Parser &parser, const void *flatbuffer, diff --git a/src/idl_gen_text.cpp b/src/idl_gen_text.cpp index 2bb4ddd1d..ca87e1eb8 100644 --- a/src/idl_gen_text.cpp +++ b/src/idl_gen_text.cpp @@ -25,6 +25,12 @@ namespace flatbuffers { static void GenStruct(const StructDef &struct_def, const Table *table, 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 // for a single FlatBuffer value into JSON format. // The general case for scalars: @@ -39,9 +45,13 @@ template void PrintVector(const Vector &v, Type type, int indent, int indent_step, std::string *_text) { std::string &text = *_text; - text += "[\n"; + text += "["; + text += NewLine(indent_step); 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, ' '); if (IsStruct(type)) Print(v.GetStructFromOffset(i * type.struct_def->bytesize), type, @@ -49,7 +59,7 @@ template void PrintVector(const Vector &v, Type type, else Print(v.Get(i), type, indent + indent_step, indent_step, nullptr, _text); } - text += "\n"; + text += NewLine(indent_step); text.append(indent, ' '); 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, int indent, int indent_step, std::string *_text) { std::string &text = *_text; - text += "{\n"; + text += "{"; + text += NewLine(indent_step); int fieldout = 0; StructDef *union_sd = nullptr; 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; if (struct_def.fixed || table->CheckField(fd.value.offset)) { // The field is present. - if (fieldout++) text += ",\n"; + if (fieldout++) { + text += ","; + text += NewLine(indent_step); + } text.append(indent + indent_step, ' '); text += fd.name; 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 += "}"; } @@ -207,7 +221,7 @@ void GenerateText(const Parser &parser, const void *flatbuffer, 0, indent_step, _text); - text += "\n"; + text += NewLine(indent_step); } } // namespace flatbuffers