NIM PC Cross Platform SDK
reader.h
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 NIM_CPP_WRAPPER_UTIL_JSON_READER_H_INCLUDED
7#define NIM_CPP_WRAPPER_UTIL_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
28namespace nim_cpp_wrapper_util {
29namespace Json {
30
37class JSON_API Reader {
38public:
39 using Char = char;
40 using Location = const Char*;
41
48 ptrdiff_t offset_start;
49 ptrdiff_t offset_limit;
50 String message;
51 };
52
55 Reader();
56
59 Reader(const Features& features);
60
75 bool parse(const std::string& document, Value& root, bool collectComments = true);
76
93 bool parse(const char* beginDoc, const char* endDoc, Value& root, bool collectComments = true);
94
97 bool parse(IStream& is, Value& root, bool collectComments = true);
98
107 String getFormatedErrorMessages() const;
108
116 String getFormattedErrorMessages() const;
117
125 std::vector<StructuredError> getStructuredErrors() const;
126
134 bool pushError(const Value& value, const String& message);
135
144 bool pushError(const Value& value, const String& message, const Value& extra);
145
151 bool good() const;
152
153private:
154 enum TokenType {
155 tokenEndOfStream = 0,
156 tokenObjectBegin,
157 tokenObjectEnd,
158 tokenArrayBegin,
159 tokenArrayEnd,
160 tokenString,
161 tokenNumber,
162 tokenTrue,
163 tokenFalse,
164 tokenNull,
165 tokenArraySeparator,
166 tokenMemberSeparator,
167 tokenComment,
168 tokenError
169 };
170
171 class Token {
172 public:
173 TokenType type_;
174 Location start_;
175 Location end_;
176 };
177
178 class ErrorInfo {
179 public:
180 Token token_;
181 String message_;
182 Location extra_;
183 };
184
185 using Errors = std::deque<ErrorInfo>;
186
187 bool readToken(Token& token);
188 void skipSpaces();
189 bool match(const Char* pattern, int patternLength);
190 bool readComment();
191 bool readCStyleComment();
192 bool readCppStyleComment();
193 bool readString();
194 void readNumber();
195 bool readValue();
196 bool readObject(Token& token);
197 bool readArray(Token& token);
198 bool decodeNumber(Token& token);
199 bool decodeNumber(Token& token, Value& decoded);
200 bool decodeString(Token& token);
201 bool decodeString(Token& token, String& decoded);
202 bool decodeDouble(Token& token);
203 bool decodeDouble(Token& token, Value& decoded);
204 bool decodeUnicodeCodePoint(Token& token, Location& current, Location end, unsigned int& unicode);
205 bool decodeUnicodeEscapeSequence(Token& token, Location& current, Location end, unsigned int& unicode);
206 bool addError(const String& message, Token& token, Location extra = nullptr);
207 bool recoverFromError(TokenType skipUntilToken);
208 bool addErrorAndRecover(const String& message, Token& token, TokenType skipUntilToken);
209 void skipUntilSpace();
210 Value& currentValue();
211 Char getNextChar();
212 void getLocationLineAndColumn(Location location, int& line, int& column) const;
213 String getLocationLineAndColumn(Location location) const;
214 void addComment(Location begin, Location end, CommentPlacement placement);
215 void skipCommentTokens(Token& token);
216
217 static bool containsNewLine(Location begin, Location end);
218 static String normalizeEOL(Location begin, Location end);
219
220 using Nodes = std::stack<Value*>;
221 Nodes nodes_;
222 Errors errors_;
223 String document_;
224 Location begin_{};
225 Location end_{};
226 Location current_{};
227 Location lastValueEnd_{};
228 Value* lastValue_{};
229 String commentsBefore_;
230 Features features_;
231 bool collectComments_{};
232}; // Reader
233
236class JSON_API CharReader {
237public:
238 virtual ~CharReader() = default;
255 virtual bool parse(char const* beginDoc, char const* endDoc, Value* root, String* errs) = 0;
256
257 class JSON_API Factory {
258 public:
259 virtual ~Factory() = default;
263 virtual CharReader* newCharReader() const = 0;
264 }; // Factory
265}; // CharReader
266
279class JSON_API CharReaderBuilder : public CharReader::Factory {
280public:
281 // Note: We use a nim_cpp_wrapper_util::Json::Value so that we can add data-members to this class
282 // without a major version bump.
323
325 ~CharReaderBuilder() override;
326
327 CharReader* newCharReader() const override;
328
332 bool validate(nim_cpp_wrapper_util::Json::Value* invalid) const;
333
336 Value& operator[](const String& key);
337
343 static void setDefaults(nim_cpp_wrapper_util::Json::Value* settings);
349 static void strictMode(nim_cpp_wrapper_util::Json::Value* settings);
350};
351
356bool JSON_API parseFromStream(CharReader::Factory const&, IStream&, Value* root, String* errs);
357
382JSON_API IStream& operator>>(IStream&, Value&);
383
384} // namespace Json
385} // namespace nim_cpp_wrapper_util
386
387#pragma pack(pop)
388
389#if defined(JSONCPP_DISABLE_DLL_INTERFACE_WARNING)
390#pragma warning(pop)
391#endif // if defined(JSONCPP_DISABLE_DLL_INTERFACE_WARNING)
392
393#endif // JSON_READER_H_INCLUDED
virtual CharReader * newCharReader() const =0
Allocate a CharReader via operator new().
Build a CharReader implementation.
Definition: reader.h:279
nim_cpp_wrapper_util::Json::Value settings_
Definition: reader.h:322
virtual bool parse(char const *beginDoc, char const *endDoc, Value *root, String *errs)=0
Read a Value from a JSON document. The document must be a UTF-8 encoded string containing the documen...
Configuration passed to reader and writer. This configuration object can be used to force the Reader ...
Definition: json_features.h:22
Unserialize a JSON document into a Value.
Definition: reader.h:37
Represents a JSON value.
Definition: value.h:196
JSON (JavaScript Object Notation).
Definition: allocator.h:14
An error tagged with where in the JSON text it was encountered.
Definition: reader.h:47