RapidJSON error handling

struct  rapidjson::ParseResult
 Result of parsing (wraps ParseErrorCode) 更多...
 

宏定义

#define RAPIDJSON_ERROR_CHARTYPE   char
 Character type of error messages. 更多...
 
#define RAPIDJSON_ERROR_STRING(x)   x
 Macro for converting string literial to RAPIDJSON_ERROR_CHARTYPE[]. 更多...
 
#define RAPIDJSON_PARSE_ERROR_NORETURN(parseErrorCode, offset)
 Macro to indicate a parse error. 更多...
 
#define RAPIDJSON_PARSE_ERROR(parseErrorCode, offset)
 (Internal) macro to indicate and handle a parse error. 更多...
 

枚举

enum  rapidjson::ParseErrorCode {
  rapidjson::kParseErrorNone = 0, rapidjson::kParseErrorDocumentEmpty, rapidjson::kParseErrorDocumentRootNotSingular, rapidjson::kParseErrorValueInvalid,
  rapidjson::kParseErrorObjectMissName, rapidjson::kParseErrorObjectMissColon, rapidjson::kParseErrorObjectMissCommaOrCurlyBracket, rapidjson::kParseErrorArrayMissCommaOrSquareBracket,
  rapidjson::kParseErrorStringUnicodeEscapeInvalidHex, rapidjson::kParseErrorStringUnicodeSurrogateInvalid, rapidjson::kParseErrorStringEscapeInvalid, rapidjson::kParseErrorStringMissQuotationMark,
  rapidjson::kParseErrorStringInvalidEncoding, rapidjson::kParseErrorNumberTooBig, rapidjson::kParseErrorNumberMissFraction, rapidjson::kParseErrorNumberMissExponent,
  rapidjson::kParseErrorTermination, rapidjson::kParseErrorUnspecificSyntaxError
}
 Error code of parsing. 更多...
 
enum  rapidjson::ValidateErrorCode {
  rapidjson::kValidateErrors = -1, rapidjson::kValidateErrorNone = 0, rapidjson::kValidateErrorMultipleOf, rapidjson::kValidateErrorMaximum,
  rapidjson::kValidateErrorExclusiveMaximum, rapidjson::kValidateErrorMinimum, rapidjson::kValidateErrorExclusiveMinimum, rapidjson::kValidateErrorMaxLength,
  rapidjson::kValidateErrorMinLength, rapidjson::kValidateErrorPattern, rapidjson::kValidateErrorMaxItems, rapidjson::kValidateErrorMinItems,
  rapidjson::kValidateErrorUniqueItems, rapidjson::kValidateErrorAdditionalItems, rapidjson::kValidateErrorMaxProperties, rapidjson::kValidateErrorMinProperties,
  rapidjson::kValidateErrorRequired, rapidjson::kValidateErrorAdditionalProperties, rapidjson::kValidateErrorPatternProperties, rapidjson::kValidateErrorDependencies,
  rapidjson::kValidateErrorEnum, rapidjson::kValidateErrorType, rapidjson::kValidateErrorOneOf, rapidjson::kValidateErrorOneOfMatch,
  rapidjson::kValidateErrorAllOf, rapidjson::kValidateErrorAnyOf, rapidjson::kValidateErrorNot
}
 Error codes when validating. 更多...
 
enum  rapidjson::PointerParseErrorCode {
  rapidjson::kPointerParseErrorNone = 0, rapidjson::kPointerParseErrorTokenMustBeginWithSolidus, rapidjson::kPointerParseErrorInvalidEscape, rapidjson::kPointerParseErrorInvalidPercentEncoding,
  rapidjson::kPointerParseErrorCharacterMustPercentEncode
}
 Error code of parsing. 更多...
 

函数

const RAPIDJSON_ERROR_CHARTYPErapidjson::GetParseError_En (ParseErrorCode parseErrorCode)
 Maps error code of parsing into error message. 更多...
 
const RAPIDJSON_ERROR_CHARTYPErapidjson::GetValidateError_En (ValidateErrorCode validateErrorCode)
 Maps error code of validation into error message. 更多...
 

