writer.h 6.8 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 <vector>
  11. #include <string>
  12. // Disable warning C4251: <data member>: <type> needs to have dll-interface to
  13. // be used by...
  14. #if defined(JSONCPP_DISABLE_DLL_INTERFACE_WARNING)
  15. #pragma warning(push)
  16. #pragma warning(disable : 4251)
  17. #endif // if defined(JSONCPP_DISABLE_DLL_INTERFACE_WARNING)
  18. namespace Json {
  19. class Value;
  20. /** \brief Abstract class for writers.
  21. */
  22. class JSON_API Writer {
  23. public:
  24. virtual ~Writer();
  25. virtual std::string write(const Value& root) = 0;
  26. };
  27. /** \brief Outputs a Value in <a HREF="http://www.json.org">JSON</a> format
  28. *without formatting (not human friendly).
  29. *
  30. * The JSON document is written in a single line. It is not intended for 'human'
  31. *consumption,
  32. * but may be usefull to support feature such as RPC where bandwith is limited.
  33. * \sa Reader, Value
  34. */
  35. class JSON_API FastWriter : public Writer {
  36. public:
  37. FastWriter();
  38. virtual ~FastWriter() {}
  39. void enableYAMLCompatibility();
  40. /** \brief Drop the "null" string from the writer's output for nullValues.
  41. * Strictly speaking, this is not valid JSON. But when the output is being
  42. * fed to a browser's Javascript, it makes for smaller output and the
  43. * browser can handle the output just fine.
  44. */
  45. void dropNullPlaceholders();
  46. void omitEndingLineFeed();
  47. public: // overridden from Writer
  48. virtual std::string write(const Value& root);
  49. private:
  50. void writeValue(const Value& value);
  51. std::string document_;
  52. bool yamlCompatiblityEnabled_;
  53. bool dropNullPlaceholders_;
  54. bool omitEndingLineFeed_;
  55. };
  56. /** \brief Writes a Value in <a HREF="http://www.json.org">JSON</a> format in a
  57. *human friendly way.
  58. *
  59. * The rules for line break and indent are as follow:
  60. * - Object value:
  61. * - if empty then print {} without indent and line break
  62. * - if not empty the print '{', line break & indent, print one value per
  63. *line
  64. * and then unindent and line break and print '}'.
  65. * - Array value:
  66. * - if empty then print [] without indent and line break
  67. * - if the array contains no object value, empty array or some other value
  68. *types,
  69. * and all the values fit on one lines, then print the array on a single
  70. *line.
  71. * - otherwise, it the values do not fit on one line, or the array contains
  72. * object or non empty array, then print one value per line.
  73. *
  74. * If the Value have comments then they are outputed according to their
  75. *#CommentPlacement.
  76. *
  77. * \sa Reader, Value, Value::setComment()
  78. */
  79. class JSON_API StyledWriter : public Writer {
  80. public:
  81. StyledWriter();
  82. virtual ~StyledWriter() {}
  83. public: // overridden from Writer
  84. /** \brief Serialize a Value in <a HREF="http://www.json.org">JSON</a> format.
  85. * \param root Value to serialize.
  86. * \return String containing the JSON document that represents the root value.
  87. */
  88. virtual std::string write(const Value& root);
  89. private:
  90. void writeValue(const Value& value);
  91. void writeArrayValue(const Value& value);
  92. bool isMultineArray(const Value& value);
  93. void pushValue(const std::string& value);
  94. void writeIndent();
  95. void writeWithIndent(const std::string& value);
  96. void indent();
  97. void unindent();
  98. void writeCommentBeforeValue(const Value& root);
  99. void writeCommentAfterValueOnSameLine(const Value& root);
  100. bool hasCommentForValue(const Value& value);
  101. static std::string normalizeEOL(const std::string& text);
  102. typedef std::vector<std::string> ChildValues;
  103. ChildValues childValues_;
  104. std::string document_;
  105. std::string indentString_;
  106. int rightMargin_;
  107. int indentSize_;
  108. bool addChildValues_;
  109. };
  110. /** \brief Writes a Value in <a HREF="http://www.json.org">JSON</a> format in a
  111. human friendly way,
  112. to a stream rather than to a string.
  113. *
  114. * The rules for line break and indent are as follow:
  115. * - Object value:
  116. * - if empty then print {} without indent and line break
  117. * - if not empty the print '{', line break & indent, print one value per
  118. line
  119. * and then unindent and line break and print '}'.
  120. * - Array value:
  121. * - if empty then print [] without indent and line break
  122. * - if the array contains no object value, empty array or some other value
  123. types,
  124. * and all the values fit on one lines, then print the array on a single
  125. line.
  126. * - otherwise, it the values do not fit on one line, or the array contains
  127. * object or non empty array, then print one value per line.
  128. *
  129. * If the Value have comments then they are outputed according to their
  130. #CommentPlacement.
  131. *
  132. * \param indentation Each level will be indented by this amount extra.
  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