全部  命名空间 文件 函数 变量 类型定义 枚举 枚举值 友元 宏定义  
error.h
浏览该文件的文档.
1 // Tencent is pleased to support the open source community by making RapidJSON available.
2 //
3 // Copyright (C) 2015 THL A29 Limited, a Tencent company, and Milo Yip. All rights reserved.
4 //
5 // Licensed under the MIT License (the "License"); you may not use this file except
6 // in compliance with the License. You may obtain a copy of the License at
7 //
8 // http://opensource.org/licenses/MIT
9 //
10 // Unless required by applicable law or agreed to in writing, software distributed
11 // under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR
12 // CONDITIONS OF ANY KIND, either express or implied. See the License for the
13 // specific language governing permissions and limitations under the License.
14 
15 #ifndef RAPIDJSON_ERROR_ERROR_H__
16 #define RAPIDJSON_ERROR_ERROR_H__
17 
18 #include "../rapidjson.h"
19 
20 /*! \file error.h */
21 
22 /*! \defgroup RAPIDJSON_ERRORS RapidJSON error handling */
23 
24 ///////////////////////////////////////////////////////////////////////////////
25 // RAPIDJSON_ERROR_CHARTYPE
26 
27 //! Character type of error messages.
28 /*! \ingroup RAPIDJSON_ERRORS
29  The default character type is \c char.
30  On Windows, user can define this macro as \c TCHAR for supporting both
31  unicode/non-unicode settings.
32 */
33 #ifndef RAPIDJSON_ERROR_CHARTYPE
34 #define RAPIDJSON_ERROR_CHARTYPE char
35 #endif
36 
37 ///////////////////////////////////////////////////////////////////////////////
38 // RAPIDJSON_ERROR_STRING
39 
40 //! Macro for converting string literial to \ref RAPIDJSON_ERROR_CHARTYPE[].
41 /*! \ingroup RAPIDJSON_ERRORS
42  By default this conversion macro does nothing.
43  On Windows, user can define this macro as \c _T(x) for supporting both
44  unicode/non-unicode settings.
45 */
46 #ifndef RAPIDJSON_ERROR_STRING
47 #define RAPIDJSON_ERROR_STRING(x) x
48 #endif
49 
50 RAPIDJSON_NAMESPACE_BEGIN
51 
52 ///////////////////////////////////////////////////////////////////////////////
53 // ParseErrorCode
54 
55 //! Error code of parsing.
56 /*! \ingroup RAPIDJSON_ERRORS
57  \see GenericReader::Parse, GenericReader::GetParseErrorCode
58 */
60  kParseErrorNone = 0, //!< No error.
61 
62  kParseErrorDocumentEmpty, //!< The document is empty.
63  kParseErrorDocumentRootNotSingular, //!< The document root must not follow by other values.
64 
65  kParseErrorValueInvalid, //!< Invalid value.
66 
67  kParseErrorObjectMissName, //!< Missing a name for object member.
68  kParseErrorObjectMissColon, //!< Missing a colon after a name of object member.
69  kParseErrorObjectMissCommaOrCurlyBracket, //!< Missing a comma or '}' after an object member.
70 
71  kParseErrorArrayMissCommaOrSquareBracket, //!< Missing a comma or ']' after an array element.
72 
73  kParseErrorStringUnicodeEscapeInvalidHex, //!< Incorrect hex digit after \\u escape in string.
74  kParseErrorStringUnicodeSurrogateInvalid, //!< The surrogate pair in string is invalid.
75  kParseErrorStringEscapeInvalid, //!< Invalid escape character in string.
76  kParseErrorStringMissQuotationMark, //!< Missing a closing quotation mark in string.
77  kParseErrorStringInvalidEncoding, //!< Invalid encoding in string.
78 
79  kParseErrorNumberTooBig, //!< Number too big to be stored in double.
80  kParseErrorNumberMissFraction, //!< Miss fraction part in number.
81  kParseErrorNumberMissExponent, //!< Miss exponent in number.
82 
83  kParseErrorTermination, //!< Parsing was terminated.
84  kParseErrorUnspecificSyntaxError //!< Unspecific syntax error.
85 };
86 
87 //! Result of parsing (wraps ParseErrorCode)
88 /*!
89  \ingroup RAPIDJSON_ERRORS
90  \code
91  Document doc;
92  ParseResult ok = doc.Parse("[42]");
93  if (!ok) {
94  fprintf(stderr, "JSON parse error: %s (%u)",
95  GetParseError_En(ok.Code()), ok.Offset());
96  exit(EXIT_FAILURE);
97  }
98  \endcode
99  \see GenericReader::Parse, GenericDocument::Parse
100 */
101 struct ParseResult {
102 
103  //! Default constructor, no error.
104  ParseResult() : code_(kParseErrorNone), offset_(0) {}
105  //! Constructor to set an error.
106  ParseResult(ParseErrorCode code, size_t offset) : code_(code), offset_(offset) {}
107 
108  //! Get the error code.
109  ParseErrorCode Code() const { return code_; }
110  //! Get the error offset, if \ref IsError(), 0 otherwise.
111  size_t Offset() const { return offset_; }
112 
113  //! Conversion to \c bool, returns \c true, iff !\ref IsError().
114  operator bool() const { return !IsError(); }
115  //! Whether the result is an error.
116  bool IsError() const { return code_ != kParseErrorNone; }
117 
118  bool operator==(const ParseResult& that) const { return code_ == that.code_; }
119  bool operator==(ParseErrorCode code) const { return code_ == code; }
120  friend bool operator==(ParseErrorCode code, const ParseResult & err) { return code == err.code_; }
121 
122  //! Reset error code.
123  void Clear() { Set(kParseErrorNone); }
124  //! Update error code and offset.
125  void Set(ParseErrorCode code, size_t offset = 0) { code_ = code; offset_ = offset; }
126 
127 private:
128  ParseErrorCode code_;
129  size_t offset_;
130 };
131 
132 //! Function pointer type of GetParseError().
133 /*! \ingroup RAPIDJSON_ERRORS
134 
135  This is the prototype for \c GetParseError_X(), where \c X is a locale.
136  User can dynamically change locale in runtime, e.g.:
137 \code
138  GetParseErrorFunc GetParseError = GetParseError_En; // or whatever
139  const RAPIDJSON_ERROR_CHARTYPE* s = GetParseError(document.GetParseErrorCode());
140 \endcode
141 */
142 typedef const RAPIDJSON_ERROR_CHARTYPE* (*GetParseErrorFunc)(ParseErrorCode);
143 
144 RAPIDJSON_NAMESPACE_END
145 
146 #endif // RAPIDJSON_ERROR_ERROR_H__
Invalid value.
Definition: error.h:65
The surrogate pair in string is invalid.
Definition: error.h:74
ParseResult(ParseErrorCode code, size_t offset)
Constructor to set an error.
Definition: error.h:106
Missing a colon after a name of object member.
Definition: error.h:68
Incorrect hex digit after \u escape in string.
Definition: error.h:73
Miss fraction part in number.
Definition: error.h:80
No error.
Definition: error.h:60
ParseErrorCode
Error code of parsing.
Definition: error.h:59
void Clear()
Reset error code.
Definition: error.h:123
Missing a comma or ']' after an array element.
Definition: error.h:71
The document root must not follow by other values.
Definition: error.h:63
bool IsError() const
Whether the result is an error.
Definition: error.h:116
Unspecific syntax error.
Definition: error.h:84
Missing a closing quotation mark in string.
Definition: error.h:76
Invalid escape character in string.
Definition: error.h:75
#define RAPIDJSON_ERROR_CHARTYPE
Character type of error messages.
Definition: error.h:34
Result of parsing (wraps ParseErrorCode)
Definition: error.h:101
Missing a name for object member.
Definition: error.h:67
ParseErrorCode Code() const
Get the error code.
Definition: error.h:109
Parsing was terminated.
Definition: error.h:83
Number too big to be stored in double.
Definition: error.h:79
Miss exponent in number.
Definition: error.h:81
ParseResult()
Default constructor, no error.
Definition: error.h:104
The document is empty.
Definition: error.h:62
Missing a comma or '}' after an object member.
Definition: error.h:69
Invalid encoding in string.
Definition: error.h:77
size_t Offset() const
Get the error offset, if IsError(), 0 otherwise.
Definition: error.h:111
void Set(ParseErrorCode code, size_t offset=0)
Update error code and offset.
Definition: error.h:125