JsonCpp project page | JsonCpp home page

reader.h
Go to the documentation of this file.
1 // Copyright 2007-2010 Baptiste Lepilleur and The JsonCpp Authors
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
6 #ifndef CPPTL_JSON_READER_H_INCLUDED
7 #define CPPTL_JSON_READER_H_INCLUDED
8
9 #if !defined(JSON_IS_AMALGAMATION)
10 #include "features.h"
11 #include "value.h"
12 #endif // if !defined(JSON_IS_AMALGAMATION)
13 #include <deque>
14 #include <iosfwd>
15 #include <stack>
16 #include <string>
17 #include <istream>
18
19 // Disable warning C4251: <data member>: <type> needs to have dll-interface to
20 // be used by...
21 #if defined(JSONCPP_DISABLE_DLL_INTERFACE_WARNING)
22 #pragma warning(push)
23 #pragma warning(disable : 4251)
24 #endif // if defined(JSONCPP_DISABLE_DLL_INTERFACE_WARNING)
25
26 #pragma pack(push, 8)
27
28 namespace Json {
29
35 class JSONCPP_DEPRECATED("Use CharReader and CharReaderBuilder instead") JSON_API Reader {
36 public:
37 typedef char Char;
38 typedef const Char* Location;
39
46 struct StructuredError {
47 ptrdiff_t offset_start;
48 ptrdiff_t offset_limit;
49 JSONCPP_STRING message;
50 };
51
55 Reader();
56
60 Reader(const Features& features);
61
76 bool
77 parse(const std::string& document, Value& root, bool collectComments = true);
78
97 bool parse(const char* beginDoc,
98 const char* endDoc,
99 Value& root,
100 bool collectComments = true);
101
104 bool parse(JSONCPP_ISTREAM& is, Value& root, bool collectComments = true);
105
115 JSONCPP_DEPRECATED("Use getFormattedErrorMessages() instead.")
116 JSONCPP_STRING getFormatedErrorMessages() const;
117
126 JSONCPP_STRING getFormattedErrorMessages() const;
127
135 std::vector<StructuredError> getStructuredErrors() const;
136
143 bool pushError(const Value& value, const JSONCPP_STRING& message);
144
152 bool pushError(const Value& value, const JSONCPP_STRING& message, const Value& extra);
153
158 bool good() const;
159
160 private:
161 enum TokenType {
162 tokenEndOfStream = 0,
163 tokenObjectBegin,
164 tokenObjectEnd,
165 tokenArrayBegin,
166 tokenArrayEnd,
167 tokenString,
168 tokenNumber,
169 tokenTrue,
170 tokenFalse,
171 tokenNull,
172 tokenArraySeparator,
173 tokenMemberSeparator,
174 tokenComment,
175 tokenError
176 };
177
178 class Token {
179 public:
180 TokenType type_;
181 Location start_;
182 Location end_;
183 };
184
185 class ErrorInfo {
186 public:
187 Token token_;
188 JSONCPP_STRING message_;
189 Location extra_;
190 };
191
192 typedef std::deque<ErrorInfo> Errors;
193
194 bool readToken(Token& token);
195 void skipSpaces();
196 bool match(Location pattern, int patternLength);
197 bool readComment();
198 bool readCStyleComment();
199 bool readCppStyleComment();
200 bool readString();
201 void readNumber();
202 bool readValue();
203 bool readObject(Token& token);
204 bool readArray(Token& token);
205 bool decodeNumber(Token& token);
206 bool decodeNumber(Token& token, Value& decoded);
207 bool decodeString(Token& token);
208 bool decodeString(Token& token, JSONCPP_STRING& decoded);
209 bool decodeDouble(Token& token);
210 bool decodeDouble(Token& token, Value& decoded);
211 bool decodeUnicodeCodePoint(Token& token,
212 Location& current,
213 Location end,
214 unsigned int& unicode);
215 bool decodeUnicodeEscapeSequence(Token& token,
216 Location& current,
217 Location end,
218 unsigned int& unicode);
219 bool addError(const JSONCPP_STRING& message, Token& token, Location extra = 0);
220 bool recoverFromError(TokenType skipUntilToken);
221 bool addErrorAndRecover(const JSONCPP_STRING& message,
222 Token& token,
223 TokenType skipUntilToken);
224 void skipUntilSpace();
225 Value& currentValue();
226 Char getNextChar();
227 void
228 getLocationLineAndColumn(Location location, int& line, int& column) const;
229 JSONCPP_STRING getLocationLineAndColumn(Location location) const;
230 void addComment(Location begin, Location end, CommentPlacement placement);
231 void skipCommentTokens(Token& token);
232
233 static bool containsNewLine(Location begin, Location end);
234 static JSONCPP_STRING normalizeEOL(Location begin, Location end);
235
236 typedef std::stack<Value*> Nodes;
237 Nodes nodes_;
238 Errors errors_;
239 JSONCPP_STRING document_;
240 Location begin_;
241 Location end_;
242 Location current_;
243 Location lastValueEnd_;
244 Value* lastValue_;
245 JSONCPP_STRING commentsBefore_;
246 Features features_;
247 bool collectComments_;
248 }; // Reader
249
252 class JSON_API CharReader {
253 public:
254 virtual ~CharReader() {}
272 virtual bool parse(
273 char const* beginDoc, char const* endDoc,
274 Value* root, JSONCPP_STRING* errs) = 0;
275
276 class JSON_API Factory {
277 public:
278 virtual ~Factory() {}
282 virtual CharReader* newCharReader() const = 0;
283 }; // Factory
284 }; // CharReader
285
298 class JSON_API CharReaderBuilder : public CharReader::Factory {
299 public:
300 // Note: We use a Json::Value so that we can add data-members to this class
301 // without a major version bump.
338 Json::Value settings_;
339
340 CharReaderBuilder();
341 ~CharReaderBuilder() JSONCPP_OVERRIDE;
342
343 CharReader* newCharReader() const JSONCPP_OVERRIDE;
344
348 bool validate(Json::Value* invalid) const;
349
352 Value& operator[](JSONCPP_STRING key);
353
359 static void setDefaults(Json::Value* settings);
365 static void strictMode(Json::Value* settings);
366 };
367
372 bool JSON_API parseFromStream(
373 CharReader::Factory const&,
374 JSONCPP_ISTREAM&,
375 Value* root, std::string* errs);
376
401 JSON_API JSONCPP_ISTREAM& operator>>(JSONCPP_ISTREAM&, Value&);
402
403 } // namespace Json
404
405 #pragma pack(pop)
406
407 #if defined(JSONCPP_DISABLE_DLL_INTERFACE_WARNING)
408 #pragma warning(pop)
409 #endif // if defined(JSONCPP_DISABLE_DLL_INTERFACE_WARNING)
410
411 #endif // CPPTL_JSON_READER_H_INCLUDED
JSONCPP_OVERRIDE
#define JSONCPP_OVERRIDE
Definition: config.h:94
JSONCPP_DEPRECATED
#define JSONCPP_DEPRECATED(message)
Definition: config.h:135
JSONCPP_ISTREAM
#define JSONCPP_ISTREAM
Definition: config.h:183
JSON_API
#define JSON_API
If defined, indicates that the source file is amalgated to prevent private header inclusion...
Definition: config.h:54
Json::Reader::StructuredError::message
std::string message
Definition: reader.h:49
Json::parseFromStream
bool parseFromStream(CharReader::Factory const &, std::istream &, Value *root, std::string *errs)
Consume entire stream and use its begin/end.
Definition: json_reader.cpp:2036
Json::CharReaderBuilder::settings_
Json::Value settings_
Configuration of this builder.
Definition: reader.h:338
JSONCPP_STRING
#define JSONCPP_STRING
Definition: config.h:179
Json::Reader::Char
char Char
Definition: reader.h:37
Json::Reader::StructuredError::offset_start
ptrdiff_t offset_start
Definition: reader.h:47
std
STL namespace.
Json::Reader::StructuredError
An error tagged with where in the JSON text it was encountered.
Definition: reader.h:46
Json::CommentPlacement
CommentPlacement
Definition: value.h:104
Json::Reader::Location
const Char * Location
Definition: reader.h:38
Json::CharReader::Factory::~Factory
virtual ~Factory()
Definition: reader.h:278
Json::CharReader::~CharReader
virtual ~CharReader()
Definition: reader.h:254
Json
JSON (JavaScript Object Notation).
Definition: allocator.h:14
Json::CharReader
Interface for reading JSON from a char array.
Definition: reader.h:252
Json::Value
Represents a JSON value.
Definition: value.h:177
Json::Reader
Unserialize a JSON document into a Value.
Definition: reader.h:35
value.h
Json::Reader::StructuredError::offset_limit
ptrdiff_t offset_limit
Definition: reader.h:48
Json::CharReaderBuilder
Build a CharReader implementation.
Definition: reader.h:298
Json::CharReader::Factory
Definition: reader.h:276
features.h
Json::Features
Configuration passed to reader and writer.
Definition: features.h:21