诸暨麻将添加redis
Du kan inte välja fler än 25 ämnen Ämnen måste starta med en bokstav eller siffra, kan innehålla bindestreck ('-') och vara max 35 tecken långa.
 
 
 
 
 
 

176 rader
6.1 KiB

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