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 JSON_READER_H_INCLUDED
7#define 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)
27#pragma pack()
28
29namespace Json {
30
38public:
39 using Char = char;
40 using Location = const Char*;
41
48 ptrdiff_t offset_start;
49 ptrdiff_t offset_limit;
51 };
52
56 Reader();
57
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
158private:
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 using Errors = std::deque<ErrorInfo>;
191
192 bool readToken(Token& token);
193 bool readTokenSkippingComments(Token& token);
194 void skipSpaces();
195 bool match(const Char* pattern, int patternLength);
196 bool readComment();
197 bool readCStyleComment();
198 bool readCppStyleComment();
199 bool readString();
200 void readNumber();
201 bool readValue();
202 bool readObject(Token& token);
203 bool readArray(Token& token);
204 bool decodeNumber(Token& token);
205 bool decodeNumber(Token& token, Value& decoded);
206 bool decodeString(Token& token);
207 bool decodeString(Token& token, String& decoded);
208 bool decodeDouble(Token& token);
209 bool decodeDouble(Token& token, Value& decoded);
210 bool decodeUnicodeCodePoint(Token& token, Location& current, Location end,
211 unsigned int& unicode);
212 bool decodeUnicodeEscapeSequence(Token& token, Location& current,
213 Location end, unsigned int& unicode);
214 bool addError(const String& message, Token& token, Location extra = nullptr);
215 bool recoverFromError(TokenType skipUntilToken);
216 bool addErrorAndRecover(const String& message, Token& token,
217 TokenType skipUntilToken);
218 void skipUntilSpace();
219 Value& currentValue();
220 Char getNextChar();
221 void getLocationLineAndColumn(Location location, int& line,
222 int& column) const;
223 String getLocationLineAndColumn(Location location) const;
224 void addComment(Location begin, Location end, CommentPlacement placement);
225
226 static bool containsNewLine(Location begin, Location end);
227 static String normalizeEOL(Location begin, Location end);
228
229 using Nodes = std::stack<Value*>;
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
246public:
252
253 virtual ~CharReader() = default;
270 virtual bool parse(char const* beginDoc, char const* endDoc, Value* root,
271 String* errs);
272
276 std::vector<StructuredError> getStructuredErrors() const;
277
279 public:
280 virtual ~Factory() = default;
284 virtual CharReader* newCharReader() const = 0;
285 }; // Factory
286
287protected:
288 class Impl {
289 public:
290 virtual ~Impl() = default;
291 virtual bool parse(char const* beginDoc, char const* endDoc, Value* root,
292 String* errs) = 0;
293 virtual std::vector<StructuredError> getStructuredErrors() const = 0;
294 };
295
296 explicit CharReader(std::unique_ptr<Impl> impl) : _impl(std::move(impl)) {}
297
298private:
299 std::unique_ptr<Impl> _impl;
300}; // CharReader
301
315public:
316 // Note: We use a Json::Value so that we can add data-members to this class
317 // without a major version bump.
361
364
365 CharReader* newCharReader() const override;
366
370 bool validate(Json::Value* invalid) const;
371
374 Value& operator[](const String& key);
375
381 static void setDefaults(Json::Value* settings);
387 static void strictMode(Json::Value* settings);
393 static void ecma404Mode(Json::Value* settings);
394};
395
401 String* errs);
402
428
429} // namespace Json
430
431#pragma pack(pop)
432
433#if defined(JSONCPP_DISABLE_DLL_INTERFACE_WARNING)
434#pragma warning(pop)
435#endif // if defined(JSONCPP_DISABLE_DLL_INTERFACE_WARNING)
436
437#endif // JSON_READER_H_INCLUDED
virtual ~Factory()=default
virtual CharReader * newCharReader() const =0
Allocate a CharReader via operator new().
virtual std::vector< StructuredError > getStructuredErrors() const =0
virtual bool parse(char const *beginDoc, char const *endDoc, Value *root, String *errs)=0
virtual ~Impl()=default
Build a CharReader implementation.
Definition reader.h:314
Json::Value settings_
Configuration of this builder.
Definition reader.h:360
~CharReaderBuilder() override
Interface for reading JSON from a char array.
Definition reader.h:245
CharReader(std::unique_ptr< Impl > impl)
Definition reader.h:296
virtual ~CharReader()=default
Configuration passed to reader and writer.
Unserialize a JSON document into a Value.
Definition reader.h:37
char Char
Definition reader.h:39
const Char * Location
Definition reader.h:40
Represents a JSON value.
Definition value.h:194
#define JSON_API
If defined, indicates that the source file is amalgamated to prevent private header inclusion.
Definition config.h:50
#define JSONCPP_DEPRECATED(message)
Definition config.h:89
JSON (JavaScript Object Notation).
Definition allocator.h:15
std::basic_string< char, std::char_traits< char >, Allocator< char > > String
Definition config.h:132
IStream & operator>>(IStream &, Value &)
Read from 'sin' into 'root'.
bool parseFromStream(CharReader::Factory const &, IStream &, Value *root, String *errs)
Consume entire stream and use its begin/end.
std::istream IStream
Definition config.h:139
An error tagged with where in the JSON text it was encountered.
Definition reader.h:47