- `--force-defaults` : Emit default values in binary output from JSON.
+- `--force-empty` : When serializing from object API representation, force
+ strings and vectors to empty rather than null.
+
NOTE: short-form options for generators are deprecated, use the long form
whenever possible.
// for code generation.
unsigned long lang_to_generate;
+ // If set (default behavior), empty string and vector fields will be set to
+ // nullptr to make the flatbuffer more compact.
+ bool set_empty_to_null;
+
IDLOptions()
: strict_json(false),
skip_js_exports(false),
force_defaults(false),
lang(IDLOptions::kJava),
mini_reflect(IDLOptions::kNone),
- lang_to_generate(0) {}
+ lang_to_generate(0),
+ set_empty_to_null(true) {}
};
// This encapsulates where the parser is in the current source file.
" --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"
+ " --force-empty When serializing from object API representation, "
+ " force strings and vectors to empty rather than null.\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"
opts.root_type = argv[argi];
} else if (arg == "--force-defaults") {
opts.force_defaults = true;
+ } else if (arg == "--force-empty") {
+ opts.set_empty_to_null = false;
} else {
for (size_t i = 0; i < params_.num_generators; ++i) {
if (arg == params_.generators[i].generator_opt_long ||
}
std::string GenCreateParam(const FieldDef &field) {
+ const IDLOptions &opts = parser_.opts;
+
std::string value = "_o->";
if (field.value.type.base_type == BASE_TYPE_UTYPE) {
value += StripUnionType(Name(field));
code += "_fbb.CreateString(" + value + ")";
// For optional fields, check to see if there actually is any data
- // in _o->field before attempting to access it.
- if (!field.required) { code = value + ".empty() ? 0 : " + code; }
+ // in _o->field before attempting to access it. If there isn't,
+ // depending on set_empty_to_null either set it to 0 or an empty string.
+ if (!field.required) {
+ auto empty_value =
+ opts.set_empty_to_null ? "0" : "_fbb.CreateSharedString(\"\")";
+ code = value + ".empty() ? " + empty_value + " : " + code;
+ }
break;
}
// Vector fields come in several flavours, of the forms:
}
}
- // For optional fields, check to see if there actually is any data
- // in _o->field before attempting to access it.
- if (!field.required) { code = value + ".size() ? " + code + " : 0"; }
+ // If set_empty_to_null option is enabled, for optional fields, check to
+ // see if there actually is any data in _o->field before attempting to
+ // access it.
+ if (opts.set_empty_to_null && !field.required) {
+ code = value + ".size() ? " + code + " : 0";
+ }
break;
}
case BASE_TYPE_UNION: {