diff --git a/docs/source/Compiler.md b/docs/source/Compiler.md index cb0bb9605..3ff89edc3 100755 --- a/docs/source/Compiler.md +++ b/docs/source/Compiler.md @@ -45,6 +45,9 @@ be generated for each file processed: in quotes, no trailing commas in tables/vectors). By default, no quotes are required/generated, and trailing commas are allowed. +- `--defaults-json` : Output fields whose value is equal to the default value + when writing JSON text. + - `--no-prefix` : Don't prefix enum values in generated C++ by their enum type. diff --git a/include/flatbuffers/idl.h b/include/flatbuffers/idl.h index ac0e20ec8..2e9cfb511 100644 --- a/include/flatbuffers/idl.h +++ b/include/flatbuffers/idl.h @@ -400,6 +400,7 @@ extern void GenComment(const std::vector &dc, // Container of options that may apply to any of the source/text generators. struct GeneratorOptions { bool strict_json; + bool output_default_scalars_in_json; int indent_step; bool output_enum_identifiers; bool prefixed_enums; @@ -411,7 +412,9 @@ struct GeneratorOptions { Language lang; - GeneratorOptions() : strict_json(false), indent_step(2), + GeneratorOptions() : strict_json(false), + output_default_scalars_in_json(false), + indent_step(2), output_enum_identifiers(true), prefixed_enums(true), include_dependence_headers(false), mutable_buffer(false), diff --git a/src/flatc.cpp b/src/flatc.cpp index b98e58ffd..5eb0fda7d 100755 --- a/src/flatc.cpp +++ b/src/flatc.cpp @@ -87,6 +87,8 @@ static void Error(const std::string &err, bool usage, bool show_exe_name) { " -M Print make rules for generated files.\n" " --strict-json Strict JSON: field names must be / will be quoted,\n" " no trailing commas in tables/vectors.\n" + " --defaults-json Output fields whose value is the default when\n" + " writing JSON\n" " --no-prefix Don\'t prefix enum values with the enum type in C++.\n" " --gen-includes Generate include statements for included schemas the\n" " generated file depends on (C++).\n" @@ -130,6 +132,8 @@ int main(int argc, const char *argv[]) { include_directories.push_back(argv[argi]); } else if(arg == "--strict-json") { opts.strict_json = true; + } else if(arg == "--defaults-json") { + opts.output_default_scalars_in_json = true; } else if(arg == "--no-prefix") { opts.prefixed_enums = false; } else if(arg == "--gen-mutable") { diff --git a/src/idl_gen_text.cpp b/src/idl_gen_text.cpp index 4fb8f0923..848aac595 100644 --- a/src/idl_gen_text.cpp +++ b/src/idl_gen_text.cpp @@ -216,8 +216,11 @@ static void GenStruct(const StructDef &struct_def, const Table *table, it != struct_def.fields.vec.end(); ++it) { FieldDef &fd = **it; - if (struct_def.fixed || table->CheckField(fd.value.offset)) { - // The field is present. + auto is_present = struct_def.fixed || table->CheckField(fd.value.offset); + auto output_anyway = opts.output_default_scalars_in_json && + IsScalar(fd.value.type.base_type) && + !fd.deprecated; + if (is_present || output_anyway) { if (fieldout++) { text += ","; } @@ -225,30 +228,36 @@ static void GenStruct(const StructDef &struct_def, const Table *table, text.append(indent + Indent(opts), ' '); OutputIdentifier(fd.name, opts, _text); text += ": "; - switch (fd.value.type.base_type) { - #define FLATBUFFERS_TD(ENUM, IDLTYPE, CTYPE, JTYPE, GTYPE, NTYPE, \ - PTYPE) \ - case BASE_TYPE_ ## ENUM: \ - GenField(fd, table, struct_def.fixed, \ - opts, indent + Indent(opts), _text); \ + if (is_present) { + switch (fd.value.type.base_type) { + #define FLATBUFFERS_TD(ENUM, IDLTYPE, CTYPE, JTYPE, GTYPE, NTYPE, \ + PTYPE) \ + case BASE_TYPE_ ## ENUM: \ + GenField(fd, table, struct_def.fixed, \ + opts, indent + Indent(opts), _text); \ + break; + FLATBUFFERS_GEN_TYPES_SCALAR(FLATBUFFERS_TD) + #undef FLATBUFFERS_TD + // Generate drop-thru case statements for all pointer types: + #define FLATBUFFERS_TD(ENUM, IDLTYPE, CTYPE, JTYPE, GTYPE, NTYPE, \ + PTYPE) \ + case BASE_TYPE_ ## ENUM: + FLATBUFFERS_GEN_TYPES_POINTER(FLATBUFFERS_TD) + #undef FLATBUFFERS_TD + GenFieldOffset(fd, table, struct_def.fixed, indent + Indent(opts), + union_sd, opts, _text); break; - FLATBUFFERS_GEN_TYPES_SCALAR(FLATBUFFERS_TD) - #undef FLATBUFFERS_TD - // Generate drop-thru case statements for all pointer types: - #define FLATBUFFERS_TD(ENUM, IDLTYPE, CTYPE, JTYPE, GTYPE, NTYPE, \ - PTYPE) \ - case BASE_TYPE_ ## ENUM: - FLATBUFFERS_GEN_TYPES_POINTER(FLATBUFFERS_TD) - #undef FLATBUFFERS_TD - GenFieldOffset(fd, table, struct_def.fixed, indent + Indent(opts), - union_sd, opts, _text); - break; + } + if (fd.value.type.base_type == BASE_TYPE_UTYPE) { + auto enum_val = fd.value.type.enum_def->ReverseLookup( + table->GetField(fd.value.offset, 0)); + assert(enum_val); + union_sd = enum_val->struct_def; + } } - if (fd.value.type.base_type == BASE_TYPE_UTYPE) { - auto enum_val = fd.value.type.enum_def->ReverseLookup( - table->GetField(fd.value.offset, 0)); - assert(enum_val); - union_sd = enum_val->struct_def; + else + { + text += fd.value.constant; } } }