diff --git a/docs/source/Compiler.md b/docs/source/Compiler.md index a73023209..3a6adf81d 100644 --- a/docs/source/Compiler.md +++ b/docs/source/Compiler.md @@ -136,5 +136,7 @@ Additional options: - `--root-type T` : Select or override the default root_type. +- `--force-defaults` : Emit default values in binary output from JSON. + NOTE: short-form options for generators are deprecated, use the long form whenever possible. diff --git a/include/flatbuffers/idl.h b/include/flatbuffers/idl.h index d60017009..88d9553d7 100644 --- a/include/flatbuffers/idl.h +++ b/include/flatbuffers/idl.h @@ -392,6 +392,7 @@ struct IDLOptions { bool protobuf_ascii_alike; bool size_prefixed; std::string root_type; + bool force_defaults; // Possible options for the more general generator below. enum Language { @@ -452,6 +453,7 @@ struct IDLOptions { reexport_ts_modules(true), protobuf_ascii_alike(false), size_prefixed(false), + force_defaults(false), lang(IDLOptions::kJava), mini_reflect(IDLOptions::kNone), lang_to_generate(0) {} @@ -527,6 +529,9 @@ class Parser : public ParserState { source_(nullptr), anonymous_counter(0), recurse_protection_counter(0) { + if (opts.force_defaults) { + builder_.ForceDefaults(true); + } // Start out with the empty namespace being current. empty_namespace_ = new Namespace(); namespaces_.push_back(empty_namespace_); diff --git a/src/flatc.cpp b/src/flatc.cpp index 0487a4513..ef91792dc 100644 --- a/src/flatc.cpp +++ b/src/flatc.cpp @@ -122,6 +122,7 @@ std::string FlatCompiler::GetUsageString(const char *program_name) const { " --reflect-types Add minimal type reflection to code generation.\n" " --reflect-names Add minimal type/name reflection.\n" " --root-type T Select or override the default root_type\n" + " --force-defaults Emit default values in binary output from JSON\n" "FILEs may be schemas (must end in .fbs), or JSON files (conforming to preceding\n" "schema). FILEs after the -- must be binary flatbuffer format files.\n" "Output files are named using the base file name of the input,\n" @@ -277,6 +278,8 @@ int FlatCompiler::Compile(int argc, const char **argv) { } else if (arg == "--root-type") { if (++argi >= argc) Error("missing type following" + arg, true); opts.root_type = argv[argi]; + } else if (arg == "--force-defaults") { + opts.force_defaults = true; } else { for (size_t i = 0; i < params_.num_generators; ++i) { if (arg == params_.generators[i].generator_opt_long ||