变量

const typedef RAPIDJSON_ERROR_CHARTYPE *(* rapidjson::GetParseErrorFunc )(ParseErrorCode)
 Function pointer type of GetParseError(). 更多...
 
const typedef RAPIDJSON_ERROR_CHARTYPE *(* rapidjson::GetValidateErrorFunc )(ValidateErrorCode)
 Function pointer type of GetValidateError(). 更多...
 

详细描述


类说明

◆ rapidjson::ParseResult

struct rapidjson::ParseResult

Result of parsing (wraps ParseErrorCode)

ParseResult ok = doc.Parse("[42]");
if (!ok) {
fprintf(stderr, "JSON parse error: %s (%u)",
GetParseError_En(ok.Code()), ok.Offset());
exit(EXIT_FAILURE);
}
参见
GenericReader::Parse, GenericDocument::Parse

Public 类型

typedef bool(ParseResult::* BooleanType) () const
 ! Unspecified boolean type
 

Public 成员函数

 ParseResult ()
 Default constructor, no error.
 
 ParseResult (ParseErrorCode code, size_t offset)
 Constructor to set an error.
 
ParseErrorCode Code () const
 Get the error code.
 
size_t Offset () const
 Get the error offset, if IsError(), 0 otherwise.
 
 operator BooleanType () const
 Explicit conversion to bool, returns true, iff !IsError().
 
bool IsError () const
 Whether the result is an error.
 
bool operator== (const ParseResult &that) const
 
bool operator== (ParseErrorCode code) const
 
bool operator!= (const ParseResult &that) const
 
bool operator!= (ParseErrorCode code) const
 
void Clear ()
 Reset error code.
 
void Set (ParseErrorCode code, size_t offset=0)
 Update error code and offset.
 

友元

bool operator== (ParseErrorCode code, const ParseResult &err)
 
bool operator!= (ParseErrorCode code, const ParseResult &err)
 

宏定义说明

◆ RAPIDJSON_ERROR_CHARTYPE

#define RAPIDJSON_ERROR_CHARTYPE   char

Character type of error messages.

The default character type is char. On Windows, user can define this macro as TCHAR for supporting both unicode/non-unicode settings.

◆ RAPIDJSON_ERROR_STRING

#define RAPIDJSON_ERROR_STRING (   x)    x

Macro for converting string literial to RAPIDJSON_ERROR_CHARTYPE[].

By default this conversion macro does nothing. On Windows, user can define this macro as _T(x) for supporting both unicode/non-unicode settings.

◆ RAPIDJSON_PARSE_ERROR

#define RAPIDJSON_PARSE_ERROR (   parseErrorCode,
  offset 
)

(Internal) macro to indicate and handle a parse error.

参数
parseErrorCoderapidjson::ParseErrorCode of the error
offsetposition of the error in JSON input (size_t)

Invokes RAPIDJSON_PARSE_ERROR_NORETURN and stops the parsing.

参见
RAPIDJSON_PARSE_ERROR_NORETURN

◆ RAPIDJSON_PARSE_ERROR_NORETURN

#define RAPIDJSON_PARSE_ERROR_NORETURN (   parseErrorCode,
  offset 
)
值:
RAPIDJSON_ASSERT(!HasParseError()); /* Error can only be assigned once */ \
SetParseError(parseErrorCode, offset); \
RAPIDJSON_MULTILINEMACRO_END

Macro to indicate a parse error.

参数
parseErrorCoderapidjson::ParseErrorCode of the error
offsetposition of the error in JSON input (size_t)

This macros can be used as a customization point for the internal error handling mechanism of RapidJSON.

A common usage model is to throw an exception instead of requiring the caller to explicitly check the rapidjson::GenericReader::Parse's return value:

