idl_gen_text.cpp 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375
  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. // independent from idl_parser, since this code is not needed for most clients
  17. #include "flatbuffers/flatbuffers.h"
  18. #include "flatbuffers/idl.h"
  19. #include "flatbuffers/util.h"
  20. namespace flatbuffers {
  21. static bool GenStruct(const StructDef &struct_def, const Table *table,
  22. int indent, const IDLOptions &opts,
  23. std::string *_text);
  24. // If indentation is less than 0, that indicates we don't want any newlines
  25. // either.
  26. const char *NewLine(const IDLOptions &opts) {
  27. return opts.indent_step >= 0 ? "\n" : "";
  28. }
  29. int Indent(const IDLOptions &opts) {
  30. return std::max(opts.indent_step, 0);
  31. }
  32. // Output an identifier with or without quotes depending on strictness.
  33. void OutputIdentifier(const std::string &name, const IDLOptions &opts,
  34. std::string *_text) {
  35. std::string &text = *_text;
  36. if (opts.strict_json) text += "\"";
  37. text += name;
  38. if (opts.strict_json) text += "\"";
  39. }
  40. // Print (and its template specialization below for pointers) generate text
  41. // for a single FlatBuffer value into JSON format.
  42. // The general case for scalars:
  43. template<typename T> bool Print(T val, Type type, int /*indent*/,
  44. StructDef * /*union_sd*/,
  45. const IDLOptions &opts,
  46. std::string *_text) {
  47. std::string &text = *_text;
  48. if (type.enum_def && opts.output_enum_identifiers) {
  49. auto enum_val = type.enum_def->ReverseLookup(static_cast<int>(val));
  50. if (enum_val) {
  51. OutputIdentifier(enum_val->name, opts, _text);
  52. return true;
  53. }
  54. }
  55. if (type.base_type == BASE_TYPE_BOOL) {
  56. text += val != 0 ? "true" : "false";
  57. } else {
  58. text += NumToString(val);
  59. }
  60. return true;
  61. }
  62. // Print a vector a sequence of JSON values, comma separated, wrapped in "[]".
  63. template<typename T> bool PrintVector(const Vector<T> &v, Type type,
  64. int indent, const IDLOptions &opts,
  65. std::string *_text) {
  66. std::string &text = *_text;
  67. text += "[";
  68. text += NewLine(opts);
  69. for (uoffset_t i = 0; i < v.size(); i++) {
  70. if (i) {
  71. text += ",";
  72. text += NewLine(opts);
  73. }
  74. text.append(indent + Indent(opts), ' ');
  75. if (IsStruct(type)) {
  76. if (!Print(v.GetStructFromOffset(i * type.struct_def->bytesize), type,
  77. indent + Indent(opts), nullptr, opts, _text)) {
  78. return false;
  79. }
  80. } else {
  81. if (!Print(v[i], type, indent + Indent(opts), nullptr,
  82. opts, _text)) {
  83. return false;
  84. }
  85. }
  86. }
  87. text += NewLine(opts);
  88. text.append(indent, ' ');
  89. text += "]";
  90. return true;
  91. }
  92. static bool EscapeString(const String &s, std::string *_text, const IDLOptions& opts) {
  93. std::string &text = *_text;
  94. text += "\"";
  95. for (uoffset_t i = 0; i < s.size(); i++) {
  96. char c = s[i];
  97. switch (c) {
  98. case '\n': text += "\\n"; break;
  99. case '\t': text += "\\t"; break;
  100. case '\r': text += "\\r"; break;
  101. case '\b': text += "\\b"; break;
  102. case '\f': text += "\\f"; break;
  103. case '\"': text += "\\\""; break;
  104. case '\\': text += "\\\\"; break;
  105. default:
  106. if (c >= ' ' && c <= '~') {
  107. text += c;
  108. } else {
  109. // Not printable ASCII data. Let's see if it's valid UTF-8 first:
  110. const char *utf8 = s.c_str() + i;
  111. int ucc = FromUTF8(&utf8);
  112. if (ucc < 0) {
  113. if (opts.allow_non_utf8) {
  114. text += "\\x";
  115. text += IntToStringHex(static_cast<uint8_t>(c), 2);
  116. } else {
  117. // There are two cases here:
  118. //
  119. // 1) We reached here by parsing an IDL file. In that case,
  120. // we previously checked for non-UTF-8, so we shouldn't reach
  121. // here.
  122. //
  123. // 2) We reached here by someone calling GenerateText()
  124. // on a previously-serialized flatbuffer. The data might have
  125. // non-UTF-8 Strings, or might be corrupt.
  126. //
  127. // In both cases, we have to give up and inform the caller
  128. // they have no JSON.
  129. return false;
  130. }
  131. } else {
  132. if (ucc <= 0xFFFF) {
  133. // Parses as Unicode within JSON's \uXXXX range, so use that.
  134. text += "\\u";
  135. text += IntToStringHex(ucc, 4);
  136. } else if (ucc <= 0x10FFFF) {
  137. // Encode Unicode SMP values to a surrogate pair using two \u escapes.
  138. uint32_t base = ucc - 0x10000;
  139. auto high_surrogate = (base >> 10) + 0xD800;
  140. auto low_surrogate = (base & 0x03FF) + 0xDC00;
  141. text += "\\u";
  142. text += IntToStringHex(high_surrogate, 4);
  143. text += "\\u";
  144. text += IntToStringHex(low_surrogate, 4);
  145. }
  146. // Skip past characters recognized.
  147. i = static_cast<uoffset_t>(utf8 - s.c_str() - 1);
  148. }
  149. }
  150. break;
  151. }
  152. }
  153. text += "\"";
  154. return true;
  155. }
  156. // Specialization of Print above for pointer types.
  157. template<> bool Print<const void *>(const void *val,
  158. Type type, int indent,
  159. StructDef *union_sd,
  160. const IDLOptions &opts,
  161. std::string *_text) {
  162. switch (type.base_type) {
  163. case BASE_TYPE_UNION:
  164. // If this assert hits, you have an corrupt buffer, a union type field
  165. // was not present or was out of range.
  166. assert(union_sd);
  167. if (!GenStruct(*union_sd,
  168. reinterpret_cast<const Table *>(val),
  169. indent,
  170. opts,
  171. _text)) {
  172. return false;
  173. }
  174. break;
  175. case BASE_TYPE_STRUCT:
  176. if (!GenStruct(*type.struct_def,
  177. reinterpret_cast<const Table *>(val),
  178. indent,
  179. opts,
  180. _text)) {
  181. return false;
  182. }
  183. break;
  184. case BASE_TYPE_STRING: {
  185. if (!EscapeString(*reinterpret_cast<const String *>(val), _text, opts)) {
  186. return false;
  187. }
  188. break;
  189. }
  190. case BASE_TYPE_VECTOR:
  191. type = type.VectorType();
  192. // Call PrintVector above specifically for each element type:
  193. switch (type.base_type) {
  194. #define FLATBUFFERS_TD(ENUM, IDLTYPE, CTYPE, JTYPE, GTYPE, NTYPE, \
  195. PTYPE) \
  196. case BASE_TYPE_ ## ENUM: \
  197. if (!PrintVector<CTYPE>( \
  198. *reinterpret_cast<const Vector<CTYPE> *>(val), \
  199. type, indent, opts, _text)) { \
  200. return false; \
  201. } \
  202. break;
  203. FLATBUFFERS_GEN_TYPES(FLATBUFFERS_TD)
  204. #undef FLATBUFFERS_TD
  205. }
  206. break;
  207. default: assert(0);
  208. }
  209. return true;
  210. }
  211. // Generate text for a scalar field.
  212. template<typename T> static bool GenField(const FieldDef &fd,
  213. const Table *table, bool fixed,
  214. const IDLOptions &opts,
  215. int indent,
  216. std::string *_text) {
  217. return Print(fixed ?
  218. reinterpret_cast<const Struct *>(table)->GetField<T>(fd.value.offset) :
  219. table->GetField<T>(fd.value.offset, 0), fd.value.type, indent, nullptr,
  220. opts, _text);
  221. }
  222. // Generate text for non-scalar field.
  223. static bool GenFieldOffset(const FieldDef &fd, const Table *table, bool fixed,
  224. int indent, StructDef *union_sd,
  225. const IDLOptions &opts, std::string *_text) {
  226. const void *val = nullptr;
  227. if (fixed) {
  228. // The only non-scalar fields in structs are structs.
  229. assert(IsStruct(fd.value.type));
  230. val = reinterpret_cast<const Struct *>(table)->
  231. GetStruct<const void *>(fd.value.offset);
  232. } else {
  233. val = IsStruct(fd.value.type)
  234. ? table->GetStruct<const void *>(fd.value.offset)
  235. : table->GetPointer<const void *>(fd.value.offset);
  236. }
  237. return Print(val, fd.value.type, indent, union_sd, opts, _text);
  238. }
  239. // Generate text for a struct or table, values separated by commas, indented,
  240. // and bracketed by "{}"
  241. static bool GenStruct(const StructDef &struct_def, const Table *table,
  242. int indent, const IDLOptions &opts,
  243. std::string *_text) {
  244. std::string &text = *_text;
  245. text += "{";
  246. int fieldout = 0;
  247. StructDef *union_sd = nullptr;
  248. for (auto it = struct_def.fields.vec.begin();
  249. it != struct_def.fields.vec.end();
  250. ++it) {
  251. FieldDef &fd = **it;
  252. auto is_present = struct_def.fixed || table->CheckField(fd.value.offset);
  253. auto output_anyway = opts.output_default_scalars_in_json &&
  254. IsScalar(fd.value.type.base_type) &&
  255. !fd.deprecated;
  256. if (is_present || output_anyway) {
  257. if (fieldout++) {
  258. text += ",";
  259. }
  260. text += NewLine(opts);
  261. text.append(indent + Indent(opts), ' ');
  262. OutputIdentifier(fd.name, opts, _text);
  263. text += ": ";
  264. if (is_present) {
  265. switch (fd.value.type.base_type) {
  266. #define FLATBUFFERS_TD(ENUM, IDLTYPE, CTYPE, JTYPE, GTYPE, NTYPE, \
  267. PTYPE) \
  268. case BASE_TYPE_ ## ENUM: \
  269. if (!GenField<CTYPE>(fd, table, struct_def.fixed, \
  270. opts, indent + Indent(opts), _text)) { \
  271. return false; \
  272. } \
  273. break;
  274. FLATBUFFERS_GEN_TYPES_SCALAR(FLATBUFFERS_TD)
  275. #undef FLATBUFFERS_TD
  276. // Generate drop-thru case statements for all pointer types:
  277. #define FLATBUFFERS_TD(ENUM, IDLTYPE, CTYPE, JTYPE, GTYPE, NTYPE, \
  278. PTYPE) \
  279. case BASE_TYPE_ ## ENUM:
  280. FLATBUFFERS_GEN_TYPES_POINTER(FLATBUFFERS_TD)
  281. #undef FLATBUFFERS_TD
  282. if (!GenFieldOffset(fd, table, struct_def.fixed, indent + Indent(opts),
  283. union_sd, opts, _text)) {
  284. return false;
  285. }
  286. break;
  287. }
  288. if (fd.value.type.base_type == BASE_TYPE_UTYPE) {
  289. auto enum_val = fd.value.type.enum_def->ReverseLookup(
  290. table->GetField<uint8_t>(fd.value.offset, 0));
  291. assert(enum_val);
  292. union_sd = enum_val->struct_def;
  293. }
  294. }
  295. else
  296. {
  297. text += fd.value.constant;
  298. }
  299. }
  300. }
  301. text += NewLine(opts);
  302. text.append(indent, ' ');
  303. text += "}";
  304. return true;
  305. }
  306. // Generate a text representation of a flatbuffer in JSON format.
  307. bool GenerateText(const Parser &parser, const void *flatbuffer,
  308. std::string *_text) {
  309. std::string &text = *_text;
  310. assert(parser.root_struct_def_); // call SetRootType()
  311. text.reserve(1024); // Reduce amount of inevitable reallocs.
  312. if (!GenStruct(*parser.root_struct_def_,
  313. GetRoot<Table>(flatbuffer),
  314. 0,
  315. parser.opts,
  316. _text)) {
  317. return false;
  318. }
  319. text += NewLine(parser.opts);
  320. return true;
  321. }
  322. std::string TextFileName(const std::string &path,
  323. const std::string &file_name) {
  324. return path + file_name + ".json";
  325. }
  326. bool GenerateTextFile(const Parser &parser,
  327. const std::string &path,
  328. const std::string &file_name) {
  329. if (!parser.builder_.GetSize() || !parser.root_struct_def_) return true;
  330. std::string text;
  331. if (!GenerateText(parser, parser.builder_.GetBufferPointer(), &text)) {
  332. return false;
  333. }
  334. return flatbuffers::SaveFile(TextFileName(path, file_name).c_str(),
  335. text,
  336. false);
  337. }
  338. std::string TextMakeRule(const Parser &parser,
  339. const std::string &path,
  340. const std::string &file_name) {
  341. if (!parser.builder_.GetSize() || !parser.root_struct_def_) return "";
  342. std::string filebase = flatbuffers::StripPath(
  343. flatbuffers::StripExtension(file_name));
  344. std::string make_rule = TextFileName(path, filebase) + ": " + file_name;
  345. auto included_files = parser.GetIncludedFilesRecursive(
  346. parser.root_struct_def_->file);
  347. for (auto it = included_files.begin();
  348. it != included_files.end(); ++it) {
  349. make_rule += " " + *it;
  350. }
  351. return make_rule;
  352. }
  353. } // namespace flatbuffers