SkJSONWriter.h 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358
  1. /*
  2. * Copyright 2017 Google Inc.
  3. *
  4. * Use of this source code is governed by a BSD-style license that can be
  5. * found in the LICENSE file.
  6. */
  7. #ifndef SkJSONWriter_DEFINED
  8. #define SkJSONWriter_DEFINED
  9. #include "include/core/SkStream.h"
  10. #include "include/private/SkNoncopyable.h"
  11. #include "include/private/SkTArray.h"
  12. /**
  13. * Lightweight class for writing properly structured JSON data. No random-access, everything must
  14. * be generated in-order. The resulting JSON is written directly to the SkWStream supplied at
  15. * construction time. Output is buffered, so writing to disk (via an SkFILEWStream) is ideal.
  16. *
  17. * There is a basic state machine to ensure that JSON is structured correctly, and to allow for
  18. * (optional) pretty formatting.
  19. *
  20. * This class adheres to the RFC-4627 usage of JSON (not ECMA-404). In other words, all JSON
  21. * created with this class must have a top-level object or array. Free-floating values of other
  22. * types are not considered valid.
  23. *
  24. * Note that all error checking is in the form of asserts - invalid usage in a non-debug build
  25. * will simply produce invalid JSON.
  26. */
  27. class SkJSONWriter : SkNoncopyable {
  28. public:
  29. enum class Mode {
  30. /**
  31. * Output the minimal amount of text. No additional whitespace (including newlines) is
  32. * generated. The resulting JSON is suitable for fast parsing and machine consumption.
  33. */
  34. kFast,
  35. /**
  36. * Output human-readable JSON, with indented objects and arrays, and one value per line.
  37. * Slightly slower than kFast, and produces data that is somewhat larger.
  38. */
  39. kPretty
  40. };
  41. /**
  42. * Construct a JSON writer that will serialize all the generated JSON to 'stream'.
  43. */
  44. SkJSONWriter(SkWStream* stream, Mode mode = Mode::kFast)
  45. : fBlock(new char[kBlockSize])
  46. , fWrite(fBlock)
  47. , fBlockEnd(fBlock + kBlockSize)
  48. , fStream(stream)
  49. , fMode(mode)
  50. , fState(State::kStart) {
  51. fScopeStack.push_back(Scope::kNone);
  52. fNewlineStack.push_back(true);
  53. }
  54. ~SkJSONWriter() {
  55. this->flush();
  56. delete[] fBlock;
  57. SkASSERT(fScopeStack.count() == 1);
  58. SkASSERT(fNewlineStack.count() == 1);
  59. }
  60. /**
  61. * Force all buffered output to be flushed to the underlying stream.
  62. */
  63. void flush() {
  64. if (fWrite != fBlock) {
  65. fStream->write(fBlock, fWrite - fBlock);
  66. fWrite = fBlock;
  67. }
  68. }
  69. /**
  70. * Append the name (key) portion of an object member. Must be called between beginObject() and
  71. * endObject(). If you have both the name and value of an object member, you can simply call
  72. * the two argument versions of the other append functions.
  73. */
  74. void appendName(const char* name) {
  75. if (!name) {
  76. return;
  77. }
  78. SkASSERT(Scope::kObject == this->scope());
  79. SkASSERT(State::kObjectBegin == fState || State::kObjectValue == fState);
  80. if (State::kObjectValue == fState) {
  81. this->write(",", 1);
  82. }
  83. this->separator(this->multiline());
  84. this->write("\"", 1);
  85. this->write(name, strlen(name));
  86. this->write("\":", 2);
  87. fState = State::kObjectName;
  88. }
  89. /**
  90. * Adds a new object. A name must be supplied when called between beginObject() and
  91. * endObject(). Calls to beginObject() must be balanced by corresponding calls to endObject().
  92. * By default, objects are written out with one named value per line (when in kPretty mode).
  93. * This can be overridden for a particular object by passing false for multiline, this will
  94. * keep the entire object on a single line. This can help with readability in some situations.
  95. * In kFast mode, this parameter is ignored.
  96. */
  97. void beginObject(const char* name = nullptr, bool multiline = true) {
  98. this->appendName(name);
  99. this->beginValue(true);
  100. this->write("{", 1);
  101. fScopeStack.push_back(Scope::kObject);
  102. fNewlineStack.push_back(multiline);
  103. fState = State::kObjectBegin;
  104. }
  105. /**
  106. * Ends an object that was previously started with beginObject().
  107. */
  108. void endObject() {
  109. SkASSERT(Scope::kObject == this->scope());
  110. SkASSERT(State::kObjectBegin == fState || State::kObjectValue == fState);
  111. bool emptyObject = State::kObjectBegin == fState;
  112. bool wasMultiline = this->multiline();
  113. this->popScope();
  114. if (!emptyObject) {
  115. this->separator(wasMultiline);
  116. }
  117. this->write("}", 1);
  118. }
  119. /**
  120. * Adds a new array. A name must be supplied when called between beginObject() and
  121. * endObject(). Calls to beginArray() must be balanced by corresponding calls to endArray().
  122. * By default, arrays are written out with one value per line (when in kPretty mode).
  123. * This can be overridden for a particular array by passing false for multiline, this will
  124. * keep the entire array on a single line. This can help with readability in some situations.
  125. * In kFast mode, this parameter is ignored.
  126. */
  127. void beginArray(const char* name = nullptr, bool multiline = true) {
  128. this->appendName(name);
  129. this->beginValue(true);
  130. this->write("[", 1);
  131. fScopeStack.push_back(Scope::kArray);
  132. fNewlineStack.push_back(multiline);
  133. fState = State::kArrayBegin;
  134. }
  135. /**
  136. * Ends an array that was previous started with beginArray().
  137. */
  138. void endArray() {
  139. SkASSERT(Scope::kArray == this->scope());
  140. SkASSERT(State::kArrayBegin == fState || State::kArrayValue == fState);
  141. bool emptyArray = State::kArrayBegin == fState;
  142. bool wasMultiline = this->multiline();
  143. this->popScope();
  144. if (!emptyArray) {
  145. this->separator(wasMultiline);
  146. }
  147. this->write("]", 1);
  148. }
  149. /**
  150. * Functions for adding values of various types. The single argument versions add un-named
  151. * values, so must be called either
  152. * - Between beginArray() and endArray() -or-
  153. * - Between beginObject() and endObject(), after calling appendName()
  154. */
  155. void appendString(const char* value) {
  156. this->beginValue();
  157. this->write("\"", 1);
  158. if (value) {
  159. while (*value) {
  160. switch (*value) {
  161. case '"': this->write("\\\"", 2); break;
  162. case '\\': this->write("\\\\", 2); break;
  163. case '\b': this->write("\\b", 2); break;
  164. case '\f': this->write("\\f", 2); break;
  165. case '\n': this->write("\\n", 2); break;
  166. case '\r': this->write("\\r", 2); break;
  167. case '\t': this->write("\\t", 2); break;
  168. default: this->write(value, 1); break;
  169. }
  170. value++;
  171. }
  172. }
  173. this->write("\"", 1);
  174. }
  175. void appendPointer(const void* value) { this->beginValue(); this->appendf("\"%p\"", value); }
  176. void appendBool(bool value) {
  177. this->beginValue();
  178. if (value) {
  179. this->write("true", 4);
  180. } else {
  181. this->write("false", 5);
  182. }
  183. }
  184. void appendS32(int32_t value) { this->beginValue(); this->appendf("%d", value); }
  185. void appendS64(int64_t value);
  186. void appendU32(uint32_t value) { this->beginValue(); this->appendf("%u", value); }
  187. void appendU64(uint64_t value);
  188. void appendFloat(float value) { this->beginValue(); this->appendf("%g", value); }
  189. void appendDouble(double value) { this->beginValue(); this->appendf("%g", value); }
  190. void appendFloatDigits(float value, int digits) {
  191. this->beginValue();
  192. this->appendf("%.*g", digits, value);
  193. }
  194. void appendDoubleDigits(double value, int digits) {
  195. this->beginValue();
  196. this->appendf("%.*g", digits, value);
  197. }
  198. void appendHexU32(uint32_t value) { this->beginValue(); this->appendf("\"0x%x\"", value); }
  199. void appendHexU64(uint64_t value);
  200. #define DEFINE_NAMED_APPEND(function, type) \
  201. void function(const char* name, type value) { this->appendName(name); this->function(value); }
  202. /**
  203. * Functions for adding named values of various types. These add a name field, so must be
  204. * called between beginObject() and endObject().
  205. */
  206. DEFINE_NAMED_APPEND(appendString, const char *)
  207. DEFINE_NAMED_APPEND(appendPointer, const void *)
  208. DEFINE_NAMED_APPEND(appendBool, bool)
  209. DEFINE_NAMED_APPEND(appendS32, int32_t)
  210. DEFINE_NAMED_APPEND(appendS64, int64_t)
  211. DEFINE_NAMED_APPEND(appendU32, uint32_t)
  212. DEFINE_NAMED_APPEND(appendU64, uint64_t)
  213. DEFINE_NAMED_APPEND(appendFloat, float)
  214. DEFINE_NAMED_APPEND(appendDouble, double)
  215. DEFINE_NAMED_APPEND(appendHexU32, uint32_t)
  216. DEFINE_NAMED_APPEND(appendHexU64, uint64_t)
  217. #undef DEFINE_NAMED_APPEND
  218. void appendFloatDigits(const char* name, float value, int digits) {
  219. this->appendName(name);
  220. this->appendFloatDigits(value, digits);
  221. }
  222. void appendDoubleDigits(const char* name, double value, int digits) {
  223. this->appendName(name);
  224. this->appendDoubleDigits(value, digits);
  225. }
  226. private:
  227. enum {
  228. // Using a 32k scratch block gives big performance wins, but we diminishing returns going
  229. // any larger. Even with a 1MB block, time to write a large (~300 MB) JSON file only drops
  230. // another ~10%.
  231. kBlockSize = 32 * 1024,
  232. };
  233. enum class Scope {
  234. kNone,
  235. kObject,
  236. kArray
  237. };
  238. enum class State {
  239. kStart,
  240. kEnd,
  241. kObjectBegin,
  242. kObjectName,
  243. kObjectValue,
  244. kArrayBegin,
  245. kArrayValue,
  246. };
  247. void appendf(const char* fmt, ...);
  248. void beginValue(bool structure = false) {
  249. SkASSERT(State::kObjectName == fState ||
  250. State::kArrayBegin == fState ||
  251. State::kArrayValue == fState ||
  252. (structure && State::kStart == fState));
  253. if (State::kArrayValue == fState) {
  254. this->write(",", 1);
  255. }
  256. if (Scope::kArray == this->scope()) {
  257. this->separator(this->multiline());
  258. } else if (Scope::kObject == this->scope() && Mode::kPretty == fMode) {
  259. this->write(" ", 1);
  260. }
  261. // We haven't added the value yet, but all (non-structure) callers emit something
  262. // immediately, so transition state, to simplify the calling code.
  263. if (!structure) {
  264. fState = Scope::kArray == this->scope() ? State::kArrayValue : State::kObjectValue;
  265. }
  266. }
  267. void separator(bool multiline) {
  268. if (Mode::kPretty == fMode) {
  269. if (multiline) {
  270. this->write("\n", 1);
  271. for (int i = 0; i < fScopeStack.count() - 1; ++i) {
  272. this->write(" ", 3);
  273. }
  274. } else {
  275. this->write(" ", 1);
  276. }
  277. }
  278. }
  279. void write(const char* buf, size_t length) {
  280. if (static_cast<size_t>(fBlockEnd - fWrite) < length) {
  281. // Don't worry about splitting writes that overflow our block.
  282. this->flush();
  283. }
  284. if (length > kBlockSize) {
  285. // Send particularly large writes straight through to the stream (unbuffered).
  286. fStream->write(buf, length);
  287. } else {
  288. memcpy(fWrite, buf, length);
  289. fWrite += length;
  290. }
  291. }
  292. Scope scope() const {
  293. SkASSERT(!fScopeStack.empty());
  294. return fScopeStack.back();
  295. }
  296. bool multiline() const {
  297. SkASSERT(!fNewlineStack.empty());
  298. return fNewlineStack.back();
  299. }
  300. void popScope() {
  301. fScopeStack.pop_back();
  302. fNewlineStack.pop_back();
  303. switch (this->scope()) {
  304. case Scope::kNone:
  305. fState = State::kEnd;
  306. break;
  307. case Scope::kObject:
  308. fState = State::kObjectValue;
  309. break;
  310. case Scope::kArray:
  311. fState = State::kArrayValue;
  312. break;
  313. default:
  314. SkDEBUGFAIL("Invalid scope");
  315. break;
  316. }
  317. }
  318. char* fBlock;
  319. char* fWrite;
  320. char* fBlockEnd;
  321. SkWStream* fStream;
  322. Mode fMode;
  323. State fState;
  324. SkSTArray<16, Scope, true> fScopeStack;
  325. SkSTArray<16, bool, true> fNewlineStack;
  326. };
  327. #endif