annotate json/reader.h @ 52:89944ab3e129 tip

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