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 "json_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
36 class JSONCPP_DEPRECATED(
37 "Use CharReader and CharReaderBuilder instead.") JSON_API Reader {
38 public:
39 typedef char Char;
40 typedef const Char* Location;
41
47 struct StructuredError {
48 ptrdiff_t offset_start;
49 ptrdiff_t offset_limit;
50 String message;
51 };
52
55 JSONCPP_DEPRECATED("Use CharReader and CharReaderBuilder instead")
56 Reader();
57
60 JSONCPP_DEPRECATED("Use CharReader and CharReaderBuilder instead")
61 Reader(const Features& features);
62
77 bool parse(const std::string& document, Value& root,
78 bool collectComments = true);
79
96 bool parse(const char* beginDoc, const char* endDoc, Value& root,
97 bool collectComments = true);
98
101 bool parse(IStream& is, Value& root, bool collectComments = true);
102
111 JSONCPP_DEPRECATED("Use getFormattedErrorMessages() instead.")
112 String getFormatedErrorMessages() const;
113
121 String getFormattedErrorMessages() const;
122
130 std::vector<StructuredError> getStructuredErrors() const;
131
139 bool pushError(const Value& value, const String& message);
140
149 bool pushError(const Value& value, const String& message, const Value& extra);
150
156 bool good() const;
157
158 private:
159 enum TokenType {
160 tokenEndOfStream = 0,
161 tokenObjectBegin,
162 tokenObjectEnd,
163 tokenArrayBegin,
164 tokenArrayEnd,
165 tokenString,
166 tokenNumber,
167 tokenTrue,
168 tokenFalse,
169 tokenNull,
170 tokenArraySeparator,
171 tokenMemberSeparator,
172 tokenComment,
173 tokenError
174 };
175
176 class Token {
177 public:
178 TokenType type_;
179 Location start_;
180 Location end_;
181 };
182
183 class ErrorInfo {
184 public:
185 Token token_;
186 String message_;
187 Location extra_;
188 };
189
190 typedef std::deque<ErrorInfo> Errors;
191
192 bool readToken(Token& token);
193 void skipSpaces();
194 bool match(const Char* pattern, int patternLength);
195 bool readComment();
196 bool readCStyleComment();
197 bool readCppStyleComment();
198 bool readString();
199 void readNumber();
200 bool readValue();
201 bool readObject(Token& token);
202 bool readArray(Token& token);
203 bool decodeNumber(Token& token);
204 bool decodeNumber(Token& token, Value& decoded);
205 bool decodeString(Token& token);
206 bool decodeString(Token& token, String& decoded);
207 bool decodeDouble(Token& token);
208 bool decodeDouble(Token& token, Value& decoded);
209 bool decodeUnicodeCodePoint(Token& token, Location& current, Location end,
210 unsigned int& unicode);
211 bool decodeUnicodeEscapeSequence(Token& token, Location& current,
212 Location end, unsigned int& unicode);
213 bool addError(const String& message, Token& token, Location extra = nullptr);
214 bool recoverFromError(TokenType skipUntilToken);
215 bool addErrorAndRecover(const String& message, Token& token,
216 TokenType skipUntilToken);
217 void skipUntilSpace();
218 Value& currentValue();
219 Char getNextChar();
220 void getLocationLineAndColumn(Location location, int& line,
221 int& column) const;
222 String getLocationLineAndColumn(Location location) const;
223 void addComment(Location begin, Location end, CommentPlacement placement);
224 void skipCommentTokens(Token& token);
225
226 static bool containsNewLine(Location begin, Location end);
227 static String normalizeEOL(Location begin, Location end);
228
229 typedef std::stack<Value*> Nodes;
230 Nodes nodes_;
231 Errors errors_;
232 String document_;
233 Location begin_{};
234 Location end_{};
235 Location current_{};
236 Location lastValueEnd_{};
237 Value* lastValue_{};
238 String commentsBefore_;
239 Features features_;
240 bool collectComments_{};
241 }; // Reader
242
245 class JSON_API CharReader {
246 public:
247 virtual ~CharReader() = default;
264 virtual bool parse(char const* beginDoc, char const* endDoc, Value* root,
265 String* errs) = 0;
266
267 class JSON_API Factory {
268 public:
269 virtual ~Factory() = default;
273 virtual CharReader* newCharReader() const = 0;
274 }; // Factory
275 }; // CharReader
276
289 class JSON_API CharReaderBuilder : public CharReader::Factory {
290 public:
291 // Note: We use a Json::Value so that we can add data-members to this class
292 // without a major version bump.
330 Json::Value settings_;
331
332 CharReaderBuilder();
333 ~CharReaderBuilder() override;
334
335 CharReader* newCharReader() const override;
336
340 bool validate(Json::Value* invalid) const;
341
344 Value& operator[](const String& key);
345
351 static void setDefaults(Json::Value* settings);
357 static void strictMode(Json::Value* settings);
358 };
359
364 bool JSON_API parseFromStream(CharReader::Factory const&, IStream&, Value* root,
365 String* errs);
366
391 JSON_API IStream& operator>>(IStream&, Value&);
392
393 } // namespace Json
394
395 #pragma pack(pop)
396
397 #if defined(JSONCPP_DISABLE_DLL_INTERFACE_WARNING)
398 #pragma warning(pop)
399 #endif // if defined(JSONCPP_DISABLE_DLL_INTERFACE_WARNING)
400
401 #endif // CPPTL_JSON_READER_H_INCLUDED
Json::CharReader::Factory
Definition: reader.h:267
Json::IStream
std::istream IStream
Definition: config.h:169
Json::CharReaderBuilder::settings_
Json::Value settings_
Configuration of this builder.
Definition: reader.h:330
Json::Value
Represents a JSON value.
Definition: value.h:188
Json::operator>>
IStream & operator>>(IStream &, Value &)
Read from 'sin' into 'root'.
Definition: json_reader.cpp:1961
Json::CommentPlacement
CommentPlacement
Definition: value.h:108
JSONCPP_DEPRECATED
#define JSONCPP_DEPRECATED(message)
Definition: config.h:119
json_features.h
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::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:245
Json::parseFromStream
bool parseFromStream(CharReader::Factory const &, IStream &, Value *root, String *errs)
Consume entire stream and use its begin/end.
Definition: json_reader.cpp:1949
value.h
Json::Features
Configuration passed to reader and writer.
Definition: json_features.h:21
Json::CharReaderBuilder
Build a CharReader implementation.
Definition: reader.h:289