writer.h 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213
  1. // Copyright 2007-2010 Baptiste Lepilleur
  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 <iosfwd>
  11. #include <vector>
  12. #include <string>
  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)
  16. #pragma warning(push)
  17. #pragma warning(disable : 4251)
  18. #endif // if defined(JSONCPP_DISABLE_DLL_INTERFACE_WARNING)
  19. namespace Json {
  20. class Value;
  21. /** \brief Abstract class for writers.
  22. */
  23. class JSON_API Writer {
  24. public:
  25. virtual ~Writer();
  26. virtual std::string write(const Value& root) = 0;
  27. };
  28. /** \brief Outputs a Value in <a HREF="http://www.json.org">JSON</a> format
  29. *without formatting (not human friendly).
  30. *
  31. * The JSON document is written in a single line. It is not intended for 'human'
  32. *consumption,
  33. * but may be usefull to support feature such as RPC where bandwith is limited.
  34. * \sa Reader, Value
  35. */
  36. class JSON_API FastWriter : public Writer {
  37. public:
  38. FastWriter();
  39. virtual ~FastWriter() {}
  40. void enableYAMLCompatibility();
  41. /** \brief Drop the "null" string from the writer's output for nullValues.
  42. * Strictly speaking, this is not valid JSON. But when the output is being
  43. * fed to a browser's Javascript, it makes for smaller output and the
  44. * browser can handle the output just fine.
  45. */
  46. void dropNullPlaceholders();
  47. void omitEndingLineFeed();
  48. public: // overridden from Writer
  49. virtual std::string write(const Value& root);
  50. private:
  51. void writeValue(const Value& value);
  52. std::string document_;
  53. bool yamlCompatiblityEnabled_;
  54. bool dropNullPlaceholders_;
  55. bool omitEndingLineFeed_;
  56. };
  57. /** \brief Writes a Value in <a HREF="http://www.json.org">JSON</a> format in a
  58. *human friendly way.
  59. *
  60. * The rules for line break and indent are as follow:
  61. * - Object value:
  62. * - if empty then print {} without indent and line break
  63. * - if not empty the print '{', line break & indent, print one value per
  64. *line
  65. * and then unindent and line break and print '}'.
  66. * - Array value:
  67. * - if empty then print [] without indent and line break
  68. * - if the array contains no object value, empty array or some other value
  69. *types,
  70. * and all the values fit on one lines, then print the array on a single
  71. *line.
  72. * - otherwise, it the values do not fit on one line, or the array contains
  73. * object or non empty array, then print one value per line.
  74. *
  75. * If the Value have comments then they are outputed according to their
  76. *#CommentPlacement.
  77. *
  78. * \sa Reader, Value, Value::setComment()
  79. */
  80. class JSON_API StyledWriter : public Writer {
  81. public:
  82. StyledWriter();
  83. virtual ~StyledWriter() {}
  84. public: // overridden from Writer
  85. /** \brief Serialize a Value in <a HREF="http://www.json.org">JSON</a> format.
  86. * \param root Value to serialize.
  87. * \return String containing the JSON document that represents the root value.
  88. */
  89. virtual std::string write(const Value& root);
  90. private:
  91. void writeValue(const Value& value);
  92. void writeArrayValue(const Value& value);
  93. bool isMultineArray(const Value& value);
  94. void pushValue(const std::string& value);
  95. void writeIndent();
  96. void writeWithIndent(const std::string& value);
  97. void indent();
  98. void unindent();
  99. void writeCommentBeforeValue(const Value& root);
  100. void writeCommentAfterValueOnSameLine(const Value& root);
  101. bool hasCommentForValue(const Value& value);
  102. static std::string normalizeEOL(const std::string& text);
  103. typedef std::vector<std::string> ChildValues;
  104. ChildValues childValues_;
  105. std::string document_;
  106. std::string indentString_;
  107. int rightMargin_;
  108. int indentSize_;
  109. bool addChildValues_;
  110. };
  111. /** \brief Writes a Value in <a HREF="http://www.json.org">JSON</a> format in a
  112. human friendly way,
  113. to a stream rather than to a string.
  114. *
  115. * The rules for line break and indent are as follow:
  116. * - Object value:
  117. * - if empty then print {} without indent and line break
  118. * - if not empty the print '{', line break & indent, print one value per
  119. line
  120. * and then unindent and line break and print '}'.
  121. * - Array value:
  122. * - if empty then print [] without indent and line break
  123. * - if the array contains no object value, empty array or some other value
  124. types,
  125. * and all the values fit on one lines, then print the array on a single
  126. line.
  127. * - otherwise, it the values do not fit on one line, or the array contains
  128. * object or non empty array, then print one value per line.
  129. *
  130. * If the Value have comments then they are outputed according to their
  131. #CommentPlacement.
  132. *
  133. * \sa Reader, Value, Value::setComment()
  134. */
  135. class JSON_API StyledStreamWriter {
  136. public:
  137. StyledStreamWriter(std::string indentation = "\t");
  138. ~StyledStreamWriter() {}
  139. public:
  140. /** \brief Serialize a Value in <a HREF="http://www.json.org">JSON</a> format.
  141. * \param out Stream to write to. (Can be ostringstream, e.g.)
  142. * \param root Value to serialize.
  143. * \note There is no point in deriving from Writer, since write() should not
  144. * return a value.
  145. */
  146. void write(std::ostream& out, const Value& root);
  147. private:
  148. void writeValue(const Value& value);
  149. void writeArrayValue(const Value& value);
  150. bool isMultineArray(const Value& value);
  151. void pushValue(const std::string& value);
  152. void writeIndent();
  153. void writeWithIndent(const std::string& value);
  154. void indent();
  155. void unindent();
  156. void writeCommentBeforeValue(const Value& root);
  157. void writeCommentAfterValueOnSameLine(const Value& root);
  158. bool hasCommentForValue(const Value& value);
  159. static std::string normalizeEOL(const std::string& text);
  160. typedef std::vector<std::string> ChildValues;
  161. ChildValues childValues_;
  162. std::ostream* document_;
  163. std::string indentString_;
  164. int rightMargin_;
  165. std::string indentation_;
  166. bool addChildValues_;
  167. };
  168. #if defined(JSON_HAS_INT64)
  169. std::string JSON_API valueToString(Int value);
  170. std::string JSON_API valueToString(UInt value);
  171. #endif // if defined(JSON_HAS_INT64)
  172. std::string JSON_API valueToString(LargestInt value);
  173. std::string JSON_API valueToString(LargestUInt value);
  174. std::string JSON_API valueToString(double value);
  175. std::string JSON_API valueToString(bool value);
  176. std::string JSON_API valueToQuotedString(const char* value);
  177. /// \brief Output using the StyledStreamWriter.
  178. /// \see Json::operator>>()
  179. JSON_API std::ostream& operator<<(std::ostream&, const Value& root);
  180. } // namespace Json
  181. #if defined(JSONCPP_DISABLE_DLL_INTERFACE_WARNING)
  182. #pragma warning(pop)
  183. #endif // if defined(JSONCPP_DISABLE_DLL_INTERFACE_WARNING)
  184. #endif // JSON_WRITER_H_INCLUDED