• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 
19 #include "flatbuffers/flatbuffers.h"
20 #include "flatbuffers/flexbuffers.h"
21 #include "flatbuffers/idl.h"
22 #include "flatbuffers/util.h"
23 
24 namespace flatbuffers {
25 
26 struct PrintScalarTag {};
27 struct PrintPointerTag {};
28 template<typename T> struct PrintTag { typedef PrintScalarTag type; };
29 template<> struct PrintTag<const void *> { typedef PrintPointerTag type; };
30 
31 struct JsonPrinter {
32   // If indentation is less than 0, that indicates we don't want any newlines
33   // either.
AddNewLineflatbuffers::JsonPrinter34   void AddNewLine() {
35     if (opts.indent_step >= 0) text += '\n';
36   }
37 
AddIndentflatbuffers::JsonPrinter38   void AddIndent(int ident) { text.append(ident, ' '); }
39 
Indentflatbuffers::JsonPrinter40   int Indent() const { return std::max(opts.indent_step, 0); }
41 
42   // Output an identifier with or without quotes depending on strictness.
OutputIdentifierflatbuffers::JsonPrinter43   void OutputIdentifier(const std::string &name) {
44     if (opts.strict_json) text += '\"';
45     text += name;
46     if (opts.strict_json) text += '\"';
47   }
48 
49   // Print (and its template specialization below for pointers) generate text
50   // for a single FlatBuffer value into JSON format.
51   // The general case for scalars:
52   template<typename T>
PrintScalarflatbuffers::JsonPrinter53   bool PrintScalar(T val, const Type &type, int /*indent*/) {
54     if (IsBool(type.base_type)) {
55       text += val != 0 ? "true" : "false";
56       return true;  // done
57     }
58 
59     if (opts.output_enum_identifiers && type.enum_def) {
60       const auto &enum_def = *type.enum_def;
61       if (auto ev = enum_def.ReverseLookup(static_cast<int64_t>(val))) {
62         text += '\"';
63         text += ev->name;
64         text += '\"';
65         return true;  // done
66       } else if (val && enum_def.attributes.Lookup("bit_flags")) {
67         const auto entry_len = text.length();
68         const auto u64 = static_cast<uint64_t>(val);
69         uint64_t mask = 0;
70         text += '\"';
71         for (auto it = enum_def.Vals().begin(), e = enum_def.Vals().end();
72              it != e; ++it) {
73           auto f = (*it)->GetAsUInt64();
74           if (f & u64) {
75             mask |= f;
76             text += (*it)->name;
77             text += ' ';
78           }
79         }
80         // Don't slice if (u64 != mask)
81         if (mask && (u64 == mask)) {
82           text[text.length() - 1] = '\"';
83           return true;  // done
84         }
85         text.resize(entry_len);  // restore
86       }
87       // print as numeric value
88     }
89 
90     text += NumToString(val);
91     return true;
92   }
93 
AddCommaflatbuffers::JsonPrinter94   void AddComma() {
95     if (!opts.protobuf_ascii_alike) text += ',';
96   }
97 
98   // Print a vector or an array of JSON values, comma seperated, wrapped in
99   // "[]".
100   template<typename Container>
PrintContainerflatbuffers::JsonPrinter101   bool PrintContainer(PrintScalarTag, const Container &c, size_t size,
102                       const Type &type, int indent, const uint8_t *) {
103     const auto elem_indent = indent + Indent();
104     text += '[';
105     AddNewLine();
106     for (uoffset_t i = 0; i < size; i++) {
107       if (i) {
108         AddComma();
109         AddNewLine();
110       }
111       AddIndent(elem_indent);
112       if (!PrintScalar(c[i], type, elem_indent)) { return false; }
113     }
114     AddNewLine();
115     AddIndent(indent);
116     text += ']';
117     return true;
118   }
119 
120   // Print a vector or an array of JSON values, comma seperated, wrapped in
121   // "[]".
122   template<typename Container>
PrintContainerflatbuffers::JsonPrinter123   bool PrintContainer(PrintPointerTag, const Container &c, size_t size,
124                       const Type &type, int indent, const uint8_t *prev_val) {
125     const auto is_struct = IsStruct(type);
126     const auto elem_indent = indent + Indent();
127     text += '[';
128     AddNewLine();
129     for (uoffset_t i = 0; i < size; i++) {
130       if (i) {
131         AddComma();
132         AddNewLine();
133       }
134       AddIndent(elem_indent);
135       auto ptr = is_struct ? reinterpret_cast<const void *>(
136                                  c.Data() + type.struct_def->bytesize * i)
137                            : c[i];
138       if (!PrintOffset(ptr, type, elem_indent, prev_val,
139                        static_cast<soffset_t>(i))) {
140         return false;
141       }
142     }
143     AddNewLine();
144     AddIndent(indent);
145     text += ']';
146     return true;
147   }
148 
149   template<typename T>
PrintVectorflatbuffers::JsonPrinter150   bool PrintVector(const void *val, const Type &type, int indent,
151                    const uint8_t *prev_val) {
152     typedef Vector<T> Container;
153     typedef typename PrintTag<typename Container::return_type>::type tag;
154     auto &vec = *reinterpret_cast<const Container *>(val);
155     return PrintContainer<Container>(tag(), vec, vec.size(), type, indent,
156                                      prev_val);
157   }
158 
159   // Print an array a sequence of JSON values, comma separated, wrapped in "[]".
160   template<typename T>
PrintArrayflatbuffers::JsonPrinter161   bool PrintArray(const void *val, size_t size, const Type &type, int indent) {
162     typedef Array<T, 0xFFFF> Container;
163     typedef typename PrintTag<typename Container::return_type>::type tag;
164     auto &arr = *reinterpret_cast<const Container *>(val);
165     return PrintContainer<Container>(tag(), arr, size, type, indent, nullptr);
166   }
167 
PrintOffsetflatbuffers::JsonPrinter168   bool PrintOffset(const void *val, const Type &type, int indent,
169                    const uint8_t *prev_val, soffset_t vector_index) {
170     switch (type.base_type) {
171       case BASE_TYPE_UNION: {
172         // If this assert hits, you have an corrupt buffer, a union type field
173         // was not present or was out of range.
174         FLATBUFFERS_ASSERT(prev_val);
175         auto union_type_byte = *prev_val;  // Always a uint8_t.
176         if (vector_index >= 0) {
177           auto type_vec = reinterpret_cast<const Vector<uint8_t> *>(
178               prev_val + ReadScalar<uoffset_t>(prev_val));
179           union_type_byte = type_vec->Get(static_cast<uoffset_t>(vector_index));
180         }
181         auto enum_val = type.enum_def->ReverseLookup(union_type_byte, true);
182         if (enum_val) {
183           return PrintOffset(val, enum_val->union_type, indent, nullptr, -1);
184         } else {
185           return false;
186         }
187       }
188       case BASE_TYPE_STRUCT:
189         return GenStruct(*type.struct_def, reinterpret_cast<const Table *>(val),
190                          indent);
191       case BASE_TYPE_STRING: {
192         auto s = reinterpret_cast<const String *>(val);
193         return EscapeString(s->c_str(), s->size(), &text, opts.allow_non_utf8,
194                             opts.natural_utf8);
195       }
196       case BASE_TYPE_VECTOR: {
197         const auto vec_type = type.VectorType();
198         // Call PrintVector above specifically for each element type:
199         // clang-format off
200         switch (vec_type.base_type) {
201         #define FLATBUFFERS_TD(ENUM, IDLTYPE, CTYPE, ...) \
202           case BASE_TYPE_ ## ENUM: \
203             if (!PrintVector<CTYPE>( \
204                   val, vec_type, indent, prev_val)) { \
205               return false; \
206             } \
207             break;
208           FLATBUFFERS_GEN_TYPES(FLATBUFFERS_TD)
209         #undef FLATBUFFERS_TD
210         }
211         // clang-format on
212         return true;
213       }
214       case BASE_TYPE_ARRAY: {
215         const auto vec_type = type.VectorType();
216         // Call PrintArray above specifically for each element type:
217         // clang-format off
218         switch (vec_type.base_type) {
219         #define FLATBUFFERS_TD(ENUM, IDLTYPE, CTYPE, ...) \
220           case BASE_TYPE_ ## ENUM: \
221             if (!PrintArray<CTYPE>( \
222                 val, type.fixed_length, vec_type, indent)) { \
223             return false; \
224             } \
225             break;
226             FLATBUFFERS_GEN_TYPES_SCALAR(FLATBUFFERS_TD)
227               // Arrays of scalars or structs are only possible.
228               FLATBUFFERS_GEN_TYPES_POINTER(FLATBUFFERS_TD)
229         #undef FLATBUFFERS_TD
230           case BASE_TYPE_ARRAY: FLATBUFFERS_ASSERT(0);
231         }
232         // clang-format on
233         return true;
234       }
235       default: FLATBUFFERS_ASSERT(0); return false;
236     }
237   }
238 
GetFieldDefaultflatbuffers::JsonPrinter239   template<typename T> static T GetFieldDefault(const FieldDef &fd) {
240     T val;
241     auto check = StringToNumber(fd.value.constant.c_str(), &val);
242     (void)check;
243     FLATBUFFERS_ASSERT(check);
244     return val;
245   }
246 
247   // Generate text for a scalar field.
248   template<typename T>
GenFieldflatbuffers::JsonPrinter249   bool GenField(const FieldDef &fd, const Table *table, bool fixed,
250                 int indent) {
251     if (fixed) {
252       return PrintScalar(
253           reinterpret_cast<const Struct *>(table)->GetField<T>(fd.value.offset),
254           fd.value.type, indent);
255     } else if (fd.IsOptional()) {
256       auto opt = table->GetOptional<T, T>(fd.value.offset);
257       if (opt) {
258         return PrintScalar(*opt, fd.value.type, indent);
259       } else {
260         text += "null";
261         return true;
262       }
263     } else {
264       return PrintScalar(
265           table->GetField<T>(fd.value.offset, GetFieldDefault<T>(fd)),
266           fd.value.type, indent);
267     }
268   }
269 
270   // Generate text for non-scalar field.
GenFieldOffsetflatbuffers::JsonPrinter271   bool GenFieldOffset(const FieldDef &fd, const Table *table, bool fixed,
272                       int indent, const uint8_t *prev_val) {
273     const void *val = nullptr;
274     if (fixed) {
275       // The only non-scalar fields in structs are structs or arrays.
276       FLATBUFFERS_ASSERT(IsStruct(fd.value.type) || IsArray(fd.value.type));
277       val = reinterpret_cast<const Struct *>(table)->GetStruct<const void *>(
278           fd.value.offset);
279     } else if (fd.flexbuffer && opts.json_nested_flexbuffers) {
280       // We could verify this FlexBuffer before access, but since this sits
281       // inside a FlatBuffer that we don't know wether it has been verified or
282       // not, there is little point making this part safer than the parent..
283       // The caller should really be verifying the whole.
284       // If the whole buffer is corrupt, we likely crash before we even get
285       // here.
286       auto vec = table->GetPointer<const Vector<uint8_t> *>(fd.value.offset);
287       auto root = flexbuffers::GetRoot(vec->data(), vec->size());
288       root.ToString(true, opts.strict_json, text);
289       return true;
290     } else if (fd.nested_flatbuffer && opts.json_nested_flatbuffers) {
291       auto vec = table->GetPointer<const Vector<uint8_t> *>(fd.value.offset);
292       auto root = GetRoot<Table>(vec->data());
293       return GenStruct(*fd.nested_flatbuffer, root, indent);
294     } else {
295       val = IsStruct(fd.value.type)
296                 ? table->GetStruct<const void *>(fd.value.offset)
297                 : table->GetPointer<const void *>(fd.value.offset);
298     }
299     return PrintOffset(val, fd.value.type, indent, prev_val, -1);
300   }
301 
302   // Generate text for a struct or table, values separated by commas, indented,
303   // and bracketed by "{}"
GenStructflatbuffers::JsonPrinter304   bool GenStruct(const StructDef &struct_def, const Table *table, int indent) {
305     text += '{';
306     int fieldout = 0;
307     const uint8_t *prev_val = nullptr;
308     const auto elem_indent = indent + Indent();
309     for (auto it = struct_def.fields.vec.begin();
310          it != struct_def.fields.vec.end(); ++it) {
311       FieldDef &fd = **it;
312       auto is_present = struct_def.fixed || table->CheckField(fd.value.offset);
313       auto output_anyway = (opts.output_default_scalars_in_json || fd.key) &&
314                            IsScalar(fd.value.type.base_type) && !fd.deprecated;
315       if (is_present || output_anyway) {
316         if (fieldout++) { AddComma(); }
317         AddNewLine();
318         AddIndent(elem_indent);
319         OutputIdentifier(fd.name);
320         if (!opts.protobuf_ascii_alike ||
321             (fd.value.type.base_type != BASE_TYPE_STRUCT &&
322              fd.value.type.base_type != BASE_TYPE_VECTOR))
323           text += ':';
324         text += ' ';
325         // clang-format off
326         switch (fd.value.type.base_type) {
327         #define FLATBUFFERS_TD(ENUM, IDLTYPE, CTYPE, ...) \
328           case BASE_TYPE_ ## ENUM: \
329             if (!GenField<CTYPE>(fd, table, struct_def.fixed, elem_indent)) { \
330               return false; \
331             } \
332             break;
333             FLATBUFFERS_GEN_TYPES_SCALAR(FLATBUFFERS_TD)
334         #undef FLATBUFFERS_TD
335         // Generate drop-thru case statements for all pointer types:
336         #define FLATBUFFERS_TD(ENUM, ...) \
337           case BASE_TYPE_ ## ENUM:
338               FLATBUFFERS_GEN_TYPES_POINTER(FLATBUFFERS_TD)
339               FLATBUFFERS_GEN_TYPE_ARRAY(FLATBUFFERS_TD)
340         #undef FLATBUFFERS_TD
341               if (!GenFieldOffset(fd, table, struct_def.fixed, elem_indent, prev_val)) {
342                 return false;
343               }
344             break;
345         }
346         // clang-format on
347         // Track prev val for use with union types.
348         if (struct_def.fixed) {
349           prev_val = reinterpret_cast<const uint8_t *>(table) + fd.value.offset;
350         } else {
351           prev_val = table->GetAddressOf(fd.value.offset);
352         }
353       }
354     }
355     AddNewLine();
356     AddIndent(indent);
357     text += '}';
358     return true;
359   }
360 
JsonPrinterflatbuffers::JsonPrinter361   JsonPrinter(const Parser &parser, std::string &dest)
362       : opts(parser.opts), text(dest) {
363     text.reserve(1024);  // Reduce amount of inevitable reallocs.
364   }
365 
366   const IDLOptions &opts;
367   std::string &text;
368 };
369 
GenerateTextImpl(const Parser & parser,const Table * table,const StructDef & struct_def,std::string * _text)370 static bool GenerateTextImpl(const Parser &parser, const Table *table,
371                              const StructDef &struct_def, std::string *_text) {
372   JsonPrinter printer(parser, *_text);
373   if (!printer.GenStruct(struct_def, table, 0)) { return false; }
374   printer.AddNewLine();
375   return true;
376 }
377 
378 // Generate a text representation of a flatbuffer in JSON format.
GenerateTextFromTable(const Parser & parser,const void * table,const std::string & table_name,std::string * _text)379 bool GenerateTextFromTable(const Parser &parser, const void *table,
380                            const std::string &table_name, std::string *_text) {
381   auto struct_def = parser.LookupStruct(table_name);
382   if (struct_def == nullptr) { return false; }
383   auto root = static_cast<const Table *>(table);
384   return GenerateTextImpl(parser, root, *struct_def, _text);
385 }
386 
387 // Generate a text representation of a flatbuffer in JSON format.
GenerateText(const Parser & parser,const void * flatbuffer,std::string * _text)388 bool GenerateText(const Parser &parser, const void *flatbuffer,
389                   std::string *_text) {
390   FLATBUFFERS_ASSERT(parser.root_struct_def_);  // call SetRootType()
391   auto root = parser.opts.size_prefixed ? GetSizePrefixedRoot<Table>(flatbuffer)
392                                         : GetRoot<Table>(flatbuffer);
393   return GenerateTextImpl(parser, root, *parser.root_struct_def_, _text);
394 }
395 
TextFileName(const std::string & path,const std::string & file_name)396 static std::string TextFileName(const std::string &path,
397                                 const std::string &file_name) {
398   return path + file_name + ".json";
399 }
400 
GenerateTextFile(const Parser & parser,const std::string & path,const std::string & file_name)401 bool GenerateTextFile(const Parser &parser, const std::string &path,
402                       const std::string &file_name) {
403   if (parser.opts.use_flexbuffers) {
404     std::string json;
405     parser.flex_root_.ToString(true, parser.opts.strict_json, json);
406     return flatbuffers::SaveFile(TextFileName(path, file_name).c_str(),
407                                  json.c_str(), json.size(), true);
408   }
409   if (!parser.builder_.GetSize() || !parser.root_struct_def_) return true;
410   std::string text;
411   if (!GenerateText(parser, parser.builder_.GetBufferPointer(), &text)) {
412     return false;
413   }
414   return flatbuffers::SaveFile(TextFileName(path, file_name).c_str(), text,
415                                false);
416 }
417 
TextMakeRule(const Parser & parser,const std::string & path,const std::string & file_name)418 std::string TextMakeRule(const Parser &parser, const std::string &path,
419                          const std::string &file_name) {
420   if (!parser.builder_.GetSize() || !parser.root_struct_def_) return "";
421   std::string filebase =
422       flatbuffers::StripPath(flatbuffers::StripExtension(file_name));
423   std::string make_rule = TextFileName(path, filebase) + ": " + file_name;
424   auto included_files =
425       parser.GetIncludedFilesRecursive(parser.root_struct_def_->file);
426   for (auto it = included_files.begin(); it != included_files.end(); ++it) {
427     make_rule += " " + *it;
428   }
429   return make_rule;
430 }
431 
432 }  // namespace flatbuffers
433