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
36
37 class JSON_API Reader {
38public:
39 using Char = char;
40 using Location = const Char*;
41
47 struct StructuredError {
48 ptrdiff_t offset_start;
49 ptrdiff_t offset_limit;
50 String message;
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
99 bool parse(const char* beginDoc, const char* endDoc, Value& root,
100 bool collectComments = true);
101
104 bool parse(IStream& is, Value& root, bool collectComments = true);
105
114 JSONCPP_DEPRECATED("Use getFormattedErrorMessages() instead.")
115 String getFormatedErrorMessages() const;
116
124 String getFormattedErrorMessages() const;
125
133 std::vector<StructuredError> getStructuredErrors() const;
134
142 bool pushError(const Value& value, const String& message);
143
152 bool pushError(const Value& value, const String& message, const Value& extra);
153
159 bool good() const;
160
161private:
162 enum TokenType {
163 tokenEndOfStream = 0,
164 tokenObjectBegin,
165 tokenObjectEnd,
166 tokenArrayBegin,
167 tokenArrayEnd,
168 tokenString,
169 tokenNumber,
170 tokenTrue,
171 tokenFalse,
172 tokenNull,
173 tokenArraySeparator,
174 tokenMemberSeparator,
175 tokenComment,
176 tokenError
177 };
178
179 class Token {
180 public:
181 TokenType type_;
182 Location start_;
183 Location end_;
184 };
185
186 class ErrorInfo {
187 public:
188 Token token_;
189 String message_;
190 Location extra_;
191 };
192
193 using Errors = std::deque<ErrorInfo>;
194
195 bool readToken(Token& token);
196 bool readTokenSkippingComments(Token& token);
197 void skipSpaces();
198 bool match(const Char* 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, Location& current, Location end,
214 unsigned int& unicode);
215 bool decodeUnicodeEscapeSequence(Token& token, Location& current,
216 Location end, unsigned int& unicode);
217 bool addError(const String& message, Token& token, Location extra = nullptr);
218 bool recoverFromError(TokenType skipUntilToken);
219 bool addErrorAndRecover(const String& message, Token& token,
220 TokenType skipUntilToken);
221 void skipUntilSpace();
222 Value& currentValue();
223 Char getNextChar();
224 void getLocationLineAndColumn(Location location, int& line,
225 int& column) const;
226 String getLocationLineAndColumn(Location location) const;
227 void addComment(Location begin, Location end, CommentPlacement placement);
228
229 static bool containsNewLine(Location begin, Location end);
230 static String normalizeEOL(Location begin, Location end);
231
232 using Nodes = std::stack<Value*>;
233 Nodes nodes_;
234 Errors errors_;
235 String document_;
236 Location begin_{};
237 Location end_{};
238 Location current_{};
239 Location lastValueEnd_{};
240 Value* lastValue_{};
241 String commentsBefore_;
242 Features features_;
243 bool collectComments_{};
244}; // Reader
245
248 class JSON_API CharReader {
249public:
250 struct JSON_API StructuredError {
251 ptrdiff_t offset_start;
252 ptrdiff_t offset_limit;
253 String message;
254 };
255
256 virtual ~CharReader() = default;
273 virtual bool parse(char const* beginDoc, char const* endDoc, Value* root,
274 String* errs);
275
279 std::vector<StructuredError> getStructuredErrors() const;
280
281 class JSON_API Factory {
282 public:
283 virtual ~Factory() = default;
287 virtual CharReader* newCharReader() const = 0;
288 }; // Factory
289
290protected:
291 class Impl {
292 public:
293 virtual ~Impl() = default;
294 virtual bool parse(char const* beginDoc, char const* endDoc, Value* root,
295 String* errs) = 0;
296 virtual std::vector<StructuredError> getStructuredErrors() const = 0;
297 };
298
299 explicit CharReader(std::unique_ptr<Impl> impl) : _impl(std::move(impl)) {}
300
301private:
302 std::unique_ptr<Impl> _impl;
303}; // CharReader
304
317 class JSON_API CharReaderBuilder : public CharReader::Factory {
318public:
319 // Note: We use a Json::Value so that we can add data-members to this class
320 // without a major version bump.
363 Json::Value settings_;
364
365 CharReaderBuilder();
366 ~CharReaderBuilder() override;
367
368 CharReader* newCharReader() const override;
369
373 bool validate(Json::Value* invalid) const;
374
377 Value& operator[](const String& key);
378
384 static void setDefaults(Json::Value* settings);
390 static void strictMode(Json::Value* settings);
396 static void ecma404Mode(Json::Value* settings);
397};
398
403bool JSON_API parseFromStream(CharReader::Factory const&, IStream&, Value* root,
404 String* errs);
405
430 JSON_API IStream& operator>>(IStream&, Value&);
431
432} // namespace Json
433
434#pragma pack(pop)
435
436#if defined(JSONCPP_DISABLE_DLL_INTERFACE_WARNING)
437#pragma warning(pop)
438#endif // if defined(JSONCPP_DISABLE_DLL_INTERFACE_WARNING)
439
440#endif // JSON_READER_H_INCLUDED
Json::CharReader::Factory
Definition reader.h:281
Json::CharReader::Factory::~Factory
virtual ~Factory()=default
Json::CharReader::Factory::newCharReader
virtual CharReader * newCharReader() const =0
Allocate a CharReader via operator new().
Json::CharReader::Impl
Definition reader.h:291
Json::CharReader::Impl::getStructuredErrors
virtual std::vector< StructuredError > getStructuredErrors() const =0
Json::CharReader::Impl::parse
virtual bool parse(char const *beginDoc, char const *endDoc, Value *root, String *errs)=0
Json::CharReader::Impl::~Impl
virtual ~Impl()=default
Json::CharReaderBuilder
Build a CharReader implementation.
Definition reader.h:317
Json::CharReaderBuilder::setDefaults
static void setDefaults(Json::Value *settings)
Called by ctor, but you can use this to reset settings_.
Definition json_reader.cpp:1960
Json::CharReaderBuilder::ecma404Mode
static void ecma404Mode(Json::Value *settings)
ECMA-404 mode.
Definition json_reader.cpp:1977
Json::CharReaderBuilder::operator[]
Value & operator[](const String &key)
A simple way to update a specific setting.
Definition json_reader.cpp:1940
Json::CharReaderBuilder::strictMode
static void strictMode(Json::Value *settings)
Same as old Features::strictMode().
Definition json_reader.cpp:1944
Json::CharReaderBuilder::validate
bool validate(Json::Value *invalid) const
Configuration of this builder.
Definition json_reader.cpp:1913
Json::CharReader
Interface for reading JSON from a char array.
Definition reader.h:248
Json::CharReader::CharReader
CharReader(std::unique_ptr< Impl > impl)
Definition reader.h:299
Json::CharReader::~CharReader
virtual ~CharReader()=default
Json::CharReader::getStructuredErrors
std::vector< StructuredError > getStructuredErrors() const
Returns a vector of structured errors encountered while parsing.
Definition json_reader.cpp:1994
Json::CharReader::parse
virtual bool parse(char const *beginDoc, char const *endDoc, Value *root, String *errs)
Read a Value from a JSON document.
Definition json_reader.cpp:1998
Json::Features
Configuration passed to reader and writer.
Definition json_features.h:22
Json::Reader::Char
char Char
Definition reader.h:39
Json::Reader::Reader
Reader()
Constructs a Reader allowing all features for parsing.
Definition json_reader.cpp:78
Json::Reader::pushError
bool pushError(const Value &value, const String &message)
Add a semantic error message.
Definition json_reader.cpp:808
Json::Reader::good
bool good() const
Return whether there are any errors.
Definition json_reader.cpp:842
Json::Reader::Location
const Char * Location
Definition reader.h:40
Json::Reader::getStructuredErrors
std::vector< StructuredError > getStructuredErrors() const
Returns a vector of structured errors encountered while parsing.
Definition json_reader.cpp:796
Json::Reader::parse
bool parse(const std::string &document, Value &root, bool collectComments=true)
Read a Value from a JSON document.
Definition json_reader.cpp:82
Json::Reader::getFormattedErrorMessages
String getFormattedErrorMessages() const
Returns a user friendly string that list errors in the parsed document.
Definition json_reader.cpp:783
Json::Value
Represents a JSON value.
Definition value.h:207
JSON_API
#define JSON_API
If defined, indicates that the source file is amalgamated to prevent private header inclusion.
Definition config.h:50
JSONCPP_DEPRECATED
#define JSONCPP_DEPRECATED(message)
Definition config.h:89
json_features.h
Json
JSON (JavaScript Object Notation).
Definition allocator.h:16
Json::IStream
std::istream IStream
Definition config.h:142
Json::String
std::basic_string< char, std::char_traits< char >, Allocator< char > > String
Definition config.h:135
Json::operator>>
IStream & operator>>(IStream &, Value &)
Read from 'sin' into 'root'.
Definition json_reader.cpp:2018
Json::parseFromStream
bool parseFromStream(CharReader::Factory const &, IStream &, Value *root, String *errs)
Consume entire stream and use its begin/end.
Definition json_reader.cpp:2006
Json::CharReader::StructuredError
Definition reader.h:250
Json::CharReader::StructuredError::offset_limit
ptrdiff_t offset_limit
Definition reader.h:252
Json::CharReader::StructuredError::offset_start
ptrdiff_t offset_start
Definition reader.h:251
Json::CharReader::StructuredError::message
String message
Definition reader.h:253
Json::Reader::StructuredError
An error tagged with where in the JSON text it was encountered.
Definition reader.h:47
Json::Reader::StructuredError::message
String message
Definition reader.h:50
Json::Reader::StructuredError::offset_start
ptrdiff_t offset_start
Definition reader.h:48
Json::Reader::StructuredError::offset_limit
ptrdiff_t offset_limit
Definition reader.h:49
value.h