Baptiste Lepilleur | 4cd8bae | 2007-03-15 22:11:38 +0000 | [diff] [blame^] | 1 | #ifndef CPPTL_JSON_READER_H_INCLUDED
|
| 2 | # define CPPTL_JSON_READER_H_INCLUDED
|
| 3 |
|
| 4 | # include "forwards.h"
|
| 5 | # include "value.h"
|
| 6 | # include <deque>
|
| 7 | # include <stack>
|
| 8 | # include <string>
|
| 9 |
|
| 10 | namespace Json {
|
| 11 |
|
| 12 | class Value;
|
| 13 |
|
| 14 | /** \brief Unserialize a <a HREF="http://www.json.org">JSON</a> document into a Value.
|
| 15 | *
|
| 16 | *
|
| 17 | */
|
| 18 | class JSON_API Reader
|
| 19 | {
|
| 20 | public:
|
| 21 | typedef char Char;
|
| 22 | typedef const Char *Location;
|
| 23 |
|
| 24 | Reader();
|
| 25 |
|
| 26 | /** \brief Read a Value from a <a HREF="http://www.json.org">JSON</a> document.
|
| 27 | * \param document UTF-8 encoded string containing the document to read.
|
| 28 | * \param root [out] Contains the root value of the document if it was
|
| 29 | * successfully parsed.
|
| 30 | * \param collectComments \c true to collect comment and allow writing them back during
|
| 31 | * serialization, \c false to discard comments.
|
| 32 | * \return \c true if the document was successfully parsed, \c false if an error occurred.
|
| 33 | */
|
| 34 | bool parse( const std::string &document,
|
| 35 | Value &root,
|
| 36 | bool collectComments = true );
|
| 37 |
|
| 38 | /** \brief Read a Value from a <a HREF="http://www.json.org">JSON</a> document.
|
| 39 | * \param document UTF-8 encoded string containing the document to read.
|
| 40 | * \param root [out] Contains the root value of the document if it was
|
| 41 | * successfully parsed.
|
| 42 | * \param collectComments \c true to collect comment and allow writing them back during
|
| 43 | * serialization, \c false to discard comments.
|
| 44 | * \return \c true if the document was successfully parsed, \c false if an error occurred.
|
| 45 | */
|
| 46 | bool parse( const char *beginDoc, const char *endDoc,
|
| 47 | Value &root,
|
| 48 | bool collectComments = true );
|
| 49 |
|
| 50 | /** \brief Returns a user friendly string that list errors in the parsed document.
|
| 51 | * \return Formatted error message with the list of errors with their location in
|
| 52 | * the parsed document. An empty string is returned if no error occurred
|
| 53 | * during parsing.
|
| 54 | */
|
| 55 | std::string getFormatedErrorMessages() const;
|
| 56 |
|
| 57 | private:
|
| 58 | enum TokenType
|
| 59 | {
|
| 60 | tokenEndOfStream = 0,
|
| 61 | tokenObjectBegin,
|
| 62 | tokenObjectEnd,
|
| 63 | tokenArrayBegin,
|
| 64 | tokenArrayEnd,
|
| 65 | tokenString,
|
| 66 | tokenNumber,
|
| 67 | tokenTrue,
|
| 68 | tokenFalse,
|
| 69 | tokenNull,
|
| 70 | tokenArraySeparator,
|
| 71 | tokenMemberSeparator,
|
| 72 | tokenComment,
|
| 73 | tokenError
|
| 74 | };
|
| 75 |
|
| 76 | class Token
|
| 77 | {
|
| 78 | public:
|
| 79 | TokenType type_;
|
| 80 | Location start_;
|
| 81 | Location end_;
|
| 82 | };
|
| 83 |
|
| 84 | class ErrorInfo
|
| 85 | {
|
| 86 | public:
|
| 87 | Token token_;
|
| 88 | std::string message_;
|
| 89 | Location extra_;
|
| 90 | };
|
| 91 |
|
| 92 | typedef std::deque<ErrorInfo> Errors;
|
| 93 |
|
| 94 | bool expectToken( TokenType type, Token &token, const char *message );
|
| 95 | bool readToken( Token &token );
|
| 96 | void skipSpaces();
|
| 97 | bool match( Location pattern,
|
| 98 | int patternLength );
|
| 99 | bool readComment();
|
| 100 | bool readCStyleComment();
|
| 101 | bool readCppStyleComment();
|
| 102 | bool readString();
|
| 103 | void readNumber();
|
| 104 | bool readValue();
|
| 105 | bool readObject( Token &token );
|
| 106 | bool readArray( Token &token );
|
| 107 | bool decodeNumber( Token &token );
|
| 108 | bool decodeString( Token &token );
|
| 109 | bool decodeString( Token &token, std::string &decoded );
|
| 110 | bool decodeDouble( Token &token );
|
| 111 | bool decodeUnicodeEscapeSequence( Token &token,
|
| 112 | Location ¤t,
|
| 113 | Location end,
|
| 114 | unsigned int &unicode );
|
| 115 | bool addError( const std::string &message,
|
| 116 | Token &token,
|
| 117 | Location extra = 0 );
|
| 118 | bool recoverFromError( TokenType skipUntilToken );
|
| 119 | bool addErrorAndRecover( const std::string &message,
|
| 120 | Token &token,
|
| 121 | TokenType skipUntilToken );
|
| 122 | void skipUntilSpace();
|
| 123 | Value ¤tValue();
|
| 124 | Char getNextChar();
|
| 125 | void getLocationLineAndColumn( Location location,
|
| 126 | int &line,
|
| 127 | int &column ) const;
|
| 128 | std::string getLocationLineAndColumn( Location location ) const;
|
| 129 | void addComment( Location begin,
|
| 130 | Location end,
|
| 131 | CommentPlacement placement );
|
| 132 | void skipCommentTokens( Token &token );
|
| 133 |
|
| 134 | typedef std::stack<Value *> Nodes;
|
| 135 | Nodes nodes_;
|
| 136 | Errors errors_;
|
| 137 | std::string document_;
|
| 138 | Location begin_;
|
| 139 | Location end_;
|
| 140 | Location current_;
|
| 141 | Location lastValueEnd_;
|
| 142 | Value *lastValue_;
|
| 143 | std::string commentsBefore_;
|
| 144 | bool collectComments_;
|
| 145 | };
|
| 146 |
|
| 147 |
|
| 148 | } // namespace Json
|
| 149 |
|
| 150 | #endif // CPPTL_JSON_READER_H_INCLUDED
|