You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

1253 lines
37 KiB

7 years ago
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
  1. // Copyright 2011 Baptiste Lepilleur and The JsonCpp Authors
  2. // Distributed under MIT license, or public domain if desired and
  3. // recognized in your jurisdiction.
  4. // See file LICENSE for detail or copy at http://jsoncpp.sourceforge.net/LICENSE
  5. #if !defined(JSON_IS_AMALGAMATION)
  6. #include "json_tool.h"
  7. #include <json/writer.h>
  8. #endif // if !defined(JSON_IS_AMALGAMATION)
  9. #include <cassert>
  10. #include <cstring>
  11. #include <iomanip>
  12. #include <memory>
  13. #include <set>
  14. #include <sstream>
  15. #include <utility>
  16. #if __cplusplus >= 201103L
  17. #include <cmath>
  18. #include <cstdio>
  19. #if !defined(isnan)
  20. #define isnan std::isnan
  21. #endif
  22. #if !defined(isfinite)
  23. #define isfinite std::isfinite
  24. #endif
  25. #else
  26. #include <cmath>
  27. #include <cstdio>
  28. #if defined(_MSC_VER)
  29. #if !defined(isnan)
  30. #include <float.h>
  31. #define isnan _isnan
  32. #endif
  33. #if !defined(isfinite)
  34. #include <float.h>
  35. #define isfinite _finite
  36. #endif
  37. #if !defined(_CRT_SECURE_CPP_OVERLOAD_STANDARD_NAMES)
  38. #define _CRT_SECURE_CPP_OVERLOAD_STANDARD_NAMES 1
  39. #endif //_CRT_SECURE_CPP_OVERLOAD_STANDARD_NAMES
  40. #endif //_MSC_VER
  41. #if defined(__sun) && defined(__SVR4) // Solaris
  42. #if !defined(isfinite)
  43. #include <ieeefp.h>
  44. #define isfinite finite
  45. #endif
  46. #endif
  47. #if defined(__hpux)
  48. #if !defined(isfinite)
  49. #if defined(__ia64) && !defined(finite)
  50. #define isfinite(x) \
  51. ((sizeof(x) == sizeof(float) ? _Isfinitef(x) : _IsFinite(x)))
  52. #endif
  53. #endif
  54. #endif
  55. #if !defined(isnan)
  56. // IEEE standard states that NaN values will not compare to themselves
  57. #define isnan(x) (x != x)
  58. #endif
  59. #if !defined(__APPLE__)
  60. #if !defined(isfinite)
  61. #define isfinite finite
  62. #endif
  63. #endif
  64. #endif
  65. #if defined(_MSC_VER)
  66. // Disable warning about strdup being deprecated.
  67. #pragma warning(disable : 4996)
  68. #endif
  69. namespace Json {
  70. #if __cplusplus >= 201103L || (defined(_CPPLIB_VER) && _CPPLIB_VER >= 520)
  71. typedef std::unique_ptr<StreamWriter> StreamWriterPtr;
  72. #else
  73. typedef std::auto_ptr<StreamWriter> StreamWriterPtr;
  74. #endif
  75. String valueToString(LargestInt value) {
  76. UIntToStringBuffer buffer;
  77. char* current = buffer + sizeof(buffer);
  78. if (value == Value::minLargestInt) {
  79. uintToString(LargestUInt(Value::maxLargestInt) + 1, current);
  80. *--current = '-';
  81. } else if (value < 0) {
  82. uintToString(LargestUInt(-value), current);
  83. *--current = '-';
  84. } else {
  85. uintToString(LargestUInt(value), current);
  86. }
  87. assert(current >= buffer);
  88. return current;
  89. }
  90. String valueToString(LargestUInt value) {
  91. UIntToStringBuffer buffer;
  92. char* current = buffer + sizeof(buffer);
  93. uintToString(value, current);
  94. assert(current >= buffer);
  95. return current;
  96. }
  97. #if defined(JSON_HAS_INT64)
  98. String valueToString(Int value) { return valueToString(LargestInt(value)); }
  99. String valueToString(UInt value) { return valueToString(LargestUInt(value)); }
  100. #endif // # if defined(JSON_HAS_INT64)
  101. namespace {
  102. String valueToString(double value,
  103. bool useSpecialFloats,
  104. unsigned int precision,
  105. PrecisionType precisionType) {
  106. // Print into the buffer. We need not request the alternative representation
  107. // that always has a decimal point because JSON doesn't distinguish the
  108. // concepts of reals and integers.
  109. if (!isfinite(value)) {
  110. static const char* const reps[2][3] = {{"NaN", "-Infinity", "Infinity"},
  111. {"null", "-1e+9999", "1e+9999"}};
  112. return reps[useSpecialFloats ? 0 : 1]
  113. [isnan(value) ? 0 : (value < 0) ? 1 : 2];
  114. }
  115. String buffer(size_t(36), '\0');
  116. while (true) {
  117. int len = jsoncpp_snprintf(
  118. &*buffer.begin(), buffer.size(),
  119. (precisionType == PrecisionType::significantDigits) ? "%.*g" : "%.*f",
  120. precision, value);
  121. assert(len >= 0);
  122. auto wouldPrint = static_cast<size_t>(len);
  123. if (wouldPrint >= buffer.size()) {
  124. buffer.resize(wouldPrint + 1);
  125. continue;
  126. }
  127. buffer.resize(wouldPrint);
  128. break;
  129. }
  130. buffer.erase(fixNumericLocale(buffer.begin(), buffer.end()), buffer.end());
  131. // strip the zero padding from the right
  132. if (precisionType == PrecisionType::decimalPlaces) {
  133. buffer.erase(fixZerosInTheEnd(buffer.begin(), buffer.end()), buffer.end());
  134. }
  135. // try to ensure we preserve the fact that this was given to us as a double on
  136. // input
  137. if (buffer.find('.') == buffer.npos && buffer.find('e') == buffer.npos) {
  138. buffer += ".0";
  139. }
  140. return buffer;
  141. }
  142. } // namespace
  143. String valueToString(double value,
  144. unsigned int precision,
  145. PrecisionType precisionType) {
  146. return valueToString(value, false, precision, precisionType);
  147. }
  148. String valueToString(bool value) { return value ? "true" : "false"; }
  149. static bool isAnyCharRequiredQuoting(char const* s, size_t n) {
  150. assert(s || !n);
  151. char const* const end = s + n;
  152. for (char const* cur = s; cur < end; ++cur) {
  153. if (*cur == '\\' || *cur == '\"' || *cur < ' ' ||
  154. static_cast<unsigned char>(*cur) < 0x80)
  155. return true;
  156. }
  157. return false;
  158. }
  159. static unsigned int utf8ToCodepoint(const char*& s, const char* e) {
  160. const unsigned int REPLACEMENT_CHARACTER = 0xFFFD;
  161. unsigned int firstByte = static_cast<unsigned char>(*s);
  162. if (firstByte < 0x80)
  163. return firstByte;
  164. if (firstByte < 0xE0) {
  165. if (e - s < 2)
  166. return REPLACEMENT_CHARACTER;
  167. unsigned int calculated =
  168. ((firstByte & 0x1F) << 6) | (static_cast<unsigned int>(s[1]) & 0x3F);
  169. s += 1;
  170. // oversized encoded characters are invalid
  171. return calculated < 0x80 ? REPLACEMENT_CHARACTER : calculated;
  172. }
  173. if (firstByte < 0xF0) {
  174. if (e - s < 3)
  175. return REPLACEMENT_CHARACTER;
  176. unsigned int calculated = ((firstByte & 0x0F) << 12) |
  177. ((static_cast<unsigned int>(s[1]) & 0x3F) << 6) |
  178. (static_cast<unsigned int>(s[2]) & 0x3F);
  179. s += 2;
  180. // surrogates aren't valid codepoints itself
  181. // shouldn't be UTF-8 encoded
  182. if (calculated >= 0xD800 && calculated <= 0xDFFF)
  183. return REPLACEMENT_CHARACTER;
  184. // oversized encoded characters are invalid
  185. return calculated < 0x800 ? REPLACEMENT_CHARACTER : calculated;
  186. }
  187. if (firstByte < 0xF8) {
  188. if (e - s < 4)
  189. return REPLACEMENT_CHARACTER;
  190. unsigned int calculated = ((firstByte & 0x07) << 18) |
  191. ((static_cast<unsigned int>(s[1]) & 0x3F) << 12) |
  192. ((static_cast<unsigned int>(s[2]) & 0x3F) << 6) |
  193. (static_cast<unsigned int>(s[3]) & 0x3F);
  194. s += 3;
  195. // oversized encoded characters are invalid
  196. return calculated < 0x10000 ? REPLACEMENT_CHARACTER : calculated;
  197. }
  198. return REPLACEMENT_CHARACTER;
  199. }
  200. static const char hex2[] = "000102030405060708090a0b0c0d0e0f"
  201. "101112131415161718191a1b1c1d1e1f"
  202. "202122232425262728292a2b2c2d2e2f"
  203. "303132333435363738393a3b3c3d3e3f"
  204. "404142434445464748494a4b4c4d4e4f"
  205. "505152535455565758595a5b5c5d5e5f"
  206. "606162636465666768696a6b6c6d6e6f"
  207. "707172737475767778797a7b7c7d7e7f"
  208. "808182838485868788898a8b8c8d8e8f"
  209. "909192939495969798999a9b9c9d9e9f"
  210. "a0a1a2a3a4a5a6a7a8a9aaabacadaeaf"
  211. "b0b1b2b3b4b5b6b7b8b9babbbcbdbebf"
  212. "c0c1c2c3c4c5c6c7c8c9cacbcccdcecf"
  213. "d0d1d2d3d4d5d6d7d8d9dadbdcdddedf"
  214. "e0e1e2e3e4e5e6e7e8e9eaebecedeeef"
  215. "f0f1f2f3f4f5f6f7f8f9fafbfcfdfeff";
  216. static String toHex16Bit(unsigned int x) {
  217. const unsigned int hi = (x >> 8) & 0xff;
  218. const unsigned int lo = x & 0xff;
  219. String result(4, ' ');
  220. result[0] = hex2[2 * hi];
  221. result[1] = hex2[2 * hi + 1];
  222. result[2] = hex2[2 * lo];
  223. result[3] = hex2[2 * lo + 1];
  224. return result;
  225. }
  226. static String valueToQuotedStringN(const char* value, unsigned length) {
  227. if (value == nullptr)
  228. return "";
  229. if (!isAnyCharRequiredQuoting(value, length))
  230. return String("\"") + value + "\"";
  231. // We have to walk value and escape any special characters.
  232. // Appending to String is not efficient, but this should be rare.
  233. // (Note: forward slashes are *not* rare, but I am not escaping them.)
  234. String::size_type maxsize = length * 2 + 3; // allescaped+quotes+NULL
  235. String result;
  236. result.reserve(maxsize); // to avoid lots of mallocs
  237. result += "\"";
  238. char const* end = value + length;
  239. for (const char* c = value; c != end; ++c) {
  240. switch (*c) {
  241. case '\"':
  242. result += "\\\"";
  243. break;
  244. case '\\':
  245. result += "\\\\";
  246. break;
  247. case '\b':
  248. result += "\\b";
  249. break;
  250. case '\f':
  251. result += "\\f";
  252. break;
  253. case '\n':
  254. result += "\\n";
  255. break;
  256. case '\r':
  257. result += "\\r";
  258. break;
  259. case '\t':
  260. result += "\\t";
  261. break;
  262. // case '/':
  263. // Even though \/ is considered a legal escape in JSON, a bare
  264. // slash is also legal, so I see no reason to escape it.
  265. // (I hope I am not misunderstanding something.)
  266. // blep notes: actually escaping \/ may be useful in javascript to avoid </
  267. // sequence.
  268. // Should add a flag to allow this compatibility mode and prevent this
  269. // sequence from occurring.
  270. default: {
  271. unsigned int cp = utf8ToCodepoint(c, end);
  272. // don't escape non-control characters
  273. // (short escape sequence are applied above)
  274. if (cp < 0x80 && cp >= 0x20)
  275. result += static_cast<char>(cp);
  276. else if (cp < 0x10000) { // codepoint is in Basic Multilingual Plane
  277. result += "\\u";
  278. result += toHex16Bit(cp);
  279. } else { // codepoint is not in Basic Multilingual Plane
  280. // convert to surrogate pair first
  281. cp -= 0x10000;
  282. result += "\\u";
  283. result += toHex16Bit((cp >> 10) + 0xD800);
  284. result += "\\u";
  285. result += toHex16Bit((cp & 0x3FF) + 0xDC00);
  286. }
  287. } break;
  288. }
  289. }
  290. result += "\"";
  291. return result;
  292. }
  293. String valueToQuotedString(const char* value) {
  294. return valueToQuotedStringN(value, static_cast<unsigned int>(strlen(value)));
  295. }
  296. // Class Writer
  297. // //////////////////////////////////////////////////////////////////
  298. Writer::~Writer() = default;
  299. // Class FastWriter
  300. // //////////////////////////////////////////////////////////////////
  301. FastWriter::FastWriter()
  302. = default;
  303. void FastWriter::enableYAMLCompatibility() { yamlCompatibilityEnabled_ = true; }
  304. void FastWriter::dropNullPlaceholders() { dropNullPlaceholders_ = true; }
  305. void FastWriter::omitEndingLineFeed() { omitEndingLineFeed_ = true; }
  306. String FastWriter::write(const Value& root) {
  307. document_.clear();
  308. writeValue(root);
  309. if (!omitEndingLineFeed_)
  310. document_ += '\n';
  311. return document_;
  312. }
  313. void FastWriter::writeValue(const Value& value) {
  314. switch (value.type()) {
  315. case nullValue:
  316. if (!dropNullPlaceholders_)
  317. document_ += "null";
  318. break;
  319. case intValue:
  320. document_ += valueToString(value.asLargestInt());
  321. break;
  322. case uintValue:
  323. document_ += valueToString(value.asLargestUInt());
  324. break;
  325. case realValue:
  326. document_ += valueToString(value.asDouble());
  327. break;
  328. case stringValue: {
  329. // Is NULL possible for value.string_? No.
  330. char const* str;
  331. char const* end;
  332. bool ok = value.getString(&str, &end);
  333. if (ok)
  334. document_ += valueToQuotedStringN(str, static_cast<unsigned>(end - str));
  335. break;
  336. }
  337. case booleanValue:
  338. document_ += valueToString(value.asBool());
  339. break;
  340. case arrayValue: {
  341. document_ += '[';
  342. ArrayIndex size = value.size();
  343. for (ArrayIndex index = 0; index < size; ++index) {
  344. if (index > 0)
  345. document_ += ',';
  346. writeValue(value[index]);
  347. }
  348. document_ += ']';
  349. } break;
  350. case objectValue: {
  351. Value::Members members(value.getMemberNames());
  352. document_ += '{';
  353. for (auto it = members.begin(); it != members.end(); ++it) {
  354. const String& name = *it;
  355. if (it != members.begin())
  356. document_ += ',';
  357. document_ += valueToQuotedStringN(name.data(),
  358. static_cast<unsigned>(name.length()));
  359. document_ += yamlCompatibilityEnabled_ ? ": " : ":";
  360. writeValue(value[name]);
  361. }
  362. document_ += '}';
  363. } break;
  364. }
  365. }
  366. // Class StyledWriter
  367. // //////////////////////////////////////////////////////////////////
  368. StyledWriter::StyledWriter() = default;
  369. String StyledWriter::write(const Value& root) {
  370. document_.clear();
  371. addChildValues_ = false;
  372. indentString_.clear();
  373. writeCommentBeforeValue(root);
  374. writeValue(root);
  375. writeCommentAfterValueOnSameLine(root);
  376. document_ += '\n';
  377. return document_;
  378. }
  379. void StyledWriter::writeValue(const Value& value) {
  380. switch (value.type()) {
  381. case nullValue:
  382. pushValue("null");
  383. break;
  384. case intValue:
  385. pushValue(valueToString(value.asLargestInt()));
  386. break;
  387. case uintValue:
  388. pushValue(valueToString(value.asLargestUInt()));
  389. break;
  390. case realValue:
  391. pushValue(valueToString(value.asDouble()));
  392. break;
  393. case stringValue: {
  394. // Is NULL possible for value.string_? No.
  395. char const* str;
  396. char const* end;
  397. bool ok = value.getString(&str, &end);
  398. if (ok)
  399. pushValue(valueToQuotedStringN(str, static_cast<unsigned>(end - str)));
  400. else
  401. pushValue("");
  402. break;
  403. }
  404. case booleanValue:
  405. pushValue(valueToString(value.asBool()));
  406. break;
  407. case arrayValue:
  408. writeArrayValue(value);
  409. break;
  410. case objectValue: {
  411. Value::Members members(value.getMemberNames());
  412. if (members.empty())
  413. pushValue("{}");
  414. else {
  415. writeWithIndent("{");
  416. indent();
  417. auto it = members.begin();
  418. for (;;) {
  419. const String& name = *it;
  420. const Value& childValue = value[name];
  421. writeCommentBeforeValue(childValue);
  422. writeWithIndent(valueToQuotedString(name.c_str()));
  423. document_ += " : ";
  424. writeValue(childValue);
  425. if (++it == members.end()) {
  426. writeCommentAfterValueOnSameLine(childValue);
  427. break;
  428. }
  429. document_ += ',';
  430. writeCommentAfterValueOnSameLine(childValue);
  431. }
  432. unindent();
  433. writeWithIndent("}");
  434. }
  435. } break;
  436. }
  437. }
  438. void StyledWriter::writeArrayValue(const Value& value) {
  439. unsigned size = value.size();
  440. if (size == 0)
  441. pushValue("[]");
  442. else {
  443. bool isArrayMultiLine = isMultilineArray(value);
  444. if (isArrayMultiLine) {
  445. writeWithIndent("[");
  446. indent();
  447. bool hasChildValue = !childValues_.empty();
  448. unsigned index = 0;
  449. for (;;) {
  450. const Value& childValue = value[index];
  451. writeCommentBeforeValue(childValue);
  452. if (hasChildValue)
  453. writeWithIndent(childValues_[index]);
  454. else {
  455. writeIndent();
  456. writeValue(childValue);
  457. }
  458. if (++index == size) {
  459. writeCommentAfterValueOnSameLine(childValue);
  460. break;
  461. }
  462. document_ += ',';
  463. writeCommentAfterValueOnSameLine(childValue);
  464. }
  465. unindent();
  466. writeWithIndent("]");
  467. } else // output on a single line
  468. {
  469. assert(childValues_.size() == size);
  470. document_ += "[ ";
  471. for (unsigned index = 0; index < size; ++index) {
  472. if (index > 0)
  473. document_ += ", ";
  474. document_ += childValues_[index];
  475. }
  476. document_ += " ]";
  477. }
  478. }
  479. }
  480. bool StyledWriter::isMultilineArray(const Value& value) {
  481. ArrayIndex const size = value.size();
  482. bool isMultiLine = size * 3 >= rightMargin_;
  483. childValues_.clear();
  484. for (ArrayIndex index = 0; index < size && !isMultiLine; ++index) {
  485. const Value& childValue = value[index];
  486. isMultiLine = ((childValue.isArray() || childValue.isObject()) &&
  487. !childValue.empty());
  488. }
  489. if (!isMultiLine) // check if line length > max line length
  490. {
  491. childValues_.reserve(size);
  492. addChildValues_ = true;
  493. ArrayIndex lineLength = 4 + (size - 1) * 2; // '[ ' + ', '*n + ' ]'
  494. for (ArrayIndex index = 0; index < size; ++index) {
  495. if (hasCommentForValue(value[index])) {
  496. isMultiLine = true;
  497. }
  498. writeValue(value[index]);
  499. lineLength += static_cast<ArrayIndex>(childValues_[index].length());
  500. }
  501. addChildValues_ = false;
  502. isMultiLine = isMultiLine || lineLength >= rightMargin_;
  503. }
  504. return isMultiLine;
  505. }
  506. void StyledWriter::pushValue(const String& value) {
  507. if (addChildValues_)
  508. childValues_.push_back(value);
  509. else
  510. document_ += value;
  511. }
  512. void StyledWriter::writeIndent() {
  513. if (!document_.empty()) {
  514. char last = document_[document_.length() - 1];
  515. if (last == ' ') // already indented
  516. return;
  517. if (last != '\n') // Comments may add new-line
  518. document_ += '\n';
  519. }
  520. document_ += indentString_;
  521. }
  522. void StyledWriter::writeWithIndent(const String& value) {
  523. writeIndent();
  524. document_ += value;
  525. }
  526. void StyledWriter::indent() { indentString_ += String(indentSize_, ' '); }
  527. void StyledWriter::unindent() {
  528. assert(indentString_.size() >= indentSize_);
  529. indentString_.resize(indentString_.size() - indentSize_);
  530. }
  531. void StyledWriter::writeCommentBeforeValue(const Value& root) {
  532. if (!root.hasComment(commentBefore))
  533. return;
  534. document_ += '\n';
  535. writeIndent();
  536. const String& comment = root.getComment(commentBefore);
  537. String::const_iterator iter = comment.begin();
  538. while (iter != comment.end()) {
  539. document_ += *iter;
  540. if (*iter == '\n' && ((iter + 1) != comment.end() && *(iter + 1) == '/'))
  541. writeIndent();
  542. ++iter;
  543. }
  544. // Comments are stripped of trailing newlines, so add one here
  545. document_ += '\n';
  546. }
  547. void StyledWriter::writeCommentAfterValueOnSameLine(const Value& root) {
  548. if (root.hasComment(commentAfterOnSameLine))
  549. document_ += " " + root.getComment(commentAfterOnSameLine);
  550. if (root.hasComment(commentAfter)) {
  551. document_ += '\n';
  552. document_ += root.getComment(commentAfter);
  553. document_ += '\n';
  554. }
  555. }
  556. bool StyledWriter::hasCommentForValue(const Value& value) {
  557. return value.hasComment(commentBefore) ||
  558. value.hasComment(commentAfterOnSameLine) ||
  559. value.hasComment(commentAfter);
  560. }
  561. // Class StyledStreamWriter
  562. // //////////////////////////////////////////////////////////////////
  563. StyledStreamWriter::StyledStreamWriter(String indentation)
  564. : document_(nullptr), indentation_(std::move(indentation)),
  565. addChildValues_(), indented_(false) {}
  566. void StyledStreamWriter::write(OStream& out, const Value& root) {
  567. document_ = &out;
  568. addChildValues_ = false;
  569. indentString_.clear();
  570. indented_ = true;
  571. writeCommentBeforeValue(root);
  572. if (!indented_)
  573. writeIndent();
  574. indented_ = true;
  575. writeValue(root);
  576. writeCommentAfterValueOnSameLine(root);
  577. *document_ << "\n";
  578. document_ = nullptr; // Forget the stream, for safety.
  579. }
  580. void StyledStreamWriter::writeValue(const Value& value) {
  581. switch (value.type()) {
  582. case nullValue:
  583. pushValue("null");
  584. break;
  585. case intValue:
  586. pushValue(valueToString(value.asLargestInt()));
  587. break;
  588. case uintValue:
  589. pushValue(valueToString(value.asLargestUInt()));
  590. break;
  591. case realValue:
  592. pushValue(valueToString(value.asDouble()));
  593. break;
  594. case stringValue: {
  595. // Is NULL possible for value.string_? No.
  596. char const* str;
  597. char const* end;
  598. bool ok = value.getString(&str, &end);
  599. if (ok)
  600. pushValue(valueToQuotedStringN(str, static_cast<unsigned>(end - str)));
  601. else
  602. pushValue("");
  603. break;
  604. }
  605. case booleanValue:
  606. pushValue(valueToString(value.asBool()));
  607. break;
  608. case arrayValue:
  609. writeArrayValue(value);
  610. break;
  611. case objectValue: {
  612. Value::Members members(value.getMemberNames());
  613. if (members.empty())
  614. pushValue("{}");
  615. else {
  616. writeWithIndent("{");
  617. indent();
  618. auto it = members.begin();
  619. for (;;) {
  620. const String& name = *it;
  621. const Value& childValue = value[name];
  622. writeCommentBeforeValue(childValue);
  623. writeWithIndent(valueToQuotedString(name.c_str()));
  624. *document_ << " : ";
  625. writeValue(childValue);
  626. if (++it == members.end()) {
  627. writeCommentAfterValueOnSameLine(childValue);
  628. break;
  629. }
  630. *document_ << ",";
  631. writeCommentAfterValueOnSameLine(childValue);
  632. }
  633. unindent();
  634. writeWithIndent("}");
  635. }
  636. } break;
  637. }
  638. }
  639. void StyledStreamWriter::writeArrayValue(const Value& value) {
  640. unsigned size = value.size();
  641. if (size == 0)
  642. pushValue("[]");
  643. else {
  644. bool isArrayMultiLine = isMultilineArray(value);
  645. if (isArrayMultiLine) {
  646. writeWithIndent("[");
  647. indent();
  648. bool hasChildValue = !childValues_.empty();
  649. unsigned index = 0;
  650. for (;;) {
  651. const Value& childValue = value[index];
  652. writeCommentBeforeValue(childValue);
  653. if (hasChildValue)
  654. writeWithIndent(childValues_[index]);
  655. else {
  656. if (!indented_)
  657. writeIndent();
  658. indented_ = true;
  659. writeValue(childValue);
  660. indented_ = false;
  661. }
  662. if (++index == size) {
  663. writeCommentAfterValueOnSameLine(childValue);
  664. break;
  665. }
  666. *document_ << ",";
  667. writeCommentAfterValueOnSameLine(childValue);
  668. }
  669. unindent();
  670. writeWithIndent("]");
  671. } else // output on a single line
  672. {
  673. assert(childValues_.size() == size);
  674. *document_ << "[ ";
  675. for (unsigned index = 0; index < size; ++index) {
  676. if (index > 0)
  677. *document_ << ", ";
  678. *document_ << childValues_[index];
  679. }
  680. *document_ << " ]";
  681. }
  682. }
  683. }
  684. bool StyledStreamWriter::isMultilineArray(const Value& value) {
  685. ArrayIndex const size = value.size();
  686. bool isMultiLine = size * 3 >= rightMargin_;
  687. childValues_.clear();
  688. for (ArrayIndex index = 0; index < size && !isMultiLine; ++index) {
  689. const Value& childValue = value[index];
  690. isMultiLine = ((childValue.isArray() || childValue.isObject()) &&
  691. !childValue.empty());
  692. }
  693. if (!isMultiLine) // check if line length > max line length
  694. {
  695. childValues_.reserve(size);
  696. addChildValues_ = true;
  697. ArrayIndex lineLength = 4 + (size - 1) * 2; // '[ ' + ', '*n + ' ]'
  698. for (ArrayIndex index = 0; index < size; ++index) {
  699. if (hasCommentForValue(value[index])) {
  700. isMultiLine = true;
  701. }
  702. writeValue(value[index]);
  703. lineLength += static_cast<ArrayIndex>(childValues_[index].length());
  704. }
  705. addChildValues_ = false;
  706. isMultiLine = isMultiLine || lineLength >= rightMargin_;
  707. }
  708. return isMultiLine;
  709. }
  710. void StyledStreamWriter::pushValue(const String& value) {
  711. if (addChildValues_)
  712. childValues_.push_back(value);
  713. else
  714. *document_ << value;
  715. }
  716. void StyledStreamWriter::writeIndent() {
  717. // blep intended this to look at the so-far-written string
  718. // to determine whether we are already indented, but
  719. // with a stream we cannot do that. So we rely on some saved state.
  720. // The caller checks indented_.
  721. *document_ << '\n' << indentString_;
  722. }
  723. void StyledStreamWriter::writeWithIndent(const String& value) {
  724. if (!indented_)
  725. writeIndent();
  726. *document_ << value;
  727. indented_ = false;
  728. }
  729. void StyledStreamWriter::indent() { indentString_ += indentation_; }
  730. void StyledStreamWriter::unindent() {
  731. assert(indentString_.size() >= indentation_.size());
  732. indentString_.resize(indentString_.size() - indentation_.size());
  733. }
  734. void StyledStreamWriter::writeCommentBeforeValue(const Value& root) {
  735. if (!root.hasComment(commentBefore))
  736. return;
  737. if (!indented_)
  738. writeIndent();
  739. const String& comment = root.getComment(commentBefore);
  740. String::const_iterator iter = comment.begin();
  741. while (iter != comment.end()) {
  742. *document_ << *iter;
  743. if (*iter == '\n' && ((iter + 1) != comment.end() && *(iter + 1) == '/'))
  744. // writeIndent(); // would include newline
  745. *document_ << indentString_;
  746. ++iter;
  747. }
  748. indented_ = false;
  749. }
  750. void StyledStreamWriter::writeCommentAfterValueOnSameLine(const Value& root) {
  751. if (root.hasComment(commentAfterOnSameLine))
  752. *document_ << ' ' << root.getComment(commentAfterOnSameLine);
  753. if (root.hasComment(commentAfter)) {
  754. writeIndent();
  755. *document_ << root.getComment(commentAfter);
  756. }
  757. indented_ = false;
  758. }
  759. bool StyledStreamWriter::hasCommentForValue(const Value& value) {
  760. return value.hasComment(commentBefore) ||
  761. value.hasComment(commentAfterOnSameLine) ||
  762. value.hasComment(commentAfter);
  763. }
  764. //////////////////////////
  765. // BuiltStyledStreamWriter
  766. /// Scoped enums are not available until C++11.
  767. struct CommentStyle {
  768. /// Decide whether to write comments.
  769. enum Enum {
  770. None, ///< Drop all comments.
  771. Most, ///< Recover odd behavior of previous versions (not implemented yet).
  772. All ///< Keep all comments.
  773. };
  774. };
  775. struct BuiltStyledStreamWriter : public StreamWriter {
  776. BuiltStyledStreamWriter(String indentation,
  777. CommentStyle::Enum cs,
  778. String colonSymbol,
  779. String nullSymbol,
  780. String endingLineFeedSymbol,
  781. bool useSpecialFloats,
  782. unsigned int precision,
  783. PrecisionType precisionType);
  784. int write(Value const& root, OStream* sout) override;
  785. private:
  786. void writeValue(Value const& value);
  787. void writeArrayValue(Value const& value);
  788. bool isMultilineArray(Value const& value);
  789. void pushValue(String const& value);
  790. void writeIndent();
  791. void writeWithIndent(String const& value);
  792. void indent();
  793. void unindent();
  794. void writeCommentBeforeValue(Value const& root);
  795. void writeCommentAfterValueOnSameLine(Value const& root);
  796. static bool hasCommentForValue(const Value& value);
  797. typedef std::vector<String> ChildValues;
  798. ChildValues childValues_;
  799. String indentString_;
  800. unsigned int rightMargin_;
  801. String indentation_;
  802. CommentStyle::Enum cs_;
  803. String colonSymbol_;
  804. String nullSymbol_;
  805. String endingLineFeedSymbol_;
  806. bool addChildValues_ : 1;
  807. bool indented_ : 1;
  808. bool useSpecialFloats_ : 1;
  809. unsigned int precision_;
  810. PrecisionType precisionType_;
  811. };
  812. BuiltStyledStreamWriter::BuiltStyledStreamWriter(String indentation,
  813. CommentStyle::Enum cs,
  814. String colonSymbol,
  815. String nullSymbol,
  816. String endingLineFeedSymbol,
  817. bool useSpecialFloats,
  818. unsigned int precision,
  819. PrecisionType precisionType)
  820. : rightMargin_(74), indentation_(std::move(indentation)), cs_(cs),
  821. colonSymbol_(std::move(colonSymbol)), nullSymbol_(std::move(nullSymbol)),
  822. endingLineFeedSymbol_(std::move(endingLineFeedSymbol)),
  823. addChildValues_(false), indented_(false),
  824. useSpecialFloats_(useSpecialFloats), precision_(precision),
  825. precisionType_(precisionType) {}
  826. int BuiltStyledStreamWriter::write(Value const& root, OStream* sout) {
  827. sout_ = sout;
  828. addChildValues_ = false;
  829. indented_ = true;
  830. indentString_.clear();
  831. writeCommentBeforeValue(root);
  832. if (!indented_)
  833. writeIndent();
  834. indented_ = true;
  835. writeValue(root);
  836. writeCommentAfterValueOnSameLine(root);
  837. *sout_ << endingLineFeedSymbol_;
  838. sout_ = nullptr;
  839. return 0;
  840. }
  841. void BuiltStyledStreamWriter::writeValue(Value const& value) {
  842. switch (value.type()) {
  843. case nullValue:
  844. pushValue(nullSymbol_);
  845. break;
  846. case intValue:
  847. pushValue(valueToString(value.asLargestInt()));
  848. break;
  849. case uintValue:
  850. pushValue(valueToString(value.asLargestUInt()));
  851. break;
  852. case realValue:
  853. pushValue(valueToString(value.asDouble(), useSpecialFloats_, precision_,
  854. precisionType_));
  855. break;
  856. case stringValue: {
  857. // Is NULL is possible for value.string_? No.
  858. char const* str;
  859. char const* end;
  860. bool ok = value.getString(&str, &end);
  861. if (ok)
  862. pushValue(valueToQuotedStringN(str, static_cast<unsigned>(end - str)));
  863. else
  864. pushValue("");
  865. break;
  866. }
  867. case booleanValue:
  868. pushValue(valueToString(value.asBool()));
  869. break;
  870. case arrayValue:
  871. writeArrayValue(value);
  872. break;
  873. case objectValue: {
  874. Value::Members members(value.getMemberNames());
  875. if (members.empty())
  876. pushValue("{}");
  877. else {
  878. writeWithIndent("{");
  879. indent();
  880. auto it = members.begin();
  881. for (;;) {
  882. String const& name = *it;
  883. Value const& childValue = value[name];
  884. writeCommentBeforeValue(childValue);
  885. writeWithIndent(valueToQuotedStringN(
  886. name.data(), static_cast<unsigned>(name.length())));
  887. *sout_ << colonSymbol_;
  888. writeValue(childValue);
  889. if (++it == members.end()) {
  890. writeCommentAfterValueOnSameLine(childValue);
  891. break;
  892. }
  893. *sout_ << ",";
  894. writeCommentAfterValueOnSameLine(childValue);
  895. }
  896. unindent();
  897. writeWithIndent("}");
  898. }
  899. } break;
  900. }
  901. }
  902. void BuiltStyledStreamWriter::writeArrayValue(Value const& value) {
  903. unsigned size = value.size();
  904. if (size == 0)
  905. pushValue("[]");
  906. else {
  907. bool isMultiLine = (cs_ == CommentStyle::All) || isMultilineArray(value);
  908. if (isMultiLine) {
  909. writeWithIndent("[");
  910. indent();
  911. bool hasChildValue = !childValues_.empty();
  912. unsigned index = 0;
  913. for (;;) {
  914. Value const& childValue = value[index];
  915. writeCommentBeforeValue(childValue);
  916. if (hasChildValue)
  917. writeWithIndent(childValues_[index]);
  918. else {
  919. if (!indented_)
  920. writeIndent();
  921. indented_ = true;
  922. writeValue(childValue);
  923. indented_ = false;
  924. }
  925. if (++index == size) {
  926. writeCommentAfterValueOnSameLine(childValue);
  927. break;
  928. }
  929. *sout_ << ",";
  930. writeCommentAfterValueOnSameLine(childValue);
  931. }
  932. unindent();
  933. writeWithIndent("]");
  934. } else // output on a single line
  935. {
  936. assert(childValues_.size() == size);
  937. *sout_ << "[";
  938. if (!indentation_.empty())
  939. *sout_ << " ";
  940. for (unsigned index = 0; index < size; ++index) {
  941. if (index > 0)
  942. *sout_ << ((!indentation_.empty()) ? ", " : ",");
  943. *sout_ << childValues_[index];
  944. }
  945. if (!indentation_.empty())
  946. *sout_ << " ";
  947. *sout_ << "]";
  948. }
  949. }
  950. }
  951. bool BuiltStyledStreamWriter::isMultilineArray(Value const& value) {
  952. ArrayIndex const size = value.size();
  953. bool isMultiLine = size * 3 >= rightMargin_;
  954. childValues_.clear();
  955. for (ArrayIndex index = 0; index < size && !isMultiLine; ++index) {
  956. Value const& childValue = value[index];
  957. isMultiLine = ((childValue.isArray() || childValue.isObject()) &&
  958. !childValue.empty());
  959. }
  960. if (!isMultiLine) // check if line length > max line length
  961. {
  962. childValues_.reserve(size);
  963. addChildValues_ = true;
  964. ArrayIndex lineLength = 4 + (size - 1) * 2; // '[ ' + ', '*n + ' ]'
  965. for (ArrayIndex index = 0; index < size; ++index) {
  966. if (hasCommentForValue(value[index])) {
  967. isMultiLine = true;
  968. }
  969. writeValue(value[index]);
  970. lineLength += static_cast<ArrayIndex>(childValues_[index].length());
  971. }
  972. addChildValues_ = false;
  973. isMultiLine = isMultiLine || lineLength >= rightMargin_;
  974. }
  975. return isMultiLine;
  976. }
  977. void BuiltStyledStreamWriter::pushValue(String const& value) {
  978. if (addChildValues_)
  979. childValues_.push_back(value);
  980. else
  981. *sout_ << value;
  982. }
  983. void BuiltStyledStreamWriter::writeIndent() {
  984. // blep intended this to look at the so-far-written string
  985. // to determine whether we are already indented, but
  986. // with a stream we cannot do that. So we rely on some saved state.
  987. // The caller checks indented_.
  988. if (!indentation_.empty()) {
  989. // In this case, drop newlines too.
  990. *sout_ << '\n' << indentString_;
  991. }
  992. }
  993. void BuiltStyledStreamWriter::writeWithIndent(String const& value) {
  994. if (!indented_)
  995. writeIndent();
  996. *sout_ << value;
  997. indented_ = false;
  998. }
  999. void BuiltStyledStreamWriter::indent() { indentString_ += indentation_; }
  1000. void BuiltStyledStreamWriter::unindent() {
  1001. assert(indentString_.size() >= indentation_.size());
  1002. indentString_.resize(indentString_.size() - indentation_.size());
  1003. }
  1004. void BuiltStyledStreamWriter::writeCommentBeforeValue(Value const& root) {
  1005. if (cs_ == CommentStyle::None)
  1006. return;
  1007. if (!root.hasComment(commentBefore))
  1008. return;
  1009. if (!indented_)
  1010. writeIndent();
  1011. const String& comment = root.getComment(commentBefore);
  1012. String::const_iterator iter = comment.begin();
  1013. while (iter != comment.end()) {
  1014. *sout_ << *iter;
  1015. if (*iter == '\n' && ((iter + 1) != comment.end() && *(iter + 1) == '/'))
  1016. // writeIndent(); // would write extra newline
  1017. *sout_ << indentString_;
  1018. ++iter;
  1019. }
  1020. indented_ = false;
  1021. }
  1022. void BuiltStyledStreamWriter::writeCommentAfterValueOnSameLine(
  1023. Value const& root) {
  1024. if (cs_ == CommentStyle::None)
  1025. return;
  1026. if (root.hasComment(commentAfterOnSameLine))
  1027. *sout_ << " " + root.getComment(commentAfterOnSameLine);
  1028. if (root.hasComment(commentAfter)) {
  1029. writeIndent();
  1030. *sout_ << root.getComment(commentAfter);
  1031. }
  1032. }
  1033. // static
  1034. bool BuiltStyledStreamWriter::hasCommentForValue(const Value& value) {
  1035. return value.hasComment(commentBefore) ||
  1036. value.hasComment(commentAfterOnSameLine) ||
  1037. value.hasComment(commentAfter);
  1038. }
  1039. ///////////////
  1040. // StreamWriter
  1041. StreamWriter::StreamWriter() : sout_(nullptr) {}
  1042. StreamWriter::~StreamWriter() = default;
  1043. StreamWriter::Factory::~Factory() = default;
  1044. StreamWriterBuilder::StreamWriterBuilder() { setDefaults(&settings_); }
  1045. StreamWriterBuilder::~StreamWriterBuilder() = default;
  1046. StreamWriter* StreamWriterBuilder::newStreamWriter() const {
  1047. String indentation = settings_["indentation"].asString();
  1048. String cs_str = settings_["commentStyle"].asString();
  1049. String pt_str = settings_["precisionType"].asString();
  1050. bool eyc = settings_["enableYAMLCompatibility"].asBool();
  1051. bool dnp = settings_["dropNullPlaceholders"].asBool();
  1052. bool usf = settings_["useSpecialFloats"].asBool();
  1053. unsigned int pre = settings_["precision"].asUInt();
  1054. CommentStyle::Enum cs = CommentStyle::All;
  1055. if (cs_str == "All") {
  1056. cs = CommentStyle::All;
  1057. } else if (cs_str == "None") {
  1058. cs = CommentStyle::None;
  1059. } else {
  1060. throwRuntimeError("commentStyle must be 'All' or 'None'");
  1061. }
  1062. PrecisionType precisionType(significantDigits);
  1063. if (pt_str == "significant") {
  1064. precisionType = PrecisionType::significantDigits;
  1065. } else if (pt_str == "decimal") {
  1066. precisionType = PrecisionType::decimalPlaces;
  1067. } else {
  1068. throwRuntimeError("precisionType must be 'significant' or 'decimal'");
  1069. }
  1070. String colonSymbol = " : ";
  1071. if (eyc) {
  1072. colonSymbol = ": ";
  1073. } else if (indentation.empty()) {
  1074. colonSymbol = ":";
  1075. }
  1076. String nullSymbol = "null";
  1077. if (dnp) {
  1078. nullSymbol.clear();
  1079. }
  1080. if (pre > 17)
  1081. pre = 17;
  1082. String endingLineFeedSymbol;
  1083. return new BuiltStyledStreamWriter(indentation, cs, colonSymbol, nullSymbol,
  1084. endingLineFeedSymbol, usf, pre,
  1085. precisionType);
  1086. }
  1087. static void getValidWriterKeys(std::set<String>* valid_keys) {
  1088. valid_keys->clear();
  1089. valid_keys->insert("indentation");
  1090. valid_keys->insert("commentStyle");
  1091. valid_keys->insert("enableYAMLCompatibility");
  1092. valid_keys->insert("dropNullPlaceholders");
  1093. valid_keys->insert("useSpecialFloats");
  1094. valid_keys->insert("precision");
  1095. valid_keys->insert("precisionType");
  1096. }
  1097. bool StreamWriterBuilder::validate(Json::Value* invalid) const {
  1098. Json::Value my_invalid;
  1099. if (!invalid)
  1100. invalid = &my_invalid; // so we do not need to test for NULL
  1101. Json::Value& inv = *invalid;
  1102. std::set<String> valid_keys;
  1103. getValidWriterKeys(&valid_keys);
  1104. Value::Members keys = settings_.getMemberNames();
  1105. size_t n = keys.size();
  1106. for (size_t i = 0; i < n; ++i) {
  1107. String const& key = keys[i];
  1108. if (valid_keys.find(key) == valid_keys.end()) {
  1109. inv[key] = settings_[key];
  1110. }
  1111. }
  1112. return inv.empty();
  1113. }
  1114. Value& StreamWriterBuilder::operator[](const String& key) {
  1115. return settings_[key];
  1116. }
  1117. // static
  1118. void StreamWriterBuilder::setDefaults(Json::Value* settings) {
  1119. //! [StreamWriterBuilderDefaults]
  1120. (*settings)["commentStyle"] = "All";
  1121. (*settings)["indentation"] = "\t";
  1122. (*settings)["enableYAMLCompatibility"] = false;
  1123. (*settings)["dropNullPlaceholders"] = false;
  1124. (*settings)["useSpecialFloats"] = false;
  1125. (*settings)["precision"] = 17;
  1126. (*settings)["precisionType"] = "significant";
  1127. //! [StreamWriterBuilderDefaults]
  1128. }
  1129. String writeString(StreamWriter::Factory const& factory, Value const& root) {
  1130. OStringStream sout;
  1131. StreamWriterPtr const writer(factory.newStreamWriter());
  1132. writer->write(root, &sout);
  1133. return sout.str();
  1134. }
  1135. OStream& operator<<(OStream& sout, Value const& root) {
  1136. StreamWriterBuilder builder;
  1137. StreamWriterPtr const writer(builder.newStreamWriter());
  1138. writer->write(root, &sout);
  1139. return sout;
  1140. }
  1141. } // namespace Json