NIM 跨平台 C++ 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
38public:
39 using Char = char;
40 using Location = const Char*;
41
48 ptrdiff_t offset_start;
49 ptrdiff_t offset_limit;
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:
155 tokenEndOfStream = 0,
168 tokenError
169 };
170
171 class Token {
172 public:
176 };
177
178 class ErrorInfo {
179 public:
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);
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*>;
224 Location begin_{};
225 Location end_{};
226 Location current_{};
227 Location lastValueEnd_{};
228 Value* lastValue_{};
231 bool collectComments_{};
232}; // Reader
233
237public:
238 virtual ~CharReader() = default;
255 virtual bool parse(char const* beginDoc, char const* endDoc, Value* root, String* errs) = 0;
256
258 public:
259 virtual ~Factory() = default;
263 virtual CharReader* newCharReader() const = 0;
264 }; // Factory
265}; // CharReader
266
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
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
357
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
Token token_
Definition: reader.h:180
Location extra_
Definition: reader.h:182
String message_
Definition: reader.h:181
Location start_
Definition: reader.h:174
Location end_
Definition: reader.h:175
TokenType type_
Definition: reader.h:173
Unserialize a JSON document into a Value.
Definition: reader.h:37
TokenType
Definition: reader.h:154
@ tokenFalse
Definition: reader.h:163
@ tokenNull
Definition: reader.h:164
@ tokenObjectEnd
Definition: reader.h:157
@ tokenMemberSeparator
Definition: reader.h:166
@ tokenString
Definition: reader.h:160
@ tokenNumber
Definition: reader.h:161
@ tokenComment
Definition: reader.h:167
@ tokenTrue
Definition: reader.h:162
@ tokenArrayEnd
Definition: reader.h:159
@ tokenArraySeparator
Definition: reader.h:165
@ tokenObjectBegin
Definition: reader.h:156
@ tokenArrayBegin
Definition: reader.h:158
String commentsBefore_
Definition: reader.h:229
String document_
Definition: reader.h:223
std::stack< Value * > Nodes
Definition: reader.h:220
const Char * Location
Definition: reader.h:40
char Char
Definition: reader.h:39
Nodes nodes_
Definition: reader.h:221
std::deque< ErrorInfo > Errors
Definition: reader.h:185
Errors errors_
Definition: reader.h:222
Features features_
Definition: reader.h:230
Represents a JSON value.
Definition: value.h:196
#define JSON_API
Definition: config.h:50
CommentPlacement
Definition: value.h:120
bool JSON_API parseFromStream(CharReader::Factory const &, IStream &, Value *root, String *errs)
Definition: json_reader.cpp:1905
JSON_API IStream & operator>>(IStream &, Value &)
Read from 'sin' into 'root'.
Definition: json_reader.cpp:1916
std::istream IStream
Definition: config.h:113
std::basic_string< char, std::char_traits< char >, Allocator< char > > String
Definition: config.h:110
JSON (JavaScript Object Notation).
Definition: allocator.h:14
An error tagged with where in the JSON text it was encountered.
Definition: reader.h:47
ptrdiff_t offset_limit
Definition: reader.h:49
ptrdiff_t offset_start
Definition: reader.h:48