[C++ ] Correctly serialize bit_flags enums to JSON with output_enum_identifiers option (#5454)

* Support output_enum_identifiers for enums with multiple bit values

* Cast bit_flag enum val to uint64_t instead of int64_t
This commit is contained in:
ll-antn
2019-08-02 00:16:44 +03:00
committed by Wouter van Oortmerssen
parent a4e3ad808e
commit 7de1a5e347
2 changed files with 43 additions and 5 deletions

View File

@@ -51,11 +51,22 @@ bool Print(T val, Type type, int /*indent*/, Type * /*union_type*/,
const IDLOptions &opts, std::string *_text) {
std::string &text = *_text;
if (type.enum_def && opts.output_enum_identifiers) {
auto ev = type.enum_def->ReverseLookup(static_cast<int64_t>(val));
if (ev) {
text += "\"";
text += ev->name;
text += "\"";
std::vector<EnumVal const *> enum_values;
if (auto ev = type.enum_def->ReverseLookup(static_cast<int64_t>(val))) {
enum_values.push_back(ev);
} else if (val && type.enum_def->attributes.Lookup("bit_flags")) {
for (auto it = type.enum_def->Vals().begin(),
e = type.enum_def->Vals().end();
it != e; ++it) {
if ((*it)->GetAsUInt64() & static_cast<uint64_t>(val))
enum_values.push_back(*it);
}
}
if (!enum_values.empty()) {
text += '\"';
for (auto it = enum_values.begin(), e = enum_values.end(); it != e; ++it)
text += (*it)->name + ' ';
text[text.length() - 1] = '\"';
return true;
}
}