JsonCpp project page | Classes | Namespace | 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 <istream>
16 #include <stack>
17 #include <string>
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 String message;
50 };
51
55 JSONCPP_DEPRECATED("Use CharReader and CharReaderBuilder instead")
56 Reader();
57
61 JSONCPP_DEPRECATED("Use CharReader and CharReaderBuilder instead")
62 Reader(const Features& features);
63
78 bool
79 parse(const std::string& document, Value& root, bool collectComments = true);
80
99 bool parse(const char* beginDoc,
100 const char* endDoc,
101 Value& root,
102 bool collectComments = true);
103
106 bool parse(IStream& is, Value& root, bool collectComments = true);
107
117 JSONCPP_DEPRECATED("Use getFormattedErrorMessages() instead.")
118 String getFormatedErrorMessages() const;
119
128 String getFormattedErrorMessages() const;
129
137 std::vector<StructuredError> getStructuredErrors() const;
138
145 bool pushError(const Value& value, const String& message);
146
154 bool pushError(const Value& value, const String& message, const Value& extra);
155
160 bool good() const;
161
162 private:
163 enum TokenType {
164 tokenEndOfStream = 0,
165 tokenObjectBegin,
166 tokenObjectEnd,
167 tokenArrayBegin,
168 tokenArrayEnd,
169 tokenString,
170 tokenNumber,
171 tokenTrue,
172 tokenFalse,
173 tokenNull,
174 tokenArraySeparator,
175 tokenMemberSeparator,
176 tokenComment,
177 tokenError
178 };
179
180 class Token {
181 public:
182 TokenType type_;
183 Location start_;
184 Location end_;
185 };
186
187 class ErrorInfo {
188 public:
189 Token token_;
190 String message_;
191 Location extra_;
192 };
193
194 typedef std::deque<ErrorInfo> Errors;
195
196 bool readToken(Token& token);
197 void skipSpaces();
198 bool match(Location pattern, int patternLength);
199 bool readComment();
200 bool readCStyleComment();
201 bool readCppStyleComment();
202 bool readString();
203 void readNumber();
204 bool readValue();
205 bool readObject(Token& token);
206 bool readArray(Token& token);
207 bool decodeNumber(Token& token);
208 bool decodeNumber(Token& token, Value& decoded);
209 bool decodeString(Token& token);
210 bool decodeString(Token& token, String& decoded);
211 bool decodeDouble(Token& token);
212 bool decodeDouble(Token& token, Value& decoded);
213 bool decodeUnicodeCodePoint(Token& token,
214 Location& current,
215 Location end,
216 unsigned int& unicode);
217 bool decodeUnicodeEscapeSequence(Token& token,
218 Location& current,
219 Location end,
220 unsigned int& unicode);
221 bool addError(const String& message, Token& token, Location extra = nullptr);
222 bool recoverFromError(TokenType skipUntilToken);
223 bool addErrorAndRecover(const String& message,
224 Token& token,
225 TokenType skipUntilToken);
226 void skipUntilSpace();
227 Value& currentValue();
228 Char getNextChar();
229 void
230 getLocationLineAndColumn(Location location, int& line, int& column) const;
231 String getLocationLineAndColumn(Location location) const;
232 void addComment(Location begin, Location end, CommentPlacement placement);
233 void skipCommentTokens(Token& token);
234
235 static bool containsNewLine(Location begin, Location end);
236 static String normalizeEOL(Location begin, Location end);
237
238 typedef std::stack<Value*> Nodes;
239 Nodes nodes_;
240 Errors errors_;
241 String document_;
242 Location begin_{};
243 Location end_{};
244 Location current_{};
245 Location lastValueEnd_{};
246 Value* lastValue_{};
247 String commentsBefore_;
248 Features features_;
249 bool collectComments_{};
250 }; // Reader
251
254 class JSON_API CharReader {
255 public:
256 virtual ~CharReader() = default;
275 virtual bool parse(char const* beginDoc,
276 char const* endDoc,
277 Value* root,
278 String* errs) = 0;
279
280 class JSON_API Factory {
281 public:
282 virtual ~Factory() = default;
286 virtual CharReader* newCharReader() const = 0;
287 }; // Factory
288 }; // CharReader
289
302 class JSON_API CharReaderBuilder : public CharReader::Factory {
303 public:
304 // Note: We use a Json::Value so that we can add data-members to this class
305 // without a major version bump.
344 Json::Value settings_;
345
346 CharReaderBuilder();
347 ~CharReaderBuilder() override;
348
349 CharReader* newCharReader() const override;
350
354 bool validate(Json::Value* invalid) const;
355
358 Value& operator[](const String& key);
359
365 static void setDefaults(Json::Value* settings);
371 static void strictMode(Json::Value* settings);
372 };
373
378 bool JSON_API parseFromStream(CharReader::Factory const&,
379 IStream&,
380 Value* root,
381 String* errs);
382
407 JSON_API IStream& operator>>(IStream&, Value&);
408
409 } // namespace Json
410
411 #pragma pack(pop)
412
413 #if defined(JSONCPP_DISABLE_DLL_INTERFACE_WARNING)
414 #pragma warning(pop)
415 #endif // if defined(JSONCPP_DISABLE_DLL_INTERFACE_WARNING)
416
417 #endif // CPPTL_JSON_READER_H_INCLUDED
Json::CharReader::Factory
Definition: reader.h:280
Json::Reader::StructuredError::offset_limit
ptrdiff_t offset_limit
Definition: reader.h:48
features.h
Json::IStream
std::istream IStream
Definition: config.h:169
Json::Reader
Unserialize a JSON document into a Value.
Definition: reader.h:35
Json::Reader::StructuredError::message
String message
Definition: reader.h:49
Json::CharReaderBuilder::settings_
Json::Value settings_
Configuration of this builder.
Definition: reader.h:344
Json::Value
Represents a JSON value.
Definition: value.h:176
Json::Reader::Char
char Char
Definition: reader.h:37
Json::operator>>
IStream & operator>>(IStream &, Value &)
Read from 'sin' into 'root'.
Definition: json_reader.cpp:2007
Json::CommentPlacement
CommentPlacement
Definition: value.h:96
JSONCPP_DEPRECATED
#define JSONCPP_DEPRECATED(message)
Definition: config.h:119
Json
JSON (JavaScript Object Notation).
Definition: allocator.h:14
JSON_API
#define JSON_API
If defined, indicates that the source file is amalgamated to prevent private header inclusion.
Definition: config.h:66
Json::Reader::StructuredError
An error tagged with where in the JSON text it was encountered.
Definition: reader.h:46
Json::String
std::basic_string< char, std::char_traits< char >, Allocator< char > > String
Definition: config.h:162
Json::CharReader
Interface for reading JSON from a char array.
Definition: reader.h:254
Json::parseFromStream
bool parseFromStream(CharReader::Factory const &, IStream &, Value *root, String *errs)
Consume entire stream and use its begin/end.
Definition: json_reader.cpp:1993
Json::Reader::Location
const typedef Char * Location
Definition: reader.h:38
value.h
Json::Features
Configuration passed to reader and writer.
Definition: features.h:21
Json::Reader::StructuredError::offset_start
ptrdiff_t offset_start
Definition: reader.h:47
Json::CharReaderBuilder
Build a CharReader implementation.
Definition: reader.h:302