writer.h 6.5 KB

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