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, 8)
27
28 namespace Json {
29
36 class JSON_API Reader {
37 public:
38 using Char = char;
39 using Location = const Char*;
40
46 struct StructuredError {
47 ptrdiff_t offset_start;
48 ptrdiff_t offset_limit;
49 String message;
50 };
51
55 Reader();
56
60 Reader(const Features& features);
61
76 bool parse(const std::string& document, Value& root,
77 bool collectComments = true);
78
95 bool parse(const char* beginDoc, const char* endDoc, Value& root,
96 bool collectComments = true);
97
100 bool parse(IStream& is, Value& root, bool collectComments = true);
101
110 JSONCPP_DEPRECATED("Use getFormattedErrorMessages() instead.")
111 String getFormatedErrorMessages() const;
112
120 String getFormattedErrorMessages() const;
121
129 std::vector<StructuredError> getStructuredErrors() const;
130
138 bool pushError(const Value& value, const String& message);
139
148 bool pushError(const Value& value, const String& message, const Value& extra);
149
155 bool good() const;
156
157private:
158 enum TokenType {
159 tokenEndOfStream = 0,
160 tokenObjectBegin,
161 tokenObjectEnd,
162 tokenArrayBegin,
163 tokenArrayEnd,
164 tokenString,
165 tokenNumber,
166 tokenTrue,
167 tokenFalse,
168 tokenNull,
169 tokenArraySeparator,
170 tokenMemberSeparator,
171 tokenComment,
172 tokenError
173 };
174
175 class Token {
176 public:
177 TokenType type_;
178 Location start_;
179 Location end_;
180 };
181
182 class ErrorInfo {
183 public:
184 Token token_;
185 String message_;
186 Location extra_;
187 };
188
189 using Errors = std::deque<ErrorInfo>;
190
191 bool readToken(Token& token);
192 void skipSpaces();
193 bool match(const Char* pattern, int patternLength);
194 bool readComment();
195 bool readCStyleComment();
196 bool readCppStyleComment();
197 bool readString();
198 void readNumber();
199 bool readValue();
200 bool readObject(Token& token);
201 bool readArray(Token& token);
202 bool decodeNumber(Token& token);
203 bool decodeNumber(Token& token, Value& decoded);
204 bool decodeString(Token& token);
205 bool decodeString(Token& token, String& decoded);
206 bool decodeDouble(Token& token);
207 bool decodeDouble(Token& token, Value& decoded);
208 bool decodeUnicodeCodePoint(Token& token, Location& current, Location end,
209 unsigned int& unicode);
210 bool decodeUnicodeEscapeSequence(Token& token, Location& current,
211 Location end, unsigned int& unicode);
212 bool addError(const String& message, Token& token, Location extra = nullptr);
213 bool recoverFromError(TokenType skipUntilToken);
214 bool addErrorAndRecover(const String& message, Token& token,
215 TokenType skipUntilToken);
216 void skipUntilSpace();
217 Value& currentValue();
218 Char getNextChar();
219 void getLocationLineAndColumn(Location location, int& line,
220 int& column) const;
221 String getLocationLineAndColumn(Location location) const;
222 void addComment(Location begin, Location end, CommentPlacement placement);
223 void skipCommentTokens(Token& token);
224
225 static bool containsNewLine(Location begin, Location end);
226 static String normalizeEOL(Location begin, Location end);
227
228 using Nodes = std::stack<Value*>;
229 Nodes nodes_;
230 Errors errors_;
231 String document_;
232 Location begin_{};
233 Location end_{};
234 Location current_{};
235 Location lastValueEnd_{};
236 Value* lastValue_{};
237 String commentsBefore_;
238 Features features_;
239 bool collectComments_{};
240}; // Reader
241
244 class JSON_API CharReader {
245 public:
246 virtual ~CharReader() = default;
263 virtual bool parse(char const* beginDoc, char const* endDoc, Value* root,
264 String* errs) = 0;
265
266 class JSON_API Factory {
267 public:
268 virtual ~Factory() = default;
272 virtual CharReader* newCharReader() const = 0;
273 }; // Factory
274}; // CharReader
275
288 class JSON_API CharReaderBuilder : public CharReader::Factory {
289 public:
290 // Note: We use a Json::Value so that we can add data-members to this class
291 // without a major version bump.
334 Json::Value settings_;
335
336 CharReaderBuilder();
337 ~CharReaderBuilder() override;
338
339 CharReader* newCharReader() const override;
340
344 bool validate(Json::Value* invalid) const;
345
348 Value& operator[](const String& key);
349
355 static void setDefaults(Json::Value* settings);
361 static void strictMode(Json::Value* settings);
362};
363
368 bool JSON_API parseFromStream(CharReader::Factory const&, IStream&, Value* root,
369 String* errs);
370
395 JSON_API IStream& operator>>(IStream&, Value&);
396
397} // namespace Json
398
399#pragma pack(pop)
400
401#if defined(JSONCPP_DISABLE_DLL_INTERFACE_WARNING)
402#pragma warning(pop)
403#endif // if defined(JSONCPP_DISABLE_DLL_INTERFACE_WARNING)
404
405#endif // JSON_READER_H_INCLUDED
Json::CharReader::Factory
Definition reader.h:266
Json::CharReader::Factory::~Factory
virtual ~Factory()=default
Json::CharReader::Factory::newCharReader
virtual CharReader * newCharReader() const =0
Allocate a CharReader via operator new().
Json::CharReaderBuilder
Build a CharReader implementation.
Definition reader.h:288
Json::CharReaderBuilder::settings_
Json::Value settings_
Configuration of this builder.
Definition reader.h:334
Json::CharReaderBuilder::~CharReaderBuilder
~CharReaderBuilder() override
Json::CharReader
Interface for reading JSON from a char array.
Definition reader.h:244
Json::CharReader::~CharReader
virtual ~CharReader()=default
Json::CharReader::parse
virtual bool parse(char const *beginDoc, char const *endDoc, Value *root, String *errs)=0
Read a Value from a JSON document.
Json::Features
Configuration passed to reader and writer.
Definition json_features.h:21
Json::Reader
Unserialize a JSON document into a Value.
Definition reader.h:36
Json::Reader::Char
char Char
Definition reader.h:38
Json::Reader::Location
const Char * Location
Definition reader.h:39
Json::Value
Represents a JSON value.
Definition value.h:193
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:14
Json::IStream
std::istream IStream
Definition config.h:139
Json::String
std::basic_string< char, std::char_traits< char >, Allocator< char > > String
Definition config.h:132
Json::operator>>
IStream & operator>>(IStream &, Value &)
Read from 'sin' into 'root'.
Definition json_reader.cpp:1982
Json::parseFromStream
bool parseFromStream(CharReader::Factory const &, IStream &, Value *root, String *errs)
Consume entire stream and use its begin/end.
Definition json_reader.cpp:1970
Json::Reader::StructuredError
An error tagged with where in the JSON text it was encountered.
Definition reader.h:46
Json::Reader::StructuredError::message
String message
Definition reader.h:49
Json::Reader::StructuredError::offset_start
ptrdiff_t offset_start
Definition reader.h:47
Json::Reader::StructuredError::offset_limit
ptrdiff_t offset_limit
Definition reader.h:48
value.h