All
Classes
Namespaces
Files
Functions
Variables
Typedefs
Enumerations
Enumerator
Friends
Macros
Groups
Pages
error.h
Go to the documentation of this file.
1
// Copyright (C) 2011 Milo Yip
2
//
3
// Permission is hereby granted, free of charge, to any person obtaining a copy
4
// of this software and associated documentation files (the "Software"), to deal
5
// in the Software without restriction, including without limitation the rights
6
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
7
// copies of the Software, and to permit persons to whom the Software is
8
// furnished to do so, subject to the following conditions:
9
//
10
// The above copyright notice and this permission notice shall be included in
11
// all copies or substantial portions of the Software.
12
//
13
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
14
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
15
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
16
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
17
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
18
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
19
// THE SOFTWARE.
20
21
#ifndef RAPIDJSON_ERROR_ERROR_H__
22
#define RAPIDJSON_ERROR_ERROR_H__
23
24
/*! \file error.h */
25
26
/*! \defgroup RAPIDJSON_ERRORS RapidJSON error handling */
27
28
///////////////////////////////////////////////////////////////////////////////
29
// RAPIDJSON_ERROR_CHARTYPE
30
31
//! Character type of error messages.
32
/*! \ingroup RAPIDJSON_ERRORS
33
The default character type is \c char.
34
On Windows, user can define this macro as \c TCHAR for supporting both
35
unicode/non-unicode settings.
36
*/
37
#ifndef RAPIDJSON_ERROR_CHARTYPE
38
#define RAPIDJSON_ERROR_CHARTYPE char
39
#endif
40
41
///////////////////////////////////////////////////////////////////////////////
42
// RAPIDJSON_ERROR_STRING
43
44
//! Macro for converting string literial to \ref RAPIDJSON_ERROR_CHARTYPE[].
45
/*! \ingroup RAPIDJSON_ERRORS
46
By default this conversion macro does nothing.
47
On Windows, user can define this macro as \c _T(x) for supporting both
48
unicode/non-unicode settings.
49
*/
50
#ifndef RAPIDJSON_ERROR_STRING
51
#define RAPIDJSON_ERROR_STRING(x) x
52
#endif
53
54
namespace
rapidjson {
55
56
///////////////////////////////////////////////////////////////////////////////
57
// ParseErrorCode
58
59
//! Error code of parsing.
60
/*! \ingroup RAPIDJSON_ERRORS
61
\see GenericReader::Parse, GenericReader::GetParseErrorCode
62
*/
63
enum
ParseErrorCode
{
64
kParseErrorNone
= 0,
//!< No error.
65
66
kParseErrorDocumentEmpty
,
//!< The document is empty.
67
kParseErrorDocumentRootNotSingular
,
//!< The document root must not follow by other values.
68
69
kParseErrorValueInvalid
,
//!< Invalid value.
70
71
kParseErrorObjectMissName
,
//!< Missing a name for object member.
72
kParseErrorObjectMissColon
,
//!< Missing a colon after a name of object member.
73
kParseErrorObjectMissCommaOrCurlyBracket
,
//!< Missing a comma or '}' after an object member.
74
75
kParseErrorArrayMissCommaOrSquareBracket
,
//!< Missing a comma or ']' after an array element.
76
77
kParseErrorStringUnicodeEscapeInvalidHex
,
//!< Incorrect hex digit after \\u escape in string.
78
kParseErrorStringUnicodeSurrogateInvalid
,
//!< The surrogate pair in string is invalid.
79
kParseErrorStringEscapeInvalid
,
//!< Invalid escape character in string.
80
kParseErrorStringMissQuotationMark
,
//!< Missing a closing quotation mark in string.
81
kParseErrorStringInvalidEncoding
,
//!< Invalid encoding in string.
82
83
kParseErrorNumberTooBig
,
//!< Number too big to be stored in double.
84
kParseErrorNumberMissFraction
,
//!< Miss fraction part in number.
85
kParseErrorNumberMissExponent
,
//!< Miss exponent in number.
86
87
kParseErrorTermination
,
//!< Parsing was terminated.
88
kParseErrorUnspecificSyntaxError
,
//!< Unspecific syntax error.
89
};
90
91
//! Result of parsing (wraps ParseErrorCode)
92
/*!
93
\ingroup RAPIDJSON_ERRORS
94
\code
95
Document doc;
96
ParseResult ok = doc.Parse("[42]");
97
if (!ok) {
98
fprintf(stderr, "JSON parse error: %s (%u)",
99
GetParseError_En(ok.Code()), ok.Offset());
100
exit(EXIT_FAILURE);
101
}
102
\endcode
103
\see GenericReader::Parse, GenericDocument::Parse
104
*/
105
struct
ParseResult
{
106
107
//! Default constructor, no error.
108
ParseResult
() : code_(
kParseErrorNone
), offset_(0) {}
109
//! Constructor to set an error.
110
ParseResult
(
ParseErrorCode
code,
size_t
offset) : code_(code), offset_(offset) {}
111
112
//! Get the error code.
113
ParseErrorCode
Code
()
const
{
return
code_; }
114
//! Get the error offset, if \ref IsError(), 0 otherwise.
115
size_t
Offset
()
const
{
return
offset_; }
116
117
//! Conversion to \c bool, returns \c true, iff !\ref IsError().
118
operator
bool()
const
{
return
!
IsError
(); }
119
//! Whether the result is an error.
120
bool
IsError
()
const
{
return
code_ !=
kParseErrorNone
; }
121
122
bool
operator==(
const
ParseResult
& that)
const
{
return
code_ == that.code_; }
123
bool
operator==(
ParseErrorCode
code)
const
{
return
code_ == code; }
124
friend
bool
operator==(
ParseErrorCode
code,
const
ParseResult
& err) {
return
code == err.code_; }
125
126
//! Reset error code.
127
void
Clear
() {
Set
(
kParseErrorNone
); }
128
//! Update error code and offset.
129
void
Set
(
ParseErrorCode
code,
size_t
offset = 0) { code_ = code; offset_ = offset; }
130
131
private
:
132
ParseErrorCode
code_;
133
size_t
offset_;
134
};
135
136
//! Function pointer type of GetParseError().
137
/*! \ingroup RAPIDJSON_ERRORS
138
139
This is the prototype for \c GetParseError_X(), where \c X is a locale.
140
User can dynamically change locale in runtime, e.g.:
141
\code
142
GetParseErrorFunc GetParseError = GetParseError_En; // or whatever
143
const RAPIDJSON_ERROR_CHARTYPE* s = GetParseError(document.GetParseErrorCode());
144
\endcode
145
*/
146
typedef
const
RAPIDJSON_ERROR_CHARTYPE
* (*GetParseErrorFunc)(
ParseErrorCode
);
147
148
}
// namespace rapidjson
149
150
#endif // RAPIDJSON_ERROR_ERROR_H__
include
rapidjson
error
error.h