mirror of
https://github.com/google/flatbuffers.git
synced 2026-06-02 12:05:50 +00:00
Add --force-empty-vectors option (#5653)
The rationale for this option is that JSON clients typically want empty arrays (i.e [] in the JSON) instead of missing properties, but not empty strings when the value isn't set. --force-empty is kept as-is, i.e. it will force both empty strings and vectors. Closes #5652
This commit is contained in:
committed by
Wouter van Oortmerssen
parent
d7530ae961
commit
a8e800bd7c
@@ -2452,10 +2452,10 @@ class CppGenerator : public BaseGenerator {
|
||||
|
||||
// For optional fields, check to see if there actually is any data
|
||||
// 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.
|
||||
// depending on set_empty_strings_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(\"\")";
|
||||
opts.set_empty_strings_to_null ? "0" : "_fbb.CreateSharedString(\"\")";
|
||||
code = value + ".empty() ? " + empty_value + " : " + code;
|
||||
}
|
||||
break;
|
||||
@@ -2557,10 +2557,10 @@ class CppGenerator : public BaseGenerator {
|
||||
}
|
||||
}
|
||||
|
||||
// If set_empty_to_null option is enabled, for optional fields, check to
|
||||
// If set_empty_vectors_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) {
|
||||
if (opts.set_empty_vectors_to_null && !field.required) {
|
||||
code = value + ".size() ? " + code + " : 0";
|
||||
}
|
||||
break;
|
||||
|
||||
Reference in New Issue
Block a user