writer.h 6.0 KB

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