#define RAPIDJSON_PARSE_ERROR_NORETURN(parseErrorCode,offset) \
throw ParseException(parseErrorCode, #parseErrorCode, offset)
#include <stdexcept> // std::runtime_error
#include "rapidjson/error/error.h" // rapidjson::ParseResult
struct ParseException : std::runtime_error, rapidjson::ParseResult {
ParseException(rapidjson::ParseErrorCode code, const char* msg, size_t offset)
: std::runtime_error(msg), ParseResult(code, offset) {}
};
参见
RAPIDJSON_PARSE_ERROR, rapidjson::GenericReader::Parse

枚举类型说明

◆ ParseErrorCode

Error code of parsing.

参见
GenericReader::Parse, GenericReader::GetParseErrorCode
枚举值
kParseErrorNone 

No error.

kParseErrorDocumentEmpty 

The document is empty.

kParseErrorDocumentRootNotSingular 

The document root must not follow by other values.

kParseErrorValueInvalid 

Invalid value.

kParseErrorObjectMissName 

Missing a name for object member.

kParseErrorObjectMissColon 

Missing a colon after a name of object member.

kParseErrorObjectMissCommaOrCurlyBracket 

Missing a comma or '}' after an object member.

kParseErrorArrayMissCommaOrSquareBracket 

Missing a comma or ']' after an array element.

kParseErrorStringUnicodeEscapeInvalidHex 

Incorrect hex digit after \u escape in string.

kParseErrorStringUnicodeSurrogateInvalid 

The surrogate pair in string is invalid.

kParseErrorStringEscapeInvalid 

Invalid escape character in string.

kParseErrorStringMissQuotationMark 

Missing a closing quotation mark in string.

kParseErrorStringInvalidEncoding 

Invalid encoding in string.

kParseErrorNumberTooBig 

Number too big to be stored in double.

kParseErrorNumberMissFraction 

Miss fraction part in number.

kParseErrorNumberMissExponent 

Miss exponent in number.

kParseErrorTermination 

Parsing was terminated.

kParseErrorUnspecificSyntaxError 

Unspecific syntax error.

◆ PointerParseErrorCode

Error code of parsing.

参见
GenericPointer::GenericPointer, GenericPointer::GetParseErrorCode
枚举值
kPointerParseErrorNone 

The parse is successful

kPointerParseErrorTokenMustBeginWithSolidus 

A token must begin with a '/'

kPointerParseErrorInvalidEscape 

Invalid escape

kPointerParseErrorInvalidPercentEncoding 

Invalid percent encoding in URI fragment

kPointerParseErrorCharacterMustPercentEncode 

A character must percent encoded in URI fragment

◆ ValidateErrorCode

Error codes when validating.

参见
GenericSchemaValidator
枚举值
kValidateErrors 

Top level error code when kValidateContinueOnErrorsFlag set.

kValidateErrorNone 

No error.

kValidateErrorMultipleOf 

Number is not a multiple of the 'multipleOf' value.

kValidateErrorMaximum 

Number is greater than the 'maximum' value.

kValidateErrorExclusiveMaximum 

Number is greater than or equal to the 'maximum' value.

kValidateErrorMinimum 

Number is less than the 'minimum' value.

kValidateErrorExclusiveMinimum 

Number is less than or equal to the 'minimum' value.

kValidateErrorMaxLength 

String is longer than the 'maxLength' value.

kValidateErrorMinLength 

String is longer than the 'maxLength' value.

kValidateErrorPattern 

String does not match the 'pattern' regular expression.

kValidateErrorMaxItems 

Array is longer than the 'maxItems' value.

kValidateErrorMinItems 

Array is shorter than the 'minItems' value.

kValidateErrorUniqueItems 

Array has duplicate items but 'uniqueItems' is true.

kValidateErrorAdditionalItems 

Array has additional items that are not allowed by the schema.

kValidateErrorMaxProperties 

Object has more members than 'maxProperties' value.

kValidateErrorMinProperties 

Object has less members than 'minProperties' value.

kValidateErrorRequired 

Object is missing one or more members required by the schema.

kValidateErrorAdditionalProperties 

Object has additional members that are not allowed by the schema.

kValidateErrorPatternProperties 

See other errors.

kValidateErrorDependencies 

Object has missing property or schema dependencies.

kValidateErrorEnum 

Property has a value that is not one of its allowed enumerated values

kValidateErrorType 

Property has a type that is not allowed by the schema..

kValidateErrorOneOf 

Property did not match any of the sub-schemas specified by 'oneOf'.

kValidateErrorOneOfMatch 

Property matched more than one of the sub-schemas specified by 'oneOf'.

kValidateErrorAllOf 

Property did not match all of the sub-schemas specified by 'allOf'.

kValidateErrorAnyOf 

Property did not match any of the sub-schemas specified by 'anyOf'.

kValidateErrorNot 

Property matched the sub-schema specified by 'not'.

函数说明

◆ GetParseError_En()

const RAPIDJSON_ERROR_CHARTYPE* rapidjson::GetParseError_En ( ParseErrorCode  parseErrorCode)
inline

Maps error code of parsing into error message.

参数
parseErrorCodeError code obtained in parsing.
返回
the error message.
注解
User can make a copy of this function for localization. Using switch-case is safer for future modification of error codes.

◆ GetValidateError_En()

const RAPIDJSON_ERROR_CHARTYPE* rapidjson::GetValidateError_En ( ValidateErrorCode  validateErrorCode)
inline

Maps error code of validation into error message.

参数
validateErrorCodeError code obtained from validator.
返回
the error message.
注解
User can make a copy of this function for localization. Using switch-case is safer for future modification of error codes.

变量说明

◆ GetParseErrorFunc

const typedef RAPIDJSON_ERROR_CHARTYPE*(* rapidjson::GetParseErrorFunc) (ParseErrorCode)

Function pointer type of GetParseError().

This is the prototype for GetParseError_X(), where X is a locale. User can dynamically change locale in runtime, e.g.:

GetParseErrorFunc GetParseError = GetParseError_En; // or whatever
const RAPIDJSON_ERROR_CHARTYPE* s = GetParseError(document.GetParseErrorCode());

◆ GetValidateErrorFunc

const typedef RAPIDJSON_ERROR_CHARTYPE*(* rapidjson::GetValidateErrorFunc) (ValidateErrorCode)

Function pointer type of GetValidateError().

This is the prototype for GetValidateError_X(), where X is a locale. User can dynamically change locale in runtime, e.g.:

GetValidateErrorFunc GetValidateError = GetValidateError_En; // or whatever
const RAPIDJSON_ERROR_CHARTYPE* s = GetValidateError(validator.GetInvalidSchemaCode());
rapidjson::ParseResult
Result of parsing (wraps ParseErrorCode)
Definition: error.h:106
rapidjson::GetValidateError_En
const RAPIDJSON_ERROR_CHARTYPE * GetValidateError_En(ValidateErrorCode validateErrorCode)
Maps error code of validation into error message.
Definition: en.h:76
rapidjson::GetValidateErrorFunc
const typedef RAPIDJSON_ERROR_CHARTYPE *(* GetValidateErrorFunc)(ValidateErrorCode)
Function pointer type of GetValidateError().
Definition: error.h:208
rapidjson::ParseErrorCode
ParseErrorCode
Error code of parsing.
Definition: error.h:64
RAPIDJSON_ASSERT
#define RAPIDJSON_ASSERT(x)
Assertion.
Definition: rapidjson.h:437
error.h
reader.h
RAPIDJSON_ERROR_CHARTYPE
#define RAPIDJSON_ERROR_CHARTYPE
Character type of error messages.
Definition: error.h:39
rapidjson::GetParseErrorFunc
const typedef RAPIDJSON_ERROR_CHARTYPE *(* GetParseErrorFunc)(ParseErrorCode)
Function pointer type of GetParseError().
Definition: error.h:153
rapidjson::Document
GenericDocument< UTF8<> > Document
GenericDocument with UTF8 encoding
Definition: document.h:2873
rapidjson::ParseResult::ParseResult
ParseResult()
Default constructor, no error.
Definition: error.h:111
rapidjson::GetParseError_En
const RAPIDJSON_ERROR_CHARTYPE * GetParseError_En(ParseErrorCode parseErrorCode)
Maps error code of parsing into error message.
Definition: en.h:36