// Generate text for a struct or table, values separated by commas, indented, // and bracketed by "{}" static void GenStruct(const StructDef &struct_def, const Table *table, int indent, const IDLOptions &opts, std::string *_text) { std::string &text = *_text; text += "{"; int fieldout = 0; StructDef *union_sd = nullptr; for (auto it = struct_def.fields.vec.begin(); it != struct_def.fields.vec.end(); ++it) { FieldDef &fd = **it; 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 += ","; } text += NewLine(opts); text.append(indent + Indent(opts), ' '); OutputIdentifier(fd.name, opts, _text); 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<CTYPE>(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; } if (fd.value.type.base_type == BASE_TYPE_UTYPE) { auto enum_val = fd.value.type.enum_def->ReverseLookup( table->GetField<uint8_t>(fd.value.offset, 0)); assert(enum_val); union_sd = enum_val->struct_def; } } else { text += fd.value.constant; } } } text += NewLine(opts); text.append(indent, ' '); text += "}"; }
// Print (and its template specialization below for pointers) generate text // for a single FlatBuffer value into JSON format. // The general case for scalars: template<typename T> void Print(T val, Type type, int /*indent*/, StructDef * /*union_sd*/, const GeneratorOptions &opts, std::string *_text) { std::string &text = *_text; if (type.enum_def && opts.output_enum_identifiers) { EnumVal* enum_val = type.enum_def->ReverseLookup(static_cast<int>(val)); if (enum_val) { OutputIdentifier(enum_val->name, opts, _text); return; } } text += NumToString(val); }
// Print (and its template specialization below for pointers) generate text // for a single FlatBuffer value into JSON format. // The general case for scalars: template<typename T> void Print(T val, Type type, int /*indent*/, StructDef * /*union_sd*/, const IDLOptions &opts, std::string *_text) { std::string &text = *_text; if (type.enum_def && opts.output_enum_identifiers) { auto enum_val = type.enum_def->ReverseLookup(static_cast<int>(val)); if (enum_val) { OutputIdentifier(enum_val->name, opts, _text); return; } } if (type.base_type == BASE_TYPE_BOOL) { text += val != 0 ? "true" : "false"; } else { text += NumToString(val); } }