reader.h 7.3 KB

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