JsonCpp - JSON data format manipulation library

JsonCpp project page | JsonCpp home page

reader.h
Go to the documentation of this file.
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
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 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 typedef std::stack<Value*> Nodes;
234 Nodes nodes_;
235 Errors errors_;
236 JSONCPP_STRING document_;
237 Location begin_;
238 Location end_;
239 Location current_;
240 Location lastValueEnd_;
241 Value* lastValue_;
242 JSONCPP_STRING commentsBefore_;
243 Features features_;
244 bool collectComments_;
245 }; // Reader
246
249 class JSON_API CharReader {
250 public:
251 virtual ~CharReader() {}
269 virtual bool parse(
270 char const* beginDoc, char const* endDoc,
271 Value* root, JSONCPP_STRING* errs) = 0;
272
273 class JSON_API Factory {
274 public:
275 virtual ~Factory() {}
279 virtual CharReader* newCharReader() const = 0;
280 }; // Factory
281 }; // CharReader
282
295 class JSON_API CharReaderBuilder : public CharReader::Factory {
296 public:
297 // Note: We use a Json::Value so that we can add data-members to this class
298 // without a major version bump.
335 Json::Value settings_;
336
337 CharReaderBuilder();
338 ~CharReaderBuilder() JSONCPP_OVERRIDE;
339
340 CharReader* newCharReader() const JSONCPP_OVERRIDE;
341
345 bool validate(Json::Value* invalid) const;
346
349 Value& operator[](JSONCPP_STRING key);
350
356 static void setDefaults(Json::Value* settings);
362 static void strictMode(Json::Value* settings);
363 };
364
369 bool JSON_API parseFromStream(
370 CharReader::Factory const&,
371 JSONCPP_ISTREAM&,
372 Value* root, std::string* errs);
373
398 JSON_API JSONCPP_ISTREAM& operator>>(JSONCPP_ISTREAM&, Value&);
399
400 } // namespace Json
401
402 #pragma pack(pop)
403
404 #if defined(JSONCPP_DISABLE_DLL_INTERFACE_WARNING)
405 #pragma warning(pop)
406 #endif // if defined(JSONCPP_DISABLE_DLL_INTERFACE_WARNING)
407
408 #endif // CPPTL_JSON_READER_H_INCLUDED
JSONCPP_OVERRIDE
#define JSONCPP_OVERRIDE
Definition: config.h:94
JSONCPP_DEPRECATED
#define JSONCPP_DEPRECATED(message)
Definition: config.h:132
JSONCPP_ISTREAM
#define JSONCPP_ISTREAM
Definition: config.h:180
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:2008
Json::CharReaderBuilder::settings_
Json::Value settings_
Configuration of this builder.
Definition: reader.h:335
JSONCPP_STRING
#define JSONCPP_STRING
Definition: config.h:176
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:275
Json::CharReader::~CharReader
virtual ~CharReader()
Definition: reader.h:251
Json
JSON (JavaScript Object Notation).
Definition: allocator.h:14
Json::CharReader
Interface for reading JSON from a char array.
Definition: reader.h:249
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:295
Json::CharReader::Factory
Definition: reader.h:273
features.h
Json::Features
Configuration passed to reader and writer.
Definition: features.h:21