Classes | Macros | Typedefs | Enumerations | Functions
RapidJSON error handling

Classes

struct  ParseResult
 Result of parsing (wraps ParseErrorCode) More...
 

Macros

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

Typedefs

typedef const RAPIDJSON_ERROR_CHARTYPE *(* GetParseErrorFunc) (ParseErrorCode)
 Function pointer type of GetParseError(). More...
 

Enumerations

enum  ParseErrorCode {
  kParseErrorNone = 0, kParseErrorDocumentEmpty, kParseErrorDocumentRootNotSingular, kParseErrorValueInvalid,
  kParseErrorObjectMissName, kParseErrorObjectMissColon, kParseErrorObjectMissCommaOrCurlyBracket, kParseErrorArrayMissCommaOrSquareBracket,
  kParseErrorStringUnicodeEscapeInvalidHex, kParseErrorStringUnicodeSurrogateInvalid, kParseErrorStringEscapeInvalid, kParseErrorStringMissQuotationMark,
  kParseErrorStringInvalidEncoding, kParseErrorNumberTooBig, kParseErrorNumberMissFraction, kParseErrorNumberMissExponent,
  kParseErrorTermination, kParseErrorUnspecificSyntaxError
}
 Error code of parsing. More...
 
enum  PointerParseErrorCode {
  kPointerParseErrorNone = 0, kPointerParseErrorTokenMustBeginWithSolidus, kPointerParseErrorInvalidEscape, kPointerParseErrorInvalidPercentEncoding,
  kPointerParseErrorCharacterMustPercentEncode
}
 Error code of parsing. More...
 

Functions

RAPIDJSON_NAMESPACE_BEGIN const RAPIDJSON_ERROR_CHARTYPEGetParseError_En (ParseErrorCode parseErrorCode)
 Maps error code of parsing into error message. More...
 

Detailed Description

Macro Definition Documentation

#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.

Definition at line 39 of file error.h.

#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.

Definition at line 52 of file error.h.

#define RAPIDJSON_PARSE_ERROR (   parseErrorCode,
  offset 
)

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

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

Invokes RAPIDJSON_PARSE_ERROR_NORETURN and stops the parsing.

See also
RAPIDJSON_PARSE_ERROR_NORETURN

Definition at line 118 of file reader.h.

#define RAPIDJSON_PARSE_ERROR_NORETURN (   parseErrorCode,
  offset 
)
Value:
RAPIDJSON_ASSERT(!HasParseError()); /* Error can only be assigned once */ \
SetParseError(parseErrorCode, offset); \
RAPIDJSON_MULTILINEMACRO_END
#define RAPIDJSON_ASSERT(x)
Assertion.
Definition: rapidjson.h:406

Macro to indicate a parse error.

Parameters
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:

1 #define RAPIDJSON_PARSE_ERROR_NORETURN(parseErrorCode,offset) \
2  throw ParseException(parseErrorCode, #parseErrorCode, offset)
3 
4 #include <stdexcept> // std::runtime_error
5 #include "rapidjson/error/error.h" // rapidjson::ParseResult
6 
7 struct ParseException : std::runtime_error, rapidjson::ParseResult {
8  ParseException(rapidjson::ParseErrorCode code, const char* msg, size_t offset)
9  : std::runtime_error(msg), ParseResult(code, offset) {}
10 };
11 
12 #include "rapidjson/reader.h"
See also
RAPIDJSON_PARSE_ERROR, rapidjson::GenericReader::Parse

Definition at line 99 of file reader.h.

Typedef Documentation

typedef const RAPIDJSON_ERROR_CHARTYPE*(* 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.:

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

Definition at line 153 of file error.h.

Enumeration Type Documentation

Error code of parsing.

See also
GenericReader::Parse, GenericReader::GetParseErrorCode
Enumerator
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.

Definition at line 64 of file error.h.

64  {
65  kParseErrorNone = 0, //!< No error.
66 
67  kParseErrorDocumentEmpty, //!< The document is empty.
68  kParseErrorDocumentRootNotSingular, //!< The document root must not follow by other values.
69 
70  kParseErrorValueInvalid, //!< Invalid value.
71 
72  kParseErrorObjectMissName, //!< Missing a name for object member.
73  kParseErrorObjectMissColon, //!< Missing a colon after a name of object member.
74  kParseErrorObjectMissCommaOrCurlyBracket, //!< Missing a comma or '}' after an object member.
75 
76  kParseErrorArrayMissCommaOrSquareBracket, //!< Missing a comma or ']' after an array element.
77 
78  kParseErrorStringUnicodeEscapeInvalidHex, //!< Incorrect hex digit after \\u escape in string.
79  kParseErrorStringUnicodeSurrogateInvalid, //!< The surrogate pair in string is invalid.
80  kParseErrorStringEscapeInvalid, //!< Invalid escape character in string.
81  kParseErrorStringMissQuotationMark, //!< Missing a closing quotation mark in string.
82  kParseErrorStringInvalidEncoding, //!< Invalid encoding in string.
83 
84  kParseErrorNumberTooBig, //!< Number too big to be stored in double.
85  kParseErrorNumberMissFraction, //!< Miss fraction part in number.
86  kParseErrorNumberMissExponent, //!< Miss exponent in number.
87 
88  kParseErrorTermination, //!< Parsing was terminated.
89  kParseErrorUnspecificSyntaxError //!< Unspecific syntax error.
90 };
No error.
Definition: error.h:65
Invalid value.
Definition: error.h:70
Parsing was terminated.
Definition: error.h:88
Missing a comma or &#39;}&#39; after an object member.
Definition: error.h:74
The document is empty.
Definition: error.h:67
Missing a comma or &#39;]&#39; after an array element.
Definition: error.h:76
Missing a name for object member.
Definition: error.h:72
Number too big to be stored in double.
Definition: error.h:84
Miss exponent in number.
Definition: error.h:86
Invalid escape character in string.
Definition: error.h:80
Invalid encoding in string.
Definition: error.h:82
Miss fraction part in number.
Definition: error.h:85
Incorrect hex digit after \u escape in string.
Definition: error.h:78
Unspecific syntax error.
Definition: error.h:89
The surrogate pair in string is invalid.
Definition: error.h:79
Missing a colon after a name of object member.
Definition: error.h:73
Missing a closing quotation mark in string.
Definition: error.h:81
The document root must not follow by other values.
Definition: error.h:68

Error code of parsing.

See also
GenericPointer::GenericPointer, GenericPointer::GetParseErrorCode
Enumerator
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.

Definition at line 37 of file pointer.h.

37  {
38  kPointerParseErrorNone = 0, //!< The parse is successful
39 
40  kPointerParseErrorTokenMustBeginWithSolidus, //!< A token must begin with a '/'
41  kPointerParseErrorInvalidEscape, //!< Invalid escape
42  kPointerParseErrorInvalidPercentEncoding, //!< Invalid percent encoding in URI fragment
43  kPointerParseErrorCharacterMustPercentEncode //!< A character must percent encoded in URI fragment
44 };
Invalid percent encoding in URI fragment.
Definition: pointer.h:42
A token must begin with a &#39;/&#39;.
Definition: pointer.h:40
The parse is successful.
Definition: pointer.h:38
A character must percent encoded in URI fragment.
Definition: pointer.h:43

Function Documentation

RAPIDJSON_NAMESPACE_BEGIN const RAPIDJSON_ERROR_CHARTYPE* GetParseError_En ( ParseErrorCode  parseErrorCode)
inline

Maps error code of parsing into error message.

Parameters
parseErrorCodeError code obtained in parsing.
Returns
the error message.
Note
User can make a copy of this function for localization. Using switch-case is safer for future modification of error codes.

Definition at line 36 of file en.h.

36  {
37  switch (parseErrorCode) {
38  case kParseErrorNone: return RAPIDJSON_ERROR_STRING("No error.");
39 
40  case kParseErrorDocumentEmpty: return RAPIDJSON_ERROR_STRING("The document is empty.");
41  case kParseErrorDocumentRootNotSingular: return RAPIDJSON_ERROR_STRING("The document root must not be followed by other values.");
42 
43  case kParseErrorValueInvalid: return RAPIDJSON_ERROR_STRING("Invalid value.");
44 
45  case kParseErrorObjectMissName: return RAPIDJSON_ERROR_STRING("Missing a name for object member.");
46  case kParseErrorObjectMissColon: return RAPIDJSON_ERROR_STRING("Missing a colon after a name of object member.");
47  case kParseErrorObjectMissCommaOrCurlyBracket: return RAPIDJSON_ERROR_STRING("Missing a comma or '}' after an object member.");
48 
49  case kParseErrorArrayMissCommaOrSquareBracket: return RAPIDJSON_ERROR_STRING("Missing a comma or ']' after an array element.");
50 
51  case kParseErrorStringUnicodeEscapeInvalidHex: return RAPIDJSON_ERROR_STRING("Incorrect hex digit after \\u escape in string.");
52  case kParseErrorStringUnicodeSurrogateInvalid: return RAPIDJSON_ERROR_STRING("The surrogate pair in string is invalid.");
53  case kParseErrorStringEscapeInvalid: return RAPIDJSON_ERROR_STRING("Invalid escape character in string.");
54  case kParseErrorStringMissQuotationMark: return RAPIDJSON_ERROR_STRING("Missing a closing quotation mark in string.");
55  case kParseErrorStringInvalidEncoding: return RAPIDJSON_ERROR_STRING("Invalid encoding in string.");
56 
57  case kParseErrorNumberTooBig: return RAPIDJSON_ERROR_STRING("Number too big to be stored in double.");
58  case kParseErrorNumberMissFraction: return RAPIDJSON_ERROR_STRING("Miss fraction part in number.");
59  case kParseErrorNumberMissExponent: return RAPIDJSON_ERROR_STRING("Miss exponent in number.");
60 
61  case kParseErrorTermination: return RAPIDJSON_ERROR_STRING("Terminate parsing due to Handler error.");
62  case kParseErrorUnspecificSyntaxError: return RAPIDJSON_ERROR_STRING("Unspecific syntax error.");
63 
64  default: return RAPIDJSON_ERROR_STRING("Unknown error.");
65  }
66 }
No error.
Definition: error.h:65
Invalid value.
Definition: error.h:70
Parsing was terminated.
Definition: error.h:88
Missing a comma or &#39;}&#39; after an object member.
Definition: error.h:74
The document is empty.
Definition: error.h:67
Missing a comma or &#39;]&#39; after an array element.
Definition: error.h:76
Missing a name for object member.
Definition: error.h:72
Number too big to be stored in double.
Definition: error.h:84
#define RAPIDJSON_ERROR_STRING(x)
Macro for converting string literial to RAPIDJSON_ERROR_CHARTYPE[].
Definition: error.h:52
Miss exponent in number.
Definition: error.h:86
Invalid escape character in string.
Definition: error.h:80
Invalid encoding in string.
Definition: error.h:82
Miss fraction part in number.
Definition: error.h:85
Incorrect hex digit after \u escape in string.
Definition: error.h:78
Unspecific syntax error.
Definition: error.h:89
The surrogate pair in string is invalid.
Definition: error.h:79
Missing a colon after a name of object member.
Definition: error.h:73
Missing a closing quotation mark in string.
Definition: error.h:81
The document root must not follow by other values.
Definition: error.h:68