1 /*
2 * Copyright 2014 Google Inc. All rights reserved.
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17 // independent from idl_parser, since this code is not needed for most clients
18 #include "idl_gen_text.h"
19
20 #include <algorithm>
21
22 #include "flatbuffers/base.h"
23 #include "flatbuffers/code_generator.h"
24 #include "flatbuffers/flatbuffers.h"
25 #include "flatbuffers/flexbuffers.h"
26 #include "flatbuffers/idl.h"
27 #include "flatbuffers/util.h"
28
29 namespace flatbuffers {
30
31 struct PrintScalarTag {};
32 struct PrintPointerTag {};
33 template<typename T> struct PrintTag {
34 typedef PrintScalarTag type;
35 };
36 template<> struct PrintTag<const void *> {
37 typedef PrintPointerTag type;
38 };
39
40 struct JsonPrinter {
41 // If indentation is less than 0, that indicates we don't want any newlines
42 // either.
AddNewLineflatbuffers::JsonPrinter43 void AddNewLine() {
44 if (opts.indent_step >= 0) text += '\n';
45 }
46
AddIndentflatbuffers::JsonPrinter47 void AddIndent(int ident) { text.append(ident, ' '); }
48
Indentflatbuffers::JsonPrinter49 int Indent() const { return std::max(opts.indent_step, 0); }
50
51 // Output an identifier with or without quotes depending on strictness.
OutputIdentifierflatbuffers::JsonPrinter52 void OutputIdentifier(const std::string &name) {
53 if (opts.strict_json) text += '\"';
54 text += name;
55 if (opts.strict_json) text += '\"';
56 }
57
58 // Print (and its template specialization below for pointers) generate text
59 // for a single FlatBuffer value into JSON format.
60 // The general case for scalars:
61 template<typename T>
PrintScalarflatbuffers::JsonPrinter62 void PrintScalar(T val, const Type &type, int /*indent*/) {
63 if (IsBool(type.base_type)) {
64 text += val != 0 ? "true" : "false";
65 return; // done
66 }
67
68 if (opts.output_enum_identifiers && type.enum_def) {
69 const auto &enum_def = *type.enum_def;
70 if (auto ev = enum_def.ReverseLookup(static_cast<int64_t>(val))) {
71 text += '\"';
72 text += ev->name;
73 text += '\"';
74 return; // done
75 } else if (val && enum_def.attributes.Lookup("bit_flags")) {
76 const auto entry_len = text.length();
77 const auto u64 = static_cast<uint64_t>(val);
78 uint64_t mask = 0;
79 text += '\"';
80 for (auto it = enum_def.Vals().begin(), e = enum_def.Vals().end();
81 it != e; ++it) {
82 auto f = (*it)->GetAsUInt64();
83 if (f & u64) {
84 mask |= f;
85 text += (*it)->name;
86 text += ' ';
87 }
88 }
89 // Don't slice if (u64 != mask)
90 if (mask && (u64 == mask)) {
91 text[text.length() - 1] = '\"';
92 return; // done
93 }
94 text.resize(entry_len); // restore
95 }
96 // print as numeric value
97 }
98
99 text += NumToString(val);
100 return;
101 }
102
AddCommaflatbuffers::JsonPrinter103 void AddComma() {
104 if (!opts.protobuf_ascii_alike) text += ',';
105 }
106
107 // Print a vector or an array of JSON values, comma seperated, wrapped in
108 // "[]".
109 template<typename Container, typename SizeT = typename Container::size_type>
PrintContainerflatbuffers::JsonPrinter110 const char *PrintContainer(PrintScalarTag, const Container &c, SizeT size,
111 const Type &type, int indent, const uint8_t *) {
112 const auto elem_indent = indent + Indent();
113 text += '[';
114 AddNewLine();
115 for (SizeT i = 0; i < size; i++) {
116 if (i) {
117 AddComma();
118 AddNewLine();
119 }
120 AddIndent(elem_indent);
121 PrintScalar(c[i], type, elem_indent);
122 }
123 AddNewLine();
124 AddIndent(indent);
125 text += ']';
126 return nullptr;
127 }
128
129 // Print a vector or an array of JSON values, comma seperated, wrapped in
130 // "[]".
131 template<typename Container, typename SizeT = typename Container::size_type>
PrintContainerflatbuffers::JsonPrinter132 const char *PrintContainer(PrintPointerTag, const Container &c, SizeT size,
133 const Type &type, int indent,
134 const uint8_t *prev_val) {
135 const auto is_struct = IsStruct(type);
136 const auto elem_indent = indent + Indent();
137 text += '[';
138 AddNewLine();
139 for (SizeT i = 0; i < size; i++) {
140 if (i) {
141 AddComma();
142 AddNewLine();
143 }
144 AddIndent(elem_indent);
145 auto ptr = is_struct ? reinterpret_cast<const void *>(
146 c.Data() + type.struct_def->bytesize * i)
147 : c[i];
148 auto err = PrintOffset(ptr, type, elem_indent, prev_val,
149 static_cast<soffset_t>(i));
150 if (err) return err;
151 }
152 AddNewLine();
153 AddIndent(indent);
154 text += ']';
155 return nullptr;
156 }
157
158 template<typename T, typename SizeT = uoffset_t>
PrintVectorflatbuffers::JsonPrinter159 const char *PrintVector(const void *val, const Type &type, int indent,
160 const uint8_t *prev_val) {
161 typedef Vector<T, SizeT> Container;
162 typedef typename PrintTag<typename Container::return_type>::type tag;
163 auto &vec = *reinterpret_cast<const Container *>(val);
164 return PrintContainer<Container>(tag(), vec, vec.size(), type, indent,
165 prev_val);
166 }
167
168 // Print an array a sequence of JSON values, comma separated, wrapped in "[]".
169 template<typename T>
PrintArrayflatbuffers::JsonPrinter170 const char *PrintArray(const void *val, uint16_t size, const Type &type,
171
172 int indent) {
173 typedef Array<T, 0xFFFF> Container;
174 typedef typename PrintTag<typename Container::return_type>::type tag;
175 auto &arr = *reinterpret_cast<const Container *>(val);
176 return PrintContainer<Container>(tag(), arr, size, type, indent, nullptr);
177 }
178
PrintOffsetflatbuffers::JsonPrinter179 const char *PrintOffset(const void *val, const Type &type, int indent,
180 const uint8_t *prev_val, soffset_t vector_index) {
181 switch (type.base_type) {
182 case BASE_TYPE_UNION: {
183 // If this assert hits, you have an corrupt buffer, a union type field
184 // was not present or was out of range.
185 FLATBUFFERS_ASSERT(prev_val);
186 auto union_type_byte = *prev_val; // Always a uint8_t.
187 if (vector_index >= 0) {
188 auto type_vec = reinterpret_cast<const Vector<uint8_t> *>(
189 prev_val + ReadScalar<uoffset_t>(prev_val));
190 union_type_byte = type_vec->Get(static_cast<uoffset_t>(vector_index));
191 }
192 auto enum_val = type.enum_def->ReverseLookup(union_type_byte, true);
193 if (enum_val) {
194 return PrintOffset(val, enum_val->union_type, indent, nullptr, -1);
195 } else {
196 return "unknown enum value";
197 }
198 }
199 case BASE_TYPE_STRUCT:
200 return GenStruct(*type.struct_def, reinterpret_cast<const Table *>(val),
201 indent);
202 case BASE_TYPE_STRING: {
203 auto s = reinterpret_cast<const String *>(val);
204 bool ok = EscapeString(s->c_str(), s->size(), &text,
205 opts.allow_non_utf8, opts.natural_utf8);
206 return ok ? nullptr : "string contains non-utf8 bytes";
207 }
208 case BASE_TYPE_VECTOR: {
209 const auto vec_type = type.VectorType();
210 // Call PrintVector above specifically for each element type:
211 // clang-format off
212 switch (vec_type.base_type) {
213 #define FLATBUFFERS_TD(ENUM, IDLTYPE, CTYPE, ...) \
214 case BASE_TYPE_ ## ENUM: { \
215 auto err = PrintVector<CTYPE>(val, vec_type, indent, prev_val); \
216 if (err) return err; \
217 break; }
218 FLATBUFFERS_GEN_TYPES(FLATBUFFERS_TD)
219 #undef FLATBUFFERS_TD
220 }
221 // clang-format on
222 return nullptr;
223 }
224 case BASE_TYPE_ARRAY: {
225 const auto vec_type = type.VectorType();
226 // Call PrintArray above specifically for each element type:
227 // clang-format off
228 switch (vec_type.base_type) {
229 #define FLATBUFFERS_TD(ENUM, IDLTYPE, CTYPE, ...) \
230 case BASE_TYPE_ ## ENUM: { \
231 auto err = PrintArray<CTYPE>(val, type.fixed_length, vec_type, indent); \
232 if (err) return err; \
233 break; }
234 FLATBUFFERS_GEN_TYPES_SCALAR(FLATBUFFERS_TD)
235 // Arrays of scalars or structs are only possible.
236 FLATBUFFERS_GEN_TYPES_POINTER(FLATBUFFERS_TD)
237 #undef FLATBUFFERS_TD
238 case BASE_TYPE_ARRAY: FLATBUFFERS_ASSERT(0);
239 }
240 // clang-format on
241 return nullptr;
242 }
243 default: FLATBUFFERS_ASSERT(0); return "unknown type";
244 }
245 }
246
GetFieldDefaultflatbuffers::JsonPrinter247 template<typename T> static T GetFieldDefault(const FieldDef &fd) {
248 T val{};
249 auto check = StringToNumber(fd.value.constant.c_str(), &val);
250 (void)check;
251 FLATBUFFERS_ASSERT(check);
252 return val;
253 }
254
255 // Generate text for a scalar field.
256 template<typename T>
GenFieldflatbuffers::JsonPrinter257 void GenField(const FieldDef &fd, const Table *table, bool fixed,
258 int indent) {
259 if (fixed) {
260 PrintScalar(
261 reinterpret_cast<const Struct *>(table)->GetField<T>(fd.value.offset),
262 fd.value.type, indent);
263 } else if (fd.IsOptional()) {
264 auto opt = table->GetOptional<T, T>(fd.value.offset);
265 if (opt) {
266 PrintScalar(*opt, fd.value.type, indent);
267 } else {
268 text += "null";
269 }
270 } else {
271 PrintScalar(table->GetField<T>(fd.value.offset, GetFieldDefault<T>(fd)),
272 fd.value.type, indent);
273 }
274 }
275
276 // Generate text for non-scalar field.
GenFieldOffsetflatbuffers::JsonPrinter277 const char *GenFieldOffset(const FieldDef &fd, const Table *table, bool fixed,
278 int indent, const uint8_t *prev_val) {
279 const void *val = nullptr;
280 if (fixed) {
281 // The only non-scalar fields in structs are structs or arrays.
282 FLATBUFFERS_ASSERT(IsStruct(fd.value.type) || IsArray(fd.value.type));
283 val = reinterpret_cast<const Struct *>(table)->GetStruct<const void *>(
284 fd.value.offset);
285 } else if (fd.flexbuffer && opts.json_nested_flexbuffers) {
286 // We could verify this FlexBuffer before access, but since this sits
287 // inside a FlatBuffer that we don't know wether it has been verified or
288 // not, there is little point making this part safer than the parent..
289 // The caller should really be verifying the whole.
290 // If the whole buffer is corrupt, we likely crash before we even get
291 // here.
292 auto vec = table->GetPointer<const Vector<uint8_t> *>(fd.value.offset);
293 auto root = flexbuffers::GetRoot(vec->data(), vec->size());
294 root.ToString(true, opts.strict_json, text);
295 return nullptr;
296 } else if (fd.nested_flatbuffer && opts.json_nested_flatbuffers) {
297 auto vec = table->GetPointer<const Vector<uint8_t> *>(fd.value.offset);
298 auto root = GetRoot<Table>(vec->data());
299 return GenStruct(*fd.nested_flatbuffer, root, indent);
300 } else {
301 val = IsStruct(fd.value.type)
302 ? table->GetStruct<const void *>(fd.value.offset)
303 : table->GetPointer<const void *>(fd.value.offset);
304 }
305 return PrintOffset(val, fd.value.type, indent, prev_val, -1);
306 }
307
308 // Generate text for a struct or table, values separated by commas, indented,
309 // and bracketed by "{}"
GenStructflatbuffers::JsonPrinter310 const char *GenStruct(const StructDef &struct_def, const Table *table,
311 int indent) {
312 text += '{';
313 int fieldout = 0;
314 const uint8_t *prev_val = nullptr;
315 const auto elem_indent = indent + Indent();
316 for (auto it = struct_def.fields.vec.begin();
317 it != struct_def.fields.vec.end(); ++it) {
318 FieldDef &fd = **it;
319 auto is_present = struct_def.fixed || table->CheckField(fd.value.offset);
320 auto output_anyway = (opts.output_default_scalars_in_json || fd.key) &&
321 IsScalar(fd.value.type.base_type) && !fd.deprecated;
322 if (is_present || output_anyway) {
323 if (fieldout++) { AddComma(); }
324 AddNewLine();
325 AddIndent(elem_indent);
326 OutputIdentifier(fd.name);
327 if (!opts.protobuf_ascii_alike ||
328 (fd.value.type.base_type != BASE_TYPE_STRUCT &&
329 fd.value.type.base_type != BASE_TYPE_VECTOR))
330 text += ':';
331 text += ' ';
332 // clang-format off
333 switch (fd.value.type.base_type) {
334 #define FLATBUFFERS_TD(ENUM, IDLTYPE, CTYPE, ...) \
335 case BASE_TYPE_ ## ENUM: { \
336 GenField<CTYPE>(fd, table, struct_def.fixed, elem_indent); \
337 break; }
338 FLATBUFFERS_GEN_TYPES_SCALAR(FLATBUFFERS_TD)
339 #undef FLATBUFFERS_TD
340 // Generate drop-thru case statements for all pointer types:
341 #define FLATBUFFERS_TD(ENUM, ...) \
342 case BASE_TYPE_ ## ENUM:
343 FLATBUFFERS_GEN_TYPES_POINTER(FLATBUFFERS_TD)
344 FLATBUFFERS_GEN_TYPE_ARRAY(FLATBUFFERS_TD)
345 #undef FLATBUFFERS_TD
346 {
347 auto err = GenFieldOffset(fd, table, struct_def.fixed, elem_indent, prev_val);
348 if (err) return err;
349 break;
350 }
351 }
352 // clang-format on
353 // Track prev val for use with union types.
354 if (struct_def.fixed) {
355 prev_val = reinterpret_cast<const uint8_t *>(table) + fd.value.offset;
356 } else {
357 prev_val = table->GetAddressOf(fd.value.offset);
358 }
359 }
360 }
361 AddNewLine();
362 AddIndent(indent);
363 text += '}';
364 return nullptr;
365 }
366
JsonPrinterflatbuffers::JsonPrinter367 JsonPrinter(const Parser &parser, std::string &dest)
368 : opts(parser.opts), text(dest) {
369 text.reserve(1024); // Reduce amount of inevitable reallocs.
370 }
371
372 const IDLOptions &opts;
373 std::string &text;
374 };
375
GenerateTextImpl(const Parser & parser,const Table * table,const StructDef & struct_def,std::string * _text)376 static const char *GenerateTextImpl(const Parser &parser, const Table *table,
377 const StructDef &struct_def,
378 std::string *_text) {
379 JsonPrinter printer(parser, *_text);
380 auto err = printer.GenStruct(struct_def, table, 0);
381 if (err) return err;
382 printer.AddNewLine();
383 return nullptr;
384 }
385
386 // Generate a text representation of a flatbuffer in JSON format.
387 // Deprecated: please use `GenTextFromTable`
GenerateTextFromTable(const Parser & parser,const void * table,const std::string & table_name,std::string * _text)388 bool GenerateTextFromTable(const Parser &parser, const void *table,
389 const std::string &table_name,
390 std::string *_text) {
391 return GenTextFromTable(parser, table, table_name, _text) != nullptr;
392 }
393
394 // Generate a text representation of a flatbuffer in JSON format.
GenTextFromTable(const Parser & parser,const void * table,const std::string & table_name,std::string * _text)395 const char *GenTextFromTable(const Parser &parser, const void *table,
396 const std::string &table_name, std::string *_text) {
397 auto struct_def = parser.LookupStruct(table_name);
398 if (struct_def == nullptr) { return "unknown struct"; }
399 auto root = static_cast<const Table *>(table);
400 return GenerateTextImpl(parser, root, *struct_def, _text);
401 }
402
403 // Deprecated: please use `GenText`
GenerateText(const Parser & parser,const void * flatbuffer,std::string * _text)404 const char *GenerateText(const Parser &parser, const void *flatbuffer,
405 std::string *_text) {
406 return GenText(parser, flatbuffer, _text);
407 }
408
409 // Generate a text representation of a flatbuffer in JSON format.
GenText(const Parser & parser,const void * flatbuffer,std::string * _text)410 const char *GenText(const Parser &parser, const void *flatbuffer,
411 std::string *_text) {
412 FLATBUFFERS_ASSERT(parser.root_struct_def_); // call SetRootType()
413 auto root = parser.opts.size_prefixed ? GetSizePrefixedRoot<Table>(flatbuffer)
414 : GetRoot<Table>(flatbuffer);
415 return GenerateTextImpl(parser, root, *parser.root_struct_def_, _text);
416 }
417
TextFileName(const std::string & path,const std::string & file_name)418 static std::string TextFileName(const std::string &path,
419 const std::string &file_name) {
420 return path + file_name + ".json";
421 }
422
423 // Deprecated: please use `GenTextFile`
GenerateTextFile(const Parser & parser,const std::string & path,const std::string & file_name)424 const char *GenerateTextFile(const Parser &parser, const std::string &path,
425 const std::string &file_name) {
426 return GenTextFile(parser, path, file_name);
427 }
428
GenTextFile(const Parser & parser,const std::string & path,const std::string & file_name)429 const char *GenTextFile(const Parser &parser, const std::string &path,
430 const std::string &file_name) {
431 if (parser.opts.use_flexbuffers) {
432 std::string json;
433 parser.flex_root_.ToString(true, parser.opts.strict_json, json);
434 return flatbuffers::SaveFile(TextFileName(path, file_name).c_str(),
435 json.c_str(), json.size(), true)
436 ? nullptr
437 : "SaveFile failed";
438 }
439 if (!parser.builder_.GetSize() || !parser.root_struct_def_) return nullptr;
440 std::string text;
441 auto err = GenText(parser, parser.builder_.GetBufferPointer(), &text);
442 if (err) return err;
443 return flatbuffers::SaveFile(TextFileName(path, file_name).c_str(), text,
444 false)
445 ? nullptr
446 : "SaveFile failed";
447 }
448
TextMakeRule(const Parser & parser,const std::string & path,const std::string & file_name)449 static std::string TextMakeRule(const Parser &parser, const std::string &path,
450 const std::string &file_name) {
451 if (!parser.builder_.GetSize() || !parser.root_struct_def_) return "";
452 std::string filebase =
453 flatbuffers::StripPath(flatbuffers::StripExtension(file_name));
454 std::string make_rule = TextFileName(path, filebase) + ": " + file_name;
455 auto included_files =
456 parser.GetIncludedFilesRecursive(parser.root_struct_def_->file);
457 for (auto it = included_files.begin(); it != included_files.end(); ++it) {
458 make_rule += " " + *it;
459 }
460 return make_rule;
461 }
462
463 namespace {
464
465 class TextCodeGenerator : public CodeGenerator {
466 public:
GenerateCode(const Parser & parser,const std::string & path,const std::string & filename)467 Status GenerateCode(const Parser &parser, const std::string &path,
468 const std::string &filename) override {
469 auto err = GenTextFile(parser, path, filename);
470 if (err) {
471 status_detail = " (" + std::string(err) + ")";
472 return Status::ERROR;
473 }
474 return Status::OK;
475 }
476
477 // Generate code from the provided `buffer` of given `length`. The buffer is a
478 // serialized reflection.fbs.
GenerateCode(const uint8_t *,int64_t,const CodeGenOptions &)479 Status GenerateCode(const uint8_t *, int64_t,
480 const CodeGenOptions &) override {
481 return Status::NOT_IMPLEMENTED;
482 }
483
GenerateMakeRule(const Parser & parser,const std::string & path,const std::string & filename,std::string & output)484 Status GenerateMakeRule(const Parser &parser, const std::string &path,
485 const std::string &filename,
486 std::string &output) override {
487 output = TextMakeRule(parser, path, filename);
488 return Status::OK;
489 }
490
GenerateGrpcCode(const Parser & parser,const std::string & path,const std::string & filename)491 Status GenerateGrpcCode(const Parser &parser, const std::string &path,
492 const std::string &filename) override {
493 (void)parser;
494 (void)path;
495 (void)filename;
496 return Status::NOT_IMPLEMENTED;
497 }
498
GenerateRootFile(const Parser & parser,const std::string & path)499 Status GenerateRootFile(const Parser &parser,
500 const std::string &path) override {
501 (void)parser;
502 (void)path;
503 return Status::NOT_IMPLEMENTED;
504 }
505
IsSchemaOnly() const506 bool IsSchemaOnly() const override { return false; }
507
SupportsBfbsGeneration() const508 bool SupportsBfbsGeneration() const override { return false; }
509
SupportsRootFileGeneration() const510 bool SupportsRootFileGeneration() const override { return false; }
511
Language() const512 IDLOptions::Language Language() const override { return IDLOptions::kJson; }
513
LanguageName() const514 std::string LanguageName() const override { return "text"; }
515 };
516
517 } // namespace
518
NewTextCodeGenerator()519 std::unique_ptr<CodeGenerator> NewTextCodeGenerator() {
520 return std::unique_ptr<TextCodeGenerator>(new TextCodeGenerator());
521 }
522
523 } // namespace flatbuffers
524