诸暨麻将添加redis
選択できるのは25トピックまでです。 トピックは、先頭が英数字で、英数字とダッシュ('-')を使用した35文字以内のものにしてください。
 
 
 
 
 
 

198 行
6.4 KiB

  1. #ifndef CPPTL_JSON_READER_H_INCLUDED
  2. # define CPPTL_JSON_READER_H_INCLUDED
  3. # include "stdafx.h"
  4. # include "features.h"
  5. # include "value.h"
  6. # include <deque>
  7. # include <stack>
  8. # include <string>
  9. # include <iostream>
  10. namespace Json {
  11. /** \brief Unserialize a <a HREF="http://www.json.org">JSON</a> document into a Value.
  12. *
  13. */
  14. class JSON_API Reader
  15. {
  16. public:
  17. typedef char Char;
  18. typedef const Char *Location;
  19. /** \brief Constructs a Reader allowing all features
  20. * for parsing.
  21. */
  22. Reader();
  23. /** \brief Constructs a Reader allowing the specified feature set
  24. * for parsing.
  25. */
  26. Reader( const Features &features );
  27. /** \brief Read a Value from a <a HREF="http://www.json.org">JSON</a> document.
  28. * \param document UTF-8 encoded string containing the document to read.
  29. * \param root [out] Contains the root value of the document if it was
  30. * successfully parsed.
  31. * \param collectComments \c true to collect comment and allow writing them back during
  32. * serialization, \c false to discard comments.
  33. * This parameter is ignored if Features::allowComments_
  34. * is \c false.
  35. * \return \c true if the document was successfully parsed, \c false if an error occurred.
  36. */
  37. bool parse( const std::string &document,
  38. Value &root,
  39. bool collectComments = true );
  40. /** \brief Read a Value from a <a HREF="http://www.json.org">JSON</a> document.
  41. * \param document UTF-8 encoded string containing the document to read.
  42. * \param root [out] Contains the root value of the document if it was
  43. * successfully parsed.
  44. * \param collectComments \c true to collect comment and allow writing them back during
  45. * serialization, \c false to discard comments.
  46. * This parameter is ignored if Features::allowComments_
  47. * is \c false.
  48. * \return \c true if the document was successfully parsed, \c false if an error occurred.
  49. */
  50. bool parse( const char *beginDoc, const char *endDoc,
  51. Value &root,
  52. bool collectComments = true );
  53. /// \brief Parse from input stream.
  54. /// \see Json::operator>>(std::istream&, Json::Value&).
  55. bool parse( std::istream &is,
  56. Value &root,
  57. bool collectComments = true );
  58. /** \brief Returns a user friendly string that list errors in the parsed document.
  59. * \return Formatted error message with the list of errors with their location in
  60. * the parsed document. An empty string is returned if no error occurred
  61. * during parsing.
  62. */
  63. std::string getFormatedErrorMessages() const;
  64. private:
  65. enum TokenType
  66. {
  67. tokenEndOfStream = 0,
  68. tokenObjectBegin,
  69. tokenObjectEnd,
  70. tokenArrayBegin,
  71. tokenArrayEnd,
  72. tokenString,
  73. tokenNumber,
  74. tokenTrue,
  75. tokenFalse,
  76. tokenNull,
  77. tokenArraySeparator,
  78. tokenMemberSeparator,
  79. tokenComment,
  80. tokenError
  81. };
  82. class Token
  83. {
  84. public:
  85. TokenType type_;
  86. Location start_;
  87. Location end_;
  88. };
  89. class ErrorInfo
  90. {
  91. public:
  92. Token token_;
  93. std::string message_;
  94. Location extra_;
  95. };
  96. typedef std::deque<ErrorInfo> Errors;
  97. bool expectToken( TokenType type, Token &token, const char *message );
  98. bool readToken( Token &token );
  99. void skipSpaces();
  100. bool match( Location pattern,
  101. int patternLength );
  102. bool readComment();
  103. bool readCStyleComment();
  104. bool readCppStyleComment();
  105. bool readString();
  106. void readNumber();
  107. bool readValue();
  108. bool readObject( Token &token );
  109. bool readArray( Token &token );
  110. bool decodeNumber( Token &token );
  111. bool decodeString( Token &token );
  112. bool decodeString( Token &token, std::string &decoded );
  113. bool decodeDouble( Token &token );
  114. bool decodeUnicodeCodePoint( Token &token,
  115. Location &current,
  116. Location end,
  117. unsigned int &unicode );
  118. bool decodeUnicodeEscapeSequence( Token &token,
  119. Location &current,
  120. Location end,
  121. unsigned int &unicode );
  122. bool addError( const std::string &message,
  123. Token &token,
  124. Location extra = 0 );
  125. bool recoverFromError( TokenType skipUntilToken );
  126. bool addErrorAndRecover( const std::string &message,
  127. Token &token,
  128. TokenType skipUntilToken );
  129. void skipUntilSpace();
  130. Value &currentValue();
  131. Char getNextChar();
  132. void getLocationLineAndColumn( Location location,
  133. int &line,
  134. int &column ) const;
  135. std::string getLocationLineAndColumn( Location location ) const;
  136. void addComment( Location begin,
  137. Location end,
  138. CommentPlacement placement );
  139. void skipCommentTokens( Token &token );
  140. typedef std::stack<Value *> Nodes;
  141. Nodes nodes_;
  142. Errors errors_;
  143. std::string document_;
  144. Location begin_;
  145. Location end_;
  146. Location current_;
  147. Location lastValueEnd_;
  148. Value *lastValue_;
  149. std::string commentsBefore_;
  150. Features features_;
  151. bool collectComments_;
  152. };
  153. /** \brief Read from 'sin' into 'root'.
  154. Always keep comments from the input JSON.
  155. This can be used to read a file into a particular sub-object.
  156. For example:
  157. \code
  158. Json::Value root;
  159. cin >> root["dir"]["file"];
  160. cout << root;
  161. \endcode
  162. Result:
  163. \verbatim
  164. {
  165. "dir": {
  166. "file": {
  167. // The input stream JSON would be nested here.
  168. }
  169. }
  170. }
  171. \endverbatim
  172. \throw std::exception on parse error.
  173. \see Json::operator<<()
  174. */
  175. std::istream& operator>>( std::istream&, Value& );
  176. } // namespace Json
  177. #endif // CPPTL_JSON_READER_H_INCLUDED