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.

383 lines
12 KiB

7 years ago
​
​
​
​
​
​
​
​
7 years ago
​
​
​
​
​
​
​
​
7 years ago
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
7 years ago
​
​
​
​
​
​
​
​
​
​
​
7 years ago
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
7 years ago
7 years ago
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
7 years ago
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
7 years ago
7 years ago
​
​
​
7 years ago
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
7 years ago
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
7 years ago
7 years ago
​
​
​
7 years ago
​
​
​
​
​
​
​
​
7 years ago
​
​
​
​
​
7 years ago
​
​
  1. // Copyright 2007-2010 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. #ifndef JSON_WRITER_H_INCLUDED
  6. #define JSON_WRITER_H_INCLUDED
  7. #if ! defined(JSON_IS_AMALGAMATION)
  8. #include "value.h"
  9. #endif // if !defined(JSON_IS_AMALGAMATION)
  10. #include <ostream>
  11. #include <string>
  12. #include <vector>
  13. // Disable warning C4251: <data member>: <type> needs to have dll-interface to
  14. // be used by...
  15. #if defined(JSONCPP_DISABLE_DLL_INTERFACE_WARNING) && defined(_MSC_VER)
  16. #pragma warning(push)
  17. #pragma warning(disable : 4251)
  18. #endif // if defined(JSONCPP_DISABLE_DLL_INTERFACE_WARNING)
  19. #pragma pack(push, 8)
  20. namespace Json
  21. {
  22. class Value;
  23. /**
  24. Usage:
  25. \code
  26. using namespace Json;
  27. void writeToStdout(StreamWriter::Factory const& factory, Value const& value) {
  28. std::unique_ptr<StreamWriter> const writer(
  29. factory.newStreamWriter());
  30. writer->write(value, &std::cout);
  31. std::cout << std::endl; // add lf and flush
  32. }
  33. \endcode
  34. */
  35. class JSON_API StreamWriter
  36. {
  37. protected:
  38. OStream * sout_; // not owned; will not delete
  39. public:
  40. StreamWriter();
  41. virtual ~StreamWriter();
  42. /** Write Value into document as configured in sub-class.
  43. Do not take ownership of sout, but maintain a reference during function.
  44. \pre sout != NULL
  45. \return zero on success (For now, we always return zero, so check the
  46. stream instead.) \throw std::exception possibly, depending on configuration
  47. */
  48. virtual int write(const Value & root, OStream * sout) = 0;
  49. /** \brief A simple abstract factory.
  50. */
  51. class JSON_API Factory
  52. {
  53. public:
  54. virtual ~Factory();
  55. /** \brief Allocate a CharReader via operator new().
  56. * \throw std::exception if something goes wrong (e.g. invalid settings)
  57. */
  58. virtual StreamWriter * newStreamWriter() const = 0;
  59. }; // Factory
  60. }; // StreamWriter
  61. /** \brief Write into stringstream, then return string, for convenience.
  62. * A StreamWriter will be created from the factory, used, and then deleted.
  63. */
  64. String JSON_API writeString(const StreamWriter::Factory & factory, const Value & root);
  65. /** \brief Build a StreamWriter implementation.
  66. Usage:
  67. \code
  68. using namespace Json;
  69. Value value = ...;
  70. StreamWriterBuilder builder;
  71. builder["commentStyle"] = "None";
  72. builder["indentation"] = " "; // or whatever you like
  73. std::unique_ptr<Json::StreamWriter> writer(
  74. builder.newStreamWriter());
  75. writer->write(value, &std::cout);
  76. std::cout << std::endl; // add lf and flush
  77. \endcode
  78. */
  79. class JSON_API StreamWriterBuilder : public StreamWriter::Factory
  80. {
  81. public:
  82. // Note: We use a Json::Value so that we can add data-members to this class
  83. // without a major version bump.
  84. /** Configuration of this builder.
  85. Available settings (case-sensitive):
  86. - "commentStyle": "None" or "All"
  87. - "indentation": "<anything>".
  88. - Setting this to an empty string also omits newline characters.
  89. - "enableYAMLCompatibility": false or true
  90. - slightly change the whitespace around colons
  91. - "dropNullPlaceholders": false or true
  92. - Drop the "null" string from the writer's output for nullValues.
  93. Strictly speaking, this is not valid JSON. But when the output is being
  94. fed to a browser's JavaScript, it makes for smaller output and the
  95. browser can handle the output just fine.
  96. - "useSpecialFloats": false or true
  97. - If true, outputs non-finite floating point values in the following way:
  98. NaN values as "NaN", positive infinity as "Infinity", and negative
  99. infinity as "-Infinity".
  100. - "precision": int
  101. - Number of precision digits for formatting of real values.
  102. - "precisionType": "significant"(default) or "decimal"
  103. - Type of precision for formatting of real values.
  104. You can examine 'settings_` yourself
  105. to see the defaults. You can also write and read them just like any
  106. JSON Value.
  107. \sa setDefaults()
  108. */
  109. Json::Value settings_;
  110. StreamWriterBuilder();
  111. ~StreamWriterBuilder() override;
  112. /**
  113. * \throw std::exception if something goes wrong (e.g. invalid settings)
  114. */
  115. StreamWriter * newStreamWriter() const override;
  116. /** \return true if 'settings' are legal and consistent;
  117. * otherwise, indicate bad settings via 'invalid'.
  118. */
  119. bool validate(Json::Value * invalid) const;
  120. /** A simple way to update a specific setting.
  121. */
  122. Value & operator[](const String & key);
  123. /** Called by ctor, but you can use this to reset settings_.
  124. * \pre 'settings' != NULL (but Json::null is fine)
  125. * \remark Defaults:
  126. * \snippet src/lib_json/json_writer.cpp StreamWriterBuilderDefaults
  127. */
  128. static void setDefaults(Json::Value * settings);
  129. };
  130. /** \brief Abstract class for writers.
  131. * \deprecated Use StreamWriter. (And really, this is an implementation detail.)
  132. */
  133. class JSONCPP_DEPRECATED("Use StreamWriter instead") JSON_API Writer
  134. {
  135. public:
  136. virtual ~Writer();
  137. virtual String write(const Value & root) = 0;
  138. };
  139. /** \brief Outputs a Value in <a HREF="http://www.json.org">JSON</a> format
  140. *without formatting (not human friendly).
  141. *
  142. * The JSON document is written in a single line. It is not intended for 'human'
  143. *consumption,
  144. * but may be useful to support feature such as RPC where bandwidth is limited.
  145. * \sa Reader, Value
  146. * \deprecated Use StreamWriterBuilder.
  147. */
  148. #if defined(_MSC_VER)
  149. #pragma warning(push)
  150. #pragma warning(disable : 4996) // Deriving from deprecated class
  151. #endif
  152. class JSONCPP_DEPRECATED("Use StreamWriterBuilder instead") JSON_API FastWriter : public Writer
  153. {
  154. public:
  155. FastWriter();
  156. ~FastWriter() override = default;
  157. void enableYAMLCompatibility();
  158. /** \brief Drop the "null" string from the writer's output for nullValues.
  159. * Strictly speaking, this is not valid JSON. But when the output is being
  160. * fed to a browser's JavaScript, it makes for smaller output and the
  161. * browser can handle the output just fine.
  162. */
  163. void dropNullPlaceholders();
  164. void omitEndingLineFeed();
  165. public: // overridden from Writer
  166. String write(const Value & root) override;
  167. private:
  168. void writeValue(const Value & value);
  169. String document_;
  170. bool yamlCompatibilityEnabled_{false};
  171. bool dropNullPlaceholders_{false};
  172. bool omitEndingLineFeed_{false};
  173. };
  174. #if defined(_MSC_VER)
  175. #pragma warning(pop)
  176. #endif
  177. /** \brief Writes a Value in <a HREF="http://www.json.org">JSON</a> format in a
  178. *human friendly way.
  179. *
  180. * The rules for line break and indent are as follow:
  181. * - Object value:
  182. * - if empty then print {} without indent and line break
  183. * - if not empty the print '{', line break & indent, print one value per
  184. *line
  185. * and then unindent and line break and print '}'.
  186. * - Array value:
  187. * - if empty then print [] without indent and line break
  188. * - if the array contains no object value, empty array or some other value
  189. *types,
  190. * and all the values fit on one lines, then print the array on a single
  191. *line.
  192. * - otherwise, it the values do not fit on one line, or the array contains
  193. * object or non empty array, then print one value per line.
  194. *
  195. * If the Value have comments then they are outputed according to their
  196. *#CommentPlacement.
  197. *
  198. * \sa Reader, Value, Value::setComment()
  199. * \deprecated Use StreamWriterBuilder.
  200. */
  201. #if defined(_MSC_VER)
  202. #pragma warning(push)
  203. #pragma warning(disable : 4996) // Deriving from deprecated class
  204. #endif
  205. class JSONCPP_DEPRECATED("Use StreamWriterBuilder instead") JSON_API StyledWriter : public Writer
  206. {
  207. public:
  208. StyledWriter();
  209. ~StyledWriter() override = default;
  210. public: // overridden from Writer
  211. /** \brief Serialize a Value in <a HREF="http://www.json.org">JSON</a> format.
  212. * \param root Value to serialize.
  213. * \return String containing the JSON document that represents the root value.
  214. */
  215. String write(const Value & root) override;
  216. private:
  217. void writeValue(const Value & value);
  218. void writeArrayValue(const Value & value);
  219. bool isMultilineArray(const Value & value);
  220. void pushValue(const String & value);
  221. void writeIndent();
  222. void writeWithIndent(const String & value);
  223. void indent();
  224. void unindent();
  225. void writeCommentBeforeValue(const Value & root);
  226. void writeCommentAfterValueOnSameLine(const Value & root);
  227. static bool hasCommentForValue(const Value & value);
  228. static String normalizeEOL(const String & text);
  229. typedef std::vector<String> ChildValues;
  230. ChildValues childValues_;
  231. String document_;
  232. String indentString_;
  233. unsigned int rightMargin_{74};
  234. unsigned int indentSize_{3};
  235. bool addChildValues_{false};
  236. };
  237. #if defined(_MSC_VER)
  238. #pragma warning(pop)
  239. #endif
  240. /** \brief Writes a Value in <a HREF="http://www.json.org">JSON</a> format in a
  241. human friendly way,
  242. to a stream rather than to a string.
  243. *
  244. * The rules for line break and indent are as follow:
  245. * - Object value:
  246. * - if empty then print {} without indent and line break
  247. * - if not empty the print '{', line break & indent, print one value per
  248. line
  249. * and then unindent and line break and print '}'.
  250. * - Array value:
  251. * - if empty then print [] without indent and line break
  252. * - if the array contains no object value, empty array or some other value
  253. types,
  254. * and all the values fit on one lines, then print the array on a single
  255. line.
  256. * - otherwise, it the values do not fit on one line, or the array contains
  257. * object or non empty array, then print one value per line.
  258. *
  259. * If the Value have comments then they are outputed according to their
  260. #CommentPlacement.
  261. *
  262. * \sa Reader, Value, Value::setComment()
  263. * \deprecated Use StreamWriterBuilder.
  264. */
  265. #if defined(_MSC_VER)
  266. #pragma warning(push)
  267. #pragma warning(disable : 4996) // Deriving from deprecated class
  268. #endif
  269. class JSONCPP_DEPRECATED("Use StreamWriterBuilder instead") JSON_API StyledStreamWriter
  270. {
  271. public:
  272. /**
  273. * \param indentation Each level will be indented by this amount extra.
  274. */
  275. StyledStreamWriter(String indentation = "\t");
  276. ~StyledStreamWriter() = default;
  277. public:
  278. /** \brief Serialize a Value in <a HREF="http://www.json.org">JSON</a> format.
  279. * \param out Stream to write to. (Can be ostringstream, e.g.)
  280. * \param root Value to serialize.
  281. * \note There is no point in deriving from Writer, since write() should not
  282. * return a value.
  283. */
  284. void write(OStream & out, const Value & root);
  285. private:
  286. void writeValue(const Value & value);
  287. void writeArrayValue(const Value & value);
  288. bool isMultilineArray(const Value & value);
  289. void pushValue(const String & value);
  290. void writeIndent();
  291. void writeWithIndent(const String & value);
  292. void indent();
  293. void unindent();
  294. void writeCommentBeforeValue(const Value & root);
  295. void writeCommentAfterValueOnSameLine(const Value & root);
  296. static bool hasCommentForValue(const Value & value);
  297. static String normalizeEOL(const String & text);
  298. typedef std::vector<String> ChildValues;
  299. ChildValues childValues_;
  300. OStream * document_;
  301. String indentString_;
  302. unsigned int rightMargin_{74};
  303. String indentation_;
  304. bool addChildValues_ : 1;
  305. bool indented_ : 1;
  306. };
  307. #if defined(_MSC_VER)
  308. #pragma warning(pop)
  309. #endif
  310. #if defined(JSON_HAS_INT64)
  311. String JSON_API valueToString(Int value);
  312. String JSON_API valueToString(UInt value);
  313. #endif // if defined(JSON_HAS_INT64)
  314. String JSON_API valueToString(LargestInt value);
  315. String JSON_API valueToString(LargestUInt value);
  316. String JSON_API valueToString(double value, unsigned int precision = Value::defaultRealPrecision, PrecisionType precisionType = PrecisionType::significantDigits);
  317. String JSON_API valueToString(bool value);
  318. String JSON_API valueToQuotedString(const char * value);
  319. /// \brief Output using the StyledStreamWriter.
  320. /// \see Json::operator>>()
  321. JSON_API OStream & operator<<(OStream &, const Value & root);
  322. } // namespace Json
  323. #pragma pack(pop)
  324. #if defined(JSONCPP_DISABLE_DLL_INTERFACE_WARNING)
  325. #pragma warning(pop)
  326. #endif // if defined(JSONCPP_DISABLE_DLL_INTERFACE_WARNING)
  327. #endif // JSON_WRITER_H_INCLUDED