![]() |
|
|||
File indexing completed on 2025-02-22 10:52:23
0001 // Tencent is pleased to support the open source community by making RapidJSON available. 0002 // 0003 // Copyright (C) 2015 THL A29 Limited, a Tencent company, and Milo Yip. 0004 // 0005 // Licensed under the MIT License (the "License"); you may not use this file except 0006 // in compliance with the License. You may obtain a copy of the License at 0007 // 0008 // http://opensource.org/licenses/MIT 0009 // 0010 // Unless required by applicable law or agreed to in writing, software distributed 0011 // under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR 0012 // CONDITIONS OF ANY KIND, either express or implied. See the License for the 0013 // specific language governing permissions and limitations under the License. 0014 0015 #ifndef RAPIDJSON_ERROR_ERROR_H_ 0016 #define RAPIDJSON_ERROR_ERROR_H_ 0017 0018 #include "../rapidjson.h" 0019 0020 #ifdef __clang__ 0021 RAPIDJSON_DIAG_PUSH 0022 RAPIDJSON_DIAG_OFF(padded) 0023 #endif 0024 0025 /*! \file error.h */ 0026 0027 /*! \defgroup RAPIDJSON_ERRORS RapidJSON error handling */ 0028 0029 /////////////////////////////////////////////////////////////////////////////// 0030 // RAPIDJSON_ERROR_CHARTYPE 0031 0032 //! Character type of error messages. 0033 /*! \ingroup RAPIDJSON_ERRORS 0034 The default character type is \c char. 0035 On Windows, user can define this macro as \c TCHAR for supporting both 0036 unicode/non-unicode settings. 0037 */ 0038 #ifndef RAPIDJSON_ERROR_CHARTYPE 0039 #define RAPIDJSON_ERROR_CHARTYPE char 0040 #endif 0041 0042 /////////////////////////////////////////////////////////////////////////////// 0043 // RAPIDJSON_ERROR_STRING 0044 0045 //! Macro for converting string literal to \ref RAPIDJSON_ERROR_CHARTYPE[]. 0046 /*! \ingroup RAPIDJSON_ERRORS 0047 By default this conversion macro does nothing. 0048 On Windows, user can define this macro as \c _T(x) for supporting both 0049 unicode/non-unicode settings. 0050 */ 0051 #ifndef RAPIDJSON_ERROR_STRING 0052 #define RAPIDJSON_ERROR_STRING(x) x 0053 #endif 0054 0055 RAPIDJSON_NAMESPACE_BEGIN 0056 0057 /////////////////////////////////////////////////////////////////////////////// 0058 // ParseErrorCode 0059 0060 //! Error code of parsing. 0061 /*! \ingroup RAPIDJSON_ERRORS 0062 \see GenericReader::Parse, GenericReader::GetParseErrorCode 0063 */ 0064 enum ParseErrorCode { 0065 kParseErrorNone = 0, //!< No error. 0066 0067 kParseErrorDocumentEmpty, //!< The document is empty. 0068 kParseErrorDocumentRootNotSingular, //!< The document root must not follow by other values. 0069 0070 kParseErrorValueInvalid, //!< Invalid value. 0071 0072 kParseErrorObjectMissName, //!< Missing a name for object member. 0073 kParseErrorObjectMissColon, //!< Missing a colon after a name of object member. 0074 kParseErrorObjectMissCommaOrCurlyBracket, //!< Missing a comma or '}' after an object member. 0075 0076 kParseErrorArrayMissCommaOrSquareBracket, //!< Missing a comma or ']' after an array element. 0077 0078 kParseErrorStringUnicodeEscapeInvalidHex, //!< Incorrect hex digit after \\u escape in string. 0079 kParseErrorStringUnicodeSurrogateInvalid, //!< The surrogate pair in string is invalid. 0080 kParseErrorStringEscapeInvalid, //!< Invalid escape character in string. 0081 kParseErrorStringMissQuotationMark, //!< Missing a closing quotation mark in string. 0082 kParseErrorStringInvalidEncoding, //!< Invalid encoding in string. 0083 0084 kParseErrorNumberTooBig, //!< Number too big to be stored in double. 0085 kParseErrorNumberMissFraction, //!< Miss fraction part in number. 0086 kParseErrorNumberMissExponent, //!< Miss exponent in number. 0087 0088 kParseErrorTermination, //!< Parsing was terminated. 0089 kParseErrorUnspecificSyntaxError //!< Unspecific syntax error. 0090 }; 0091 0092 //! Result of parsing (wraps ParseErrorCode) 0093 /*! 0094 \ingroup RAPIDJSON_ERRORS 0095 \code 0096 Document doc; 0097 ParseResult ok = doc.Parse("[42]"); 0098 if (!ok) { 0099 fprintf(stderr, "JSON parse error: %s (%u)", 0100 GetParseError_En(ok.Code()), ok.Offset()); 0101 exit(EXIT_FAILURE); 0102 } 0103 \endcode 0104 \see GenericReader::Parse, GenericDocument::Parse 0105 */ 0106 struct ParseResult { 0107 //!! Unspecified boolean type 0108 typedef bool (ParseResult::*BooleanType)() const; 0109 public: 0110 //! Default constructor, no error. 0111 ParseResult() : code_(kParseErrorNone), offset_(0) {} 0112 //! Constructor to set an error. 0113 ParseResult(ParseErrorCode code, size_t offset) : code_(code), offset_(offset) {} 0114 0115 //! Get the error code. 0116 ParseErrorCode Code() const { return code_; } 0117 //! Get the error offset, if \ref IsError(), 0 otherwise. 0118 size_t Offset() const { return offset_; } 0119 0120 //! Explicit conversion to \c bool, returns \c true, iff !\ref IsError(). 0121 operator BooleanType() const { return !IsError() ? &ParseResult::IsError : NULL; } 0122 //! Whether the result is an error. 0123 bool IsError() const { return code_ != kParseErrorNone; } 0124 0125 bool operator==(const ParseResult& that) const { return code_ == that.code_; } 0126 bool operator==(ParseErrorCode code) const { return code_ == code; } 0127 friend bool operator==(ParseErrorCode code, const ParseResult & err) { return code == err.code_; } 0128 0129 bool operator!=(const ParseResult& that) const { return !(*this == that); } 0130 bool operator!=(ParseErrorCode code) const { return !(*this == code); } 0131 friend bool operator!=(ParseErrorCode code, const ParseResult & err) { return err != code; } 0132 0133 //! Reset error code. 0134 void Clear() { Set(kParseErrorNone); } 0135 //! Update error code and offset. 0136 void Set(ParseErrorCode code, size_t offset = 0) { code_ = code; offset_ = offset; } 0137 0138 private: 0139 ParseErrorCode code_; 0140 size_t offset_; 0141 }; 0142 0143 //! Function pointer type of GetParseError(). 0144 /*! \ingroup RAPIDJSON_ERRORS 0145 0146 This is the prototype for \c GetParseError_X(), where \c X is a locale. 0147 User can dynamically change locale in runtime, e.g.: 0148 \code 0149 GetParseErrorFunc GetParseError = GetParseError_En; // or whatever 0150 const RAPIDJSON_ERROR_CHARTYPE* s = GetParseError(document.GetParseErrorCode()); 0151 \endcode 0152 */ 0153 typedef const RAPIDJSON_ERROR_CHARTYPE* (*GetParseErrorFunc)(ParseErrorCode); 0154 0155 /////////////////////////////////////////////////////////////////////////////// 0156 // ValidateErrorCode 0157 0158 //! Error codes when validating. 0159 /*! \ingroup RAPIDJSON_ERRORS 0160 \see GenericSchemaValidator 0161 */ 0162 enum ValidateErrorCode { 0163 kValidateErrors = -1, //!< Top level error code when kValidateContinueOnErrorsFlag set. 0164 kValidateErrorNone = 0, //!< No error. 0165 0166 kValidateErrorMultipleOf, //!< Number is not a multiple of the 'multipleOf' value. 0167 kValidateErrorMaximum, //!< Number is greater than the 'maximum' value. 0168 kValidateErrorExclusiveMaximum, //!< Number is greater than or equal to the 'maximum' value. 0169 kValidateErrorMinimum, //!< Number is less than the 'minimum' value. 0170 kValidateErrorExclusiveMinimum, //!< Number is less than or equal to the 'minimum' value. 0171 0172 kValidateErrorMaxLength, //!< String is longer than the 'maxLength' value. 0173 kValidateErrorMinLength, //!< String is longer than the 'maxLength' value. 0174 kValidateErrorPattern, //!< String does not match the 'pattern' regular expression. 0175 0176 kValidateErrorMaxItems, //!< Array is longer than the 'maxItems' value. 0177 kValidateErrorMinItems, //!< Array is shorter than the 'minItems' value. 0178 kValidateErrorUniqueItems, //!< Array has duplicate items but 'uniqueItems' is true. 0179 kValidateErrorAdditionalItems, //!< Array has additional items that are not allowed by the schema. 0180 0181 kValidateErrorMaxProperties, //!< Object has more members than 'maxProperties' value. 0182 kValidateErrorMinProperties, //!< Object has less members than 'minProperties' value. 0183 kValidateErrorRequired, //!< Object is missing one or more members required by the schema. 0184 kValidateErrorAdditionalProperties, //!< Object has additional members that are not allowed by the schema. 0185 kValidateErrorPatternProperties, //!< See other errors. 0186 kValidateErrorDependencies, //!< Object has missing property or schema dependencies. 0187 0188 kValidateErrorEnum, //!< Property has a value that is not one of its allowed enumerated values. 0189 kValidateErrorType, //!< Property has a type that is not allowed by the schema. 0190 0191 kValidateErrorOneOf, //!< Property did not match any of the sub-schemas specified by 'oneOf'. 0192 kValidateErrorOneOfMatch, //!< Property matched more than one of the sub-schemas specified by 'oneOf'. 0193 kValidateErrorAllOf, //!< Property did not match all of the sub-schemas specified by 'allOf'. 0194 kValidateErrorAnyOf, //!< Property did not match any of the sub-schemas specified by 'anyOf'. 0195 kValidateErrorNot, //!< Property matched the sub-schema specified by 'not'. 0196 0197 kValidateErrorReadOnly, //!< Property is read-only but has been provided when validation is for writing 0198 kValidateErrorWriteOnly //!< Property is write-only but has been provided when validation is for reading 0199 }; 0200 0201 //! Function pointer type of GetValidateError(). 0202 /*! \ingroup RAPIDJSON_ERRORS 0203 0204 This is the prototype for \c GetValidateError_X(), where \c X is a locale. 0205 User can dynamically change locale in runtime, e.g.: 0206 \code 0207 GetValidateErrorFunc GetValidateError = GetValidateError_En; // or whatever 0208 const RAPIDJSON_ERROR_CHARTYPE* s = GetValidateError(validator.GetInvalidSchemaCode()); 0209 \endcode 0210 */ 0211 typedef const RAPIDJSON_ERROR_CHARTYPE* (*GetValidateErrorFunc)(ValidateErrorCode); 0212 0213 /////////////////////////////////////////////////////////////////////////////// 0214 // SchemaErrorCode 0215 0216 //! Error codes when validating. 0217 /*! \ingroup RAPIDJSON_ERRORS 0218 \see GenericSchemaValidator 0219 */ 0220 enum SchemaErrorCode { 0221 kSchemaErrorNone = 0, //!< No error. 0222 0223 kSchemaErrorStartUnknown, //!< Pointer to start of schema does not resolve to a location in the document 0224 kSchemaErrorRefPlainName, //!< $ref fragment must be a JSON pointer 0225 kSchemaErrorRefInvalid, //!< $ref must not be an empty string 0226 kSchemaErrorRefPointerInvalid, //!< $ref fragment is not a valid JSON pointer at offset 0227 kSchemaErrorRefUnknown, //!< $ref does not resolve to a location in the target document 0228 kSchemaErrorRefCyclical, //!< $ref is cyclical 0229 kSchemaErrorRefNoRemoteProvider, //!< $ref is remote but there is no remote provider 0230 kSchemaErrorRefNoRemoteSchema, //!< $ref is remote but the remote provider did not return a schema 0231 kSchemaErrorRegexInvalid, //!< Invalid regular expression in 'pattern' or 'patternProperties' 0232 kSchemaErrorSpecUnknown, //!< JSON schema draft or OpenAPI version is not recognized 0233 kSchemaErrorSpecUnsupported, //!< JSON schema draft or OpenAPI version is not supported 0234 kSchemaErrorSpecIllegal, //!< Both JSON schema draft and OpenAPI version found in document 0235 kSchemaErrorReadOnlyAndWriteOnly //!< Property must not be both 'readOnly' and 'writeOnly' 0236 }; 0237 0238 //! Function pointer type of GetSchemaError(). 0239 /*! \ingroup RAPIDJSON_ERRORS 0240 0241 This is the prototype for \c GetSchemaError_X(), where \c X is a locale. 0242 User can dynamically change locale in runtime, e.g.: 0243 \code 0244 GetSchemaErrorFunc GetSchemaError = GetSchemaError_En; // or whatever 0245 const RAPIDJSON_ERROR_CHARTYPE* s = GetSchemaError(validator.GetInvalidSchemaCode()); 0246 \endcode 0247 */ 0248 typedef const RAPIDJSON_ERROR_CHARTYPE* (*GetSchemaErrorFunc)(SchemaErrorCode); 0249 0250 /////////////////////////////////////////////////////////////////////////////// 0251 // PointerParseErrorCode 0252 0253 //! Error code of JSON pointer parsing. 0254 /*! \ingroup RAPIDJSON_ERRORS 0255 \see GenericPointer::GenericPointer, GenericPointer::GetParseErrorCode 0256 */ 0257 enum PointerParseErrorCode { 0258 kPointerParseErrorNone = 0, //!< The parse is successful 0259 0260 kPointerParseErrorTokenMustBeginWithSolidus, //!< A token must begin with a '/' 0261 kPointerParseErrorInvalidEscape, //!< Invalid escape 0262 kPointerParseErrorInvalidPercentEncoding, //!< Invalid percent encoding in URI fragment 0263 kPointerParseErrorCharacterMustPercentEncode //!< A character must percent encoded in URI fragment 0264 }; 0265 0266 //! Function pointer type of GetPointerParseError(). 0267 /*! \ingroup RAPIDJSON_ERRORS 0268 0269 This is the prototype for \c GetPointerParseError_X(), where \c X is a locale. 0270 User can dynamically change locale in runtime, e.g.: 0271 \code 0272 GetPointerParseErrorFunc GetPointerParseError = GetPointerParseError_En; // or whatever 0273 const RAPIDJSON_ERROR_CHARTYPE* s = GetPointerParseError(pointer.GetParseErrorCode()); 0274 \endcode 0275 */ 0276 typedef const RAPIDJSON_ERROR_CHARTYPE* (*GetPointerParseErrorFunc)(PointerParseErrorCode); 0277 0278 0279 RAPIDJSON_NAMESPACE_END 0280 0281 #ifdef __clang__ 0282 RAPIDJSON_DIAG_POP 0283 #endif 0284 0285 #endif // RAPIDJSON_ERROR_ERROR_H_
[ Source navigation ] | [ Diff markup ] | [ Identifier search ] | [ general search ] |
This page was automatically generated by the 2.3.7 LXR engine. The LXR team |
![]() ![]() |