JSONWriter.h 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460
  1. /* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
  2. /* This Source Code Form is subject to the terms of the Mozilla Public
  3. * License, v. 2.0. If a copy of the MPL was not distributed with this
  4. * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
  5. /* A JSON pretty-printer class. */
  6. // A typical JSON-writing library requires you to first build up a data
  7. // structure that represents a JSON object and then serialize it (to file, or
  8. // somewhere else). This approach makes for a clean API, but building the data
  9. // structure takes up memory. Sometimes that isn't desirable, such as when the
  10. // JSON data is produced for memory reporting.
  11. //
  12. // The JSONWriter class instead allows JSON data to be written out
  13. // incrementally without building up large data structures.
  14. //
  15. // The API is slightly uglier than you would see in a typical JSON-writing
  16. // library, but still fairly easy to use. It's possible to generate invalid
  17. // JSON with JSONWriter, but typically the most basic testing will identify any
  18. // such problems.
  19. //
  20. // Similarly, there are no RAII facilities for automatically closing objects
  21. // and arrays. These would be nice if you are generating all your code within
  22. // nested functions, but in other cases you'd have to maintain an explicit
  23. // stack of RAII objects and manually unwind it, which is no better than just
  24. // calling "end" functions. Furthermore, the consequences of forgetting to
  25. // close an object or array are obvious and, again, will be identified via
  26. // basic testing, unlike other cases where RAII is typically used (e.g. smart
  27. // pointers) and the consequences of defects are more subtle.
  28. //
  29. // Importantly, the class does solve the two hard problems of JSON
  30. // pretty-printing, which are (a) correctly escaping strings, and (b) adding
  31. // appropriate indentation and commas between items.
  32. //
  33. // By default, every property is placed on its own line. However, it is
  34. // possible to request that objects and arrays be placed entirely on a single
  35. // line, which can reduce output size significantly in some cases.
  36. //
  37. // Strings used (for property names and string property values) are |const
  38. // char*| throughout, and can be ASCII or UTF-8.
  39. //
  40. // EXAMPLE
  41. // -------
  42. // Assume that |MyWriteFunc| is a class that implements |JSONWriteFunc|. The
  43. // following code:
  44. //
  45. // JSONWriter w(MakeUnique<MyWriteFunc>());
  46. // w.Start();
  47. // {
  48. // w.NullProperty("null");
  49. // w.BoolProperty("bool", true);
  50. // w.IntProperty("int", 1);
  51. // w.StartArrayProperty("array");
  52. // {
  53. // w.StringElement("string");
  54. // w.StartObjectElement();
  55. // {
  56. // w.DoubleProperty("double", 3.4);
  57. // w.StartArrayProperty("single-line array", w.SingleLineStyle);
  58. // {
  59. // w.IntElement(1);
  60. // w.StartObjectElement(); // SingleLineStyle is inherited from
  61. // w.EndObjectElement(); // above for this collection
  62. // }
  63. // w.EndArray();
  64. // }
  65. // w.EndObjectElement();
  66. // }
  67. // w.EndArrayProperty();
  68. // }
  69. // w.End();
  70. //
  71. // will produce pretty-printed output for the following JSON object:
  72. //
  73. // {
  74. // "null": null,
  75. // "bool": true,
  76. // "int": 1,
  77. // "array": [
  78. // "string",
  79. // {
  80. // "double": 3.4,
  81. // "single-line array": [1, {}]
  82. // }
  83. // ]
  84. // }
  85. //
  86. // The nesting in the example code is obviously optional, but can aid
  87. // readability.
  88. #ifndef mozilla_JSONWriter_h
  89. #define mozilla_JSONWriter_h
  90. #include "mozilla/double-conversion.h"
  91. #include "mozilla/IntegerPrintfMacros.h"
  92. #include "mozilla/PodOperations.h"
  93. #include "mozilla/Sprintf.h"
  94. #include "mozilla/UniquePtr.h"
  95. #include "mozilla/Vector.h"
  96. #include <stdio.h>
  97. namespace mozilla {
  98. // A quasi-functor for JSONWriter. We don't use a true functor because that
  99. // requires templatizing JSONWriter, and the templatization seeps to lots of
  100. // places we don't want it to.
  101. class JSONWriteFunc
  102. {
  103. public:
  104. virtual void Write(const char* aStr) = 0;
  105. virtual ~JSONWriteFunc() {}
  106. };
  107. // Ideally this would be within |EscapedString| but when compiling with GCC
  108. // on Linux that caused link errors, whereas this formulation didn't.
  109. namespace detail {
  110. extern MFBT_DATA const char gTwoCharEscapes[256];
  111. } // namespace detail
  112. class JSONWriter
  113. {
  114. // From http://www.ietf.org/rfc/rfc4627.txt:
  115. //
  116. // "All Unicode characters may be placed within the quotation marks except
  117. // for the characters that must be escaped: quotation mark, reverse
  118. // solidus, and the control characters (U+0000 through U+001F)."
  119. //
  120. // This implementation uses two-char escape sequences where possible, namely:
  121. //
  122. // \", \\, \b, \f, \n, \r, \t
  123. //
  124. // All control characters not in the above list are represented with a
  125. // six-char escape sequence, e.g. '\u000b' (a.k.a. '\v').
  126. //
  127. class EscapedString
  128. {
  129. // Only one of |mUnownedStr| and |mOwnedStr| are ever non-null. |mIsOwned|
  130. // indicates which one is in use. They're not within a union because that
  131. // wouldn't work with UniquePtr.
  132. bool mIsOwned;
  133. const char* mUnownedStr;
  134. UniquePtr<char[]> mOwnedStr;
  135. void SanityCheck() const
  136. {
  137. MOZ_ASSERT_IF( mIsOwned, mOwnedStr.get() && !mUnownedStr);
  138. MOZ_ASSERT_IF(!mIsOwned, !mOwnedStr.get() && mUnownedStr);
  139. }
  140. static char hexDigitToAsciiChar(uint8_t u)
  141. {
  142. u = u & 0xf;
  143. return u < 10 ? '0' + u : 'a' + (u - 10);
  144. }
  145. public:
  146. explicit EscapedString(const char* aStr)
  147. : mUnownedStr(nullptr)
  148. , mOwnedStr(nullptr)
  149. {
  150. const char* p;
  151. // First, see if we need to modify the string.
  152. size_t nExtra = 0;
  153. p = aStr;
  154. while (true) {
  155. uint8_t u = *p; // ensure it can't be interpreted as negative
  156. if (u == 0) {
  157. break;
  158. }
  159. if (detail::gTwoCharEscapes[u]) {
  160. nExtra += 1;
  161. } else if (u <= 0x1f) {
  162. nExtra += 5;
  163. }
  164. p++;
  165. }
  166. if (nExtra == 0) {
  167. // No escapes needed. Easy.
  168. mIsOwned = false;
  169. mUnownedStr = aStr;
  170. return;
  171. }
  172. // Escapes are needed. We'll create a new string.
  173. mIsOwned = true;
  174. size_t len = (p - aStr) + nExtra;
  175. mOwnedStr = MakeUnique<char[]>(len + 1);
  176. p = aStr;
  177. size_t i = 0;
  178. while (true) {
  179. uint8_t u = *p; // ensure it can't be interpreted as negative
  180. if (u == 0) {
  181. mOwnedStr[i] = 0;
  182. break;
  183. }
  184. if (detail::gTwoCharEscapes[u]) {
  185. mOwnedStr[i++] = '\\';
  186. mOwnedStr[i++] = detail::gTwoCharEscapes[u];
  187. } else if (u <= 0x1f) {
  188. mOwnedStr[i++] = '\\';
  189. mOwnedStr[i++] = 'u';
  190. mOwnedStr[i++] = '0';
  191. mOwnedStr[i++] = '0';
  192. mOwnedStr[i++] = hexDigitToAsciiChar((u & 0x00f0) >> 4);
  193. mOwnedStr[i++] = hexDigitToAsciiChar(u & 0x000f);
  194. } else {
  195. mOwnedStr[i++] = u;
  196. }
  197. p++;
  198. }
  199. }
  200. ~EscapedString()
  201. {
  202. SanityCheck();
  203. }
  204. const char* get() const
  205. {
  206. SanityCheck();
  207. return mIsOwned ? mOwnedStr.get() : mUnownedStr;
  208. }
  209. };
  210. public:
  211. // Collections (objects and arrays) are printed in a multi-line style by
  212. // default. This can be changed to a single-line style if SingleLineStyle is
  213. // specified. If a collection is printed in single-line style, every nested
  214. // collection within it is also printed in single-line style, even if
  215. // multi-line style is requested.
  216. enum CollectionStyle {
  217. MultiLineStyle, // the default
  218. SingleLineStyle
  219. };
  220. protected:
  221. const UniquePtr<JSONWriteFunc> mWriter;
  222. Vector<bool, 8> mNeedComma; // do we need a comma at depth N?
  223. Vector<bool, 8> mNeedNewlines; // do we need newlines at depth N?
  224. size_t mDepth; // the current nesting depth
  225. void Indent()
  226. {
  227. for (size_t i = 0; i < mDepth; i++) {
  228. mWriter->Write(" ");
  229. }
  230. }
  231. // Adds whatever is necessary (maybe a comma, and then a newline and
  232. // whitespace) to separate an item (property or element) from what's come
  233. // before.
  234. void Separator()
  235. {
  236. if (mNeedComma[mDepth]) {
  237. mWriter->Write(",");
  238. }
  239. if (mDepth > 0 && mNeedNewlines[mDepth]) {
  240. mWriter->Write("\n");
  241. Indent();
  242. } else if (mNeedComma[mDepth]) {
  243. mWriter->Write(" ");
  244. }
  245. }
  246. void PropertyNameAndColon(const char* aName)
  247. {
  248. EscapedString escapedName(aName);
  249. mWriter->Write("\"");
  250. mWriter->Write(escapedName.get());
  251. mWriter->Write("\": ");
  252. }
  253. void Scalar(const char* aMaybePropertyName, const char* aStringValue)
  254. {
  255. Separator();
  256. if (aMaybePropertyName) {
  257. PropertyNameAndColon(aMaybePropertyName);
  258. }
  259. mWriter->Write(aStringValue);
  260. mNeedComma[mDepth] = true;
  261. }
  262. void QuotedScalar(const char* aMaybePropertyName, const char* aStringValue)
  263. {
  264. Separator();
  265. if (aMaybePropertyName) {
  266. PropertyNameAndColon(aMaybePropertyName);
  267. }
  268. mWriter->Write("\"");
  269. mWriter->Write(aStringValue);
  270. mWriter->Write("\"");
  271. mNeedComma[mDepth] = true;
  272. }
  273. void NewVectorEntries()
  274. {
  275. // If these tiny allocations OOM we might as well just crash because we
  276. // must be in serious memory trouble.
  277. MOZ_RELEASE_ASSERT(mNeedComma.resizeUninitialized(mDepth + 1));
  278. MOZ_RELEASE_ASSERT(mNeedNewlines.resizeUninitialized(mDepth + 1));
  279. mNeedComma[mDepth] = false;
  280. mNeedNewlines[mDepth] = true;
  281. }
  282. void StartCollection(const char* aMaybePropertyName, const char* aStartChar,
  283. CollectionStyle aStyle = MultiLineStyle)
  284. {
  285. Separator();
  286. if (aMaybePropertyName) {
  287. mWriter->Write("\"");
  288. mWriter->Write(aMaybePropertyName);
  289. mWriter->Write("\": ");
  290. }
  291. mWriter->Write(aStartChar);
  292. mNeedComma[mDepth] = true;
  293. mDepth++;
  294. NewVectorEntries();
  295. mNeedNewlines[mDepth] =
  296. mNeedNewlines[mDepth - 1] && aStyle == MultiLineStyle;
  297. }
  298. // Adds the whitespace and closing char necessary to end a collection.
  299. void EndCollection(const char* aEndChar)
  300. {
  301. if (mNeedNewlines[mDepth]) {
  302. mWriter->Write("\n");
  303. mDepth--;
  304. Indent();
  305. } else {
  306. mDepth--;
  307. }
  308. mWriter->Write(aEndChar);
  309. }
  310. public:
  311. explicit JSONWriter(UniquePtr<JSONWriteFunc> aWriter)
  312. : mWriter(Move(aWriter))
  313. , mNeedComma()
  314. , mNeedNewlines()
  315. , mDepth(0)
  316. {
  317. NewVectorEntries();
  318. }
  319. // Returns the JSONWriteFunc passed in at creation, for temporary use. The
  320. // JSONWriter object still owns the JSONWriteFunc.
  321. JSONWriteFunc* WriteFunc() const { return mWriter.get(); }
  322. // For all the following functions, the "Prints:" comment indicates what the
  323. // basic output looks like. However, it doesn't indicate the whitespace and
  324. // trailing commas, which are automatically added as required.
  325. //
  326. // All property names and string properties are escaped as necessary.
  327. // Prints: {
  328. void Start(CollectionStyle aStyle = MultiLineStyle)
  329. {
  330. StartCollection(nullptr, "{", aStyle);
  331. }
  332. // Prints: }
  333. void End() { EndCollection("}\n"); }
  334. // Prints: "<aName>": null
  335. void NullProperty(const char* aName)
  336. {
  337. Scalar(aName, "null");
  338. }
  339. // Prints: null
  340. void NullElement() { NullProperty(nullptr); }
  341. // Prints: "<aName>": <aBool>
  342. void BoolProperty(const char* aName, bool aBool)
  343. {
  344. Scalar(aName, aBool ? "true" : "false");
  345. }
  346. // Prints: <aBool>
  347. void BoolElement(bool aBool) { BoolProperty(nullptr, aBool); }
  348. // Prints: "<aName>": <aInt>
  349. void IntProperty(const char* aName, int64_t aInt)
  350. {
  351. char buf[64];
  352. SprintfLiteral(buf, "%" PRId64, aInt);
  353. Scalar(aName, buf);
  354. }
  355. // Prints: <aInt>
  356. void IntElement(int64_t aInt) { IntProperty(nullptr, aInt); }
  357. // Prints: "<aName>": <aDouble>
  358. void DoubleProperty(const char* aName, double aDouble)
  359. {
  360. static const size_t buflen = 64;
  361. char buf[buflen];
  362. const double_conversion::DoubleToStringConverter &converter =
  363. double_conversion::DoubleToStringConverter::EcmaScriptConverter();
  364. double_conversion::StringBuilder builder(buf, buflen);
  365. converter.ToShortest(aDouble, &builder);
  366. Scalar(aName, builder.Finalize());
  367. }
  368. // Prints: <aDouble>
  369. void DoubleElement(double aDouble) { DoubleProperty(nullptr, aDouble); }
  370. // Prints: "<aName>": "<aStr>"
  371. void StringProperty(const char* aName, const char* aStr)
  372. {
  373. EscapedString escapedStr(aStr);
  374. QuotedScalar(aName, escapedStr.get());
  375. }
  376. // Prints: "<aStr>"
  377. void StringElement(const char* aStr) { StringProperty(nullptr, aStr); }
  378. // Prints: "<aName>": [
  379. void StartArrayProperty(const char* aName,
  380. CollectionStyle aStyle = MultiLineStyle)
  381. {
  382. StartCollection(aName, "[", aStyle);
  383. }
  384. // Prints: [
  385. void StartArrayElement(CollectionStyle aStyle = MultiLineStyle)
  386. {
  387. StartArrayProperty(nullptr, aStyle);
  388. }
  389. // Prints: ]
  390. void EndArray() { EndCollection("]"); }
  391. // Prints: "<aName>": {
  392. void StartObjectProperty(const char* aName,
  393. CollectionStyle aStyle = MultiLineStyle)
  394. {
  395. StartCollection(aName, "{", aStyle);
  396. }
  397. // Prints: {
  398. void StartObjectElement(CollectionStyle aStyle = MultiLineStyle)
  399. {
  400. StartObjectProperty(nullptr, aStyle);
  401. }
  402. // Prints: }
  403. void EndObject() { EndCollection("}"); }
  404. };
  405. } // namespace mozilla
  406. #endif /* mozilla_JSONWriter_h */