annotate json/reader.h @ 49:178642d134a7 tip

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