Loading...
Searching...
No Matches
RapidJSON error handling

## Classes

struct | rapidjson::ParseResult
| Result of parsing (wraps ParseErrorCode) More...

## Macros

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

## Typedefs

typedef const RAPIDJSON_ERROR_CHARTYPE *(* | rapidjson::GetParseErrorFunc) (ParseErrorCode)
| Function pointer type of GetParseError().

## Enumerations

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. More...
enum | rapidjson::PointerParseErrorCode { rapidjson::kPointerParseErrorNone = 0 , rapidjson::kPointerParseErrorTokenMustBeginWithSolidus , rapidjson::kPointerParseErrorInvalidEscape , rapidjson::kPointerParseErrorInvalidPercentEncoding , rapidjson::kPointerParseErrorCharacterMustPercentEncode }
| Error code of parsing. More...

## Functions

const RAPIDJSON_ERROR_CHARTYPE * | rapidjson::GetParseError_En (ParseErrorCode parseErrorCode)
| Maps error code of parsing into error message.

## Detailed Description

## Class Documentation

## rapidjson::ParseResult

struct rapidjson::ParseResult

Result of parsing (wraps ParseErrorCode)

Document doc;
ParseResult ok = doc.Parse("[42]");
if (!ok) {
fprintf(stderr, "JSON parse error: %s (%u)",
GetParseError_En(ok.Code()), ok.Offset());
exit(EXIT_FAILURE);
}
rapidjson::GenericDocument
A document for parsing JSON text as DOM.
Definition document.h:2024
rapidjson::GenericDocument::Parse
GenericDocument & Parse(const typename SourceEncoding::Ch *str)
Parse JSON text from a read-only string (with Encoding conversion)
Definition document.h:2221
rapidjson::ParseResult::Code
ParseErrorCode Code() const
Get the error code.
Definition error.h:114
rapidjson::ParseResult::Offset
size_t Offset() const
Get the error offset, if IsError(), 0 otherwise.
Definition error.h:116
rapidjson::GetParseError_En
const RAPIDJSON_ERROR_CHARTYPE * GetParseError_En(ParseErrorCode parseErrorCode)
Maps error code of parsing into error message.
Definition en.h:36
rapidjson::ParseResult
Result of parsing (wraps ParseErrorCode)
Definition error.h:106

See also
  GenericReader::Parse, GenericDocument::Parse

### Public Member Functions

| 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 bool () const
| 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
void | Clear ()
| Reset error code.
void | Set (ParseErrorCode code, size_t offset=0)
| Update error code and offset.

### Friends

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

## Macro Definition Documentation

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

Parameters

  parseErrorCode | rapidjson::ParseErrorCode of the error
  offset | position of the error in JSON input (`size_t`)

Invokes RAPIDJSON_PARSE_ERROR_NORETURN and stops the parsing.

See also
  RAPIDJSON_PARSE_ERROR_NORETURN

## RAPIDJSON_PARSE_ERROR_NORETURN

#define RAPIDJSON_PARSE_ERROR_NORETURN | ( |  | parseErrorCode,
|  |  | offset
| ) |  |

Value:
RAPIDJSON_MULTILINEMACRO_BEGIN \
RAPIDJSON_ASSERT(!HasParseError()); /* Error can only be assigned once */ \
SetParseError(parseErrorCode, offset); \
RAPIDJSON_MULTILINEMACRO_END

Macro to indicate a parse error.

Parameters

  parseErrorCode | rapidjson::ParseErrorCode of the error
  offset | position 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) {}
};
#include "rapidjson/reader.h"
error.h
rapidjson::ParseErrorCode
ParseErrorCode
Error code of parsing.
Definition error.h:64
reader.h

See also
  RAPIDJSON_PARSE_ERROR, rapidjson::GenericReader::Parse

## Typedef Documentation

## GetParseErrorFunc

typedef const 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());
rapidjson::GetParseErrorFunc
const RAPIDJSON_ERROR_CHARTYPE *(* GetParseErrorFunc)(ParseErrorCode)
Function pointer type of GetParseError().
Definition error.h:147
RAPIDJSON_ERROR_CHARTYPE
#define RAPIDJSON_ERROR_CHARTYPE
Character type of error messages.
Definition error.h:39

## Enumeration Type Documentation

## ParseErrorCode

enum rapidjson::ParseErrorCode

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.

## PointerParseErrorCode

enum rapidjson::PointerParseErrorCode

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.

## Function Documentation

## GetParseError_En()

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

inline

Maps error code of parsing into error message.

Parameters

  parseErrorCode | Error 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.