🔨 user-defined exception 302

Also moved all exception classes into the detail namespace and
introduced them via “using” into basic_json.
This commit is contained in:
Niels Lohmann 2017-03-06 19:34:44 +01:00
parent 38c2e20ce8
commit a186106bde
No known key found for this signature in database
GPG key ID: 7F3CEA63AE251B69
3 changed files with 530 additions and 462 deletions

View file

@ -110,6 +110,187 @@ This namespace collects some functions that could not be defined inside the
*/
namespace detail
{
////////////////
// exceptions //
////////////////
/*!
@brief general exception of the @ref basic_json class
Extension of std::exception objects with a member @a id for exception ids.
@since version 3.0.0
*/
class exception : public std::exception
{
public:
/// create exception with id an explanatory string
exception(int id_, const std::string& ename, const std::string& what_arg_)
: id(id_),
what_arg("[json.exception." + ename + "." + std::to_string(id_) + "] " + what_arg_)
{}
/// returns the explanatory string
virtual const char* what() const noexcept
{
return what_arg.c_str();
}
/// the id of the exception
const int id;
private:
/// the explanatory string
const std::string what_arg;
};
/*!
@brief exception indicating a parse error
This excpetion is thrown by the library when a parse error occurs. Parse
errors can occur during the deserialization of JSON text as well as when
using JSON Patch.
Member @a byte holds the byte index of the last read character in the input
file.
@note For an input with n bytes, 1 is the index of the first character
and n+1 is the index of the terminating null byte or the end of
file.
Exceptions have ids 1xx.
name / id | example massage | description
------------------------------ | --------------- | -------------------------
json.exception.[parse_error](@ref parse_error).101 | "parse error at 2: unexpected end of input; expected string literal" | This error indicates a syntax error while deserializing a JSON text. The error message describes that an unexpected token (character) was encountered, and the member @ref parse_error::byte indicates the error position.
json.exception.[parse_error](@ref parse_error).102 | "parse error at 14: missing or wrong low surrogate" | JSON uses the `\uxxxx` format to describe Unicode characters. Code points above above 0xFFFF are split into two `\uxxxx` entries ("surrogate pairs"). This error indicates that the surrogate pair is incomplete or contains an invalid code point.
json.exception.[parse_error](@ref parse_error).103 | "parse error: code points above 0x10FFFF are invalid" | Unicode supports code points up to 0x10FFFF. Code points above 0x10FFFF are invalid.
json.exception.[parse_error](@ref parse_error).104 | "parse error: JSON patch must be an array of objects" | [RFC 6902](https://tools.ietf.org/html/rfc6902) requires a JSON Patch document to be a JSON document that represents an array of objects.
json.exception.[parse_error](@ref parse_error).105 | "parse error: operation must have string member 'op'" | An operation of a JSON Patch document must contain exactly one "op" member, whose value indicates the operation to perform. Its value must be one of "add", "remove", "replace", "move", "copy", or "test"; other values are errors.
json.exception.[parse_error](@ref parse_error).106 | "parse error: array index '01' must not begin with '0'" | An array index in a JSON Pointer ([RFC 6901](https://tools.ietf.org/html/rfc6901)) may be `0` or any number wihtout a leading `0`.
json.exception.[parse_error](@ref parse_error).107 | "parse error: JSON pointer must be empty or begin with '/' - was: 'foo'" | A JSON Pointer must be a Unicode string containing a sequence of zero or more reference tokens, each prefixed by a `/` character.
json.exception.[parse_error](@ref parse_error).108 | "parse error: escape character '~' must be followed with '0' or '1'" | In a JSON Pointer, only `~0` and `~1` are valid escape sequences.
json.exception.[parse_error](@ref parse_error).109 | "parse error: array index 'one' is not a number" | A JSON Pointer array index must be a number.
@since version 3.0.0
*/
class parse_error : public exception
{
public:
/*!
@brief create a parse error exception
@param[in] id_ the id of the exception
@param[in] byte_ the byte index where the error occured (or 0 if
the position cannot be determined)
@param[in] what_arg_ the explanatory string
*/
parse_error(int id_, size_t byte_, const std::string& what_arg_)
: exception(id_, "parse_error", "parse error" +
(byte_ != 0 ? (" at " + std::to_string(byte_)) : "") +
": " + what_arg_),
byte(byte_)
{}
/*!
@brief byte index of the parse error
The byte index of the last read character in the input file.
@note For an input with n bytes, 1 is the index of the first character
and n+1 is the index of the terminating null byte or the end of
file.
*/
const size_t byte;
};
/*!
@brief exception indicating errors with iterators
Exceptions have ids 2xx.
name / id | example massage | description
------------------------------ | --------------- | -------------------------
json.exception.[invalid_iterator](@ref invalid_iterator).201 | "iterators are not compatible" | The iterators passed to constructor @ref basic_json(InputIT first, InputIT last) are not compatible, meaning they do not belong to the same container. Therefore, the range (@a first, @a last) is invalid.
json.exception.[invalid_iterator](@ref invalid_iterator).202 | "iterator does not fit current value" | In an erase or insert function, the passed iterator @a pos does not belong to the JSON value for which the function was called. It hence does not define a valid position for the deletion/insertion.
json.exception.[invalid_iterator](@ref invalid_iterator).203 | "iterators do not fit current value" | Either iterator passed to function @ref erase(IteratorType first, IteratorType last) does not belong to the JSON value from which values shall be erased. It hence does not define a valid range to delete values from.
json.exception.[invalid_iterator](@ref invalid_iterator).204 | "iterators out of range" | When an iterator range for a primitive type (number, boolean, or string) is passed to a constructor or an erase function, this range has to be exactly (@ref begin(), @ref end()), because this is the only way the single stored value is expressed. All other ranges are invalid.
json.exception.[invalid_iterator](@ref invalid_iterator).205 | "iterator out of range" | When an iterator for a primitive type (number, boolean, or string) is passed to an erase function, the iterator has to be the @ref begin() iterator, because it is the only way to address the stored value. All other iterators are invalid.
json.exception.[invalid_iterator](@ref invalid_iterator).206 | "cannot construct with iterators from null" | The iterators passed to constructor @ref basic_json(InputIT first, InputIT last) belong to a JSON null value and hence to not define a valid range.
json.exception.[invalid_iterator](@ref invalid_iterator).207 | "cannot use key() for non-object iterators" | The key() member function can only be used on iterators belonging to a JSON object, because other types do not have a concept of a key.
json.exception.[invalid_iterator](@ref invalid_iterator).208 | "cannot use operator[] for object iterators" | The operator[] to specify a concrete offset cannot be used on iterators belonging to a JSON object, because JSON objects are unordered.
json.exception.[invalid_iterator](@ref invalid_iterator).209 | "cannot use offsets with object iterators" | The offset operators (+, -, +=, -=) cannot be used on iterators belonging to a JSON object, because JSON objects are unordered.
json.exception.[invalid_iterator](@ref invalid_iterator).210 | "iterators do not fit" | The iterator range passed to the insert function are not compatible, meaning they do not belong to the same container. Therefore, the range (@a first, @a last) is invalid.
json.exception.[invalid_iterator](@ref invalid_iterator).211 | "passed iterators may not belong to container" | The iterator range passed to the insert function must not be a subrange of the container to insert to.
json.exception.[invalid_iterator](@ref invalid_iterator).212 | "cannot compare iterators of different containers" | When two iterators are compared, they must belong to the same container.
json.exception.[invalid_iterator](@ref invalid_iterator).213 | "cannot compare order of object iterators" | The order of object iterators cannot be compated, because JSON objects are unordered.
json.exception.[invalid_iterator](@ref invalid_iterator).214 | "cannot get value" | Cannot get value for iterator: Either the iterator belongs to a null value or it is an iterator to a primitive type (number, boolean, or string), but the iterator is different to @ref begin().
@since version 3.0.0
*/
class invalid_iterator : public exception
{
public:
invalid_iterator(int id_, const std::string& what_arg_)
: exception(id_, "invalid_iterator", what_arg_)
{}
};
/*!
@brief exception indicating executing a member function with a wrong type
Exceptions have ids 3xx.
name / id | example massage | description
------------------------------ | --------------- | -------------------------
json.exception.[type_error](@ref type_error).301 | "cannot create object from initializer list" | To create an object from an initializer list, the initializer list must consist only of a list of pairs whose first element is a string. When this constraint is violated, an array is created instead.
json.exception.[type_error](@ref type_error).302 | "type must be object, but is array" | During implicit or explicit value conversion, the JSON type must be compatible to the target type. For instance, a JSON string can only be converted into string types, but not into numbers or boolean types.
json.exception.[type_error](@ref type_error).303 | "incompatible ReferenceType for get_ref, actual type is object" | To retrieve a reference to a value stored in a @ref basic_json object with @ref get_ref, the type of the reference must match the value type. For instance, for a JSON array, the @a ReferenceType must be @ref array_t&.
json.exception.[type_error](@ref type_error).304 | "cannot use at() with string" | The @ref at() member functions can only be executed for certain JSON types.
json.exception.[type_error](@ref type_error).305 | "cannot use operator[] with string" | The @ref operator[] member functions can only be executed for certain JSON types.
json.exception.[type_error](@ref type_error).306 | "cannot use value() with string" | The @ref value() member functions can only be executed for certain JSON types.
json.exception.[type_error](@ref type_error).307 | "cannot use erase() with string" | The @ref erase() member functions can only be executed for certain JSON types.
json.exception.[type_error](@ref type_error).308 | "cannot use push_back() with string" | The @ref push_back() and @ref operator+= member functions can only be executed for certain JSON types.
json.exception.[type_error](@ref type_error).309 | "cannot use insert() with" | The @ref insert() member functions can only be executed for certain JSON types.
json.exception.[type_error](@ref type_error).310 | "cannot use swap() with number" | The @ref swap() member functions can only be executed for certain JSON types.
json.exception.[type_error](@ref type_error).311 | "cannot use emplace_back() with string" | The @ref emplace_back() member function can only be executed for certain JSON types.
json.exception.[type_error](@ref type_error).313 | "invalid value to unflatten" | The @ref unflatten function converts an object whose keys are JSON Pointers back into an arbitrary nested JSON value. The JSON Pointers must not overlap, because then the resulting value would not be well defined.
json.exception.[type_error](@ref type_error).314 | "only objects can be unflattened" | The @ref unflatten function only works for an object whose keys are JSON Pointers.
json.exception.[type_error](@ref type_error).315 | "values in object must be primitive" | The @ref unflatten function only works for an object whose keys are JSON Pointers and whose values are primitive.
@since version 3.0.0
*/
class type_error : public exception
{
public:
type_error(int id_, const std::string& what_arg_)
: exception(id_, "type_error", what_arg_)
{}
};
/*!
@brief exception indicating access out of the defined range
Exceptions have ids 4xx.
name / id | example massage | description
------------------------------ | --------------- | -------------------------
json.exception.[out_of_range](@ref out_of_range).401 | "array index 3 is out of range" | The provided array index @a i is larger than @a size-1.
json.exception.[out_of_range](@ref out_of_range).402 | "array index '-' (3) is out of range" | The special array index `-` in a JSON Pointer never describes a valid element of the array, but the index past the end.
json.exception.[out_of_range](@ref out_of_range).403 | "key 'foo' not found" | The provided key was not found in the JSON object.
json.exception.[out_of_range](@ref out_of_range).404 | "unresolved reference token 'foo'" | A reference token in a JSON Pointer could not be resolved.
json.exception.[out_of_range](@ref out_of_range).405 | "JSON pointer has no parent" | The JSON Patch operations 'remove' and 'add' can not be applied to the root element of the JSON value.
@since version 3.0.0
*/
class out_of_range : public exception
{
public:
out_of_range(int id_, const std::string& what_arg_)
: exception(id_, "out_of_range", what_arg_)
{}
};
///////////////////////////
// JSON type enumeration //
///////////////////////////
@ -617,8 +798,7 @@ void get_arithmetic_value(const BasicJsonType& j, ArithmeticType& val)
}
default:
{
JSON_THROW(
std::domain_error("type must be number, but is " + j.type_name()));
JSON_THROW(type_error(302, "type must be number, but is " + j.type_name()));
}
}
}
@ -628,7 +808,7 @@ void from_json(const BasicJsonType& j, typename BasicJsonType::boolean_t& b)
{
if (not j.is_boolean())
{
JSON_THROW(std::domain_error("type must be boolean, but is " + j.type_name()));
JSON_THROW(type_error(302, "type must be boolean, but is " + j.type_name()));
}
b = *j.template get_ptr<const typename BasicJsonType::boolean_t*>();
}
@ -638,7 +818,7 @@ void from_json(const BasicJsonType& j, typename BasicJsonType::string_t& s)
{
if (not j.is_string())
{
JSON_THROW(std::domain_error("type must be string, but is " + j.type_name()));
JSON_THROW(type_error(302, "type must be string, but is " + j.type_name()));
}
s = *j.template get_ptr<const typename BasicJsonType::string_t*>();
}
@ -675,7 +855,7 @@ void from_json(const BasicJsonType& j, typename BasicJsonType::array_t& arr)
{
if (not j.is_array())
{
JSON_THROW(std::domain_error("type must be array, but is " + j.type_name()));
JSON_THROW(type_error(302, "type must be array, but is " + j.type_name()));
}
arr = *j.template get_ptr<const typename BasicJsonType::array_t*>();
}
@ -688,13 +868,13 @@ void from_json(const BasicJsonType& j, std::forward_list<T, Allocator>& l)
// (except when it's null.. ?)
if (j.is_null())
{
JSON_THROW(std::domain_error("type must be array, but is " + j.type_name()));
JSON_THROW(type_error(302, "type must be array, but is " + j.type_name()));
}
if (not std::is_same<T, BasicJsonType>::value)
{
if (not j.is_array())
{
JSON_THROW(std::domain_error("type must be array, but is " + j.type_name()));
JSON_THROW(type_error(302, "type must be array, but is " + j.type_name()));
}
}
for (auto it = j.rbegin(), end = j.rend(); it != end; ++it)
@ -744,7 +924,7 @@ void from_json(const BasicJsonType& j, CompatibleArrayType& arr)
{
if (j.is_null())
{
JSON_THROW(std::domain_error("type must be array, but is " + j.type_name()));
JSON_THROW(type_error(302, "type must be array, but is " + j.type_name()));
}
// when T == BasicJsonType, do not check if value_t is correct
@ -752,7 +932,7 @@ void from_json(const BasicJsonType& j, CompatibleArrayType& arr)
{
if (not j.is_array())
{
JSON_THROW(std::domain_error("type must be array, but is " + j.type_name()));
JSON_THROW(type_error(302, "type must be array, but is " + j.type_name()));
}
}
from_json_array_impl(j, arr, priority_tag<1> {});
@ -764,7 +944,7 @@ void from_json(const BasicJsonType& j, CompatibleObjectType& obj)
{
if (not j.is_object())
{
JSON_THROW(std::domain_error("type must be object, but is " + j.type_name()));
JSON_THROW(type_error(302, "type must be object, but is " + j.type_name()));
}
auto inner_object = j.template get_ptr<const typename BasicJsonType::object_t*>();
@ -814,7 +994,7 @@ void from_json(const BasicJsonType& j, ArithmeticType& val)
}
default:
{
JSON_THROW(std::domain_error("type must be number, but is " + j.type_name()));
JSON_THROW(type_error(302, "type must be number, but is " + j.type_name()));
}
}
}
@ -1046,6 +1226,33 @@ class basic_json
template<typename T, typename SFINAE>
using json_serializer = JSONSerializer<T, SFINAE>;
////////////////
// exceptions //
////////////////
/// @name exceptions
/// Classes to implement user-defined exceptions.
/// @{
/*
name / id | example massage | description
------------------------------ | --------------- | -------------------------
json.exception.other.500 | "unsuccessful" | A JSON Patch operation 'test' failed.
*/
/// @copydoc detail::parse_error
using parse_error = detail::parse_error;
/// @copydoc detail::invalid_iterator
using invalid_iterator = detail::invalid_iterator;
/// @copydoc detail::type_error
using type_error = detail::type_error;
/// @copydoc detail::out_of_range
using out_of_range = detail::out_of_range;
/// @}
/////////////////////
// container types //
/////////////////////
@ -1088,179 +1295,6 @@ class basic_json
/// @}
public:
////////////////
// exceptions //
////////////////
/// @name exceptions
/// Classes to implement user-defined exceptions.
/// @{
/*!
@brief general exception of the @ref basic_json class
Extension of std::exception objects with a member @a id for exception ids.
name / id | example massage | description
------------------------------ | --------------- | -------------------------
json.exception.[parse_error](@ref parse_error).101 | "parse error at 2: unexpected end of input; expected string literal" | This error indicates a syntax error while deserializing a JSON text. The error message describes that an unexpected token (character) was encountered, and the member @ref parse_error::byte indicates the error position.
json.exception.[parse_error](@ref parse_error).102 | "parse error at 14: missing or wrong low surrogate" | JSON uses the `\uxxxx` format to describe Unicode characters. Code points above above 0xFFFF are split into two `\uxxxx` entries ("surrogate pairs"). This error indicates that the surrogate pair is incomplete or contains an invalid code point.
json.exception.[parse_error](@ref parse_error).103 | "parse error: code points above 0x10FFFF are invalid" | Unicode supports code points up to 0x10FFFF. Code points above 0x10FFFF are invalid.
json.exception.[parse_error](@ref parse_error).104 | "parse error: JSON patch must be an array of objects" | [RFC 6902](https://tools.ietf.org/html/rfc6902) requires a JSON Patch document to be a JSON document that represents an array of objects.
json.exception.[parse_error](@ref parse_error).105 | "parse error: operation must have string member 'op'" | An operation of a JSON Patch document must contain exactly one "op" member, whose value indicates the operation to perform. Its value must be one of "add", "remove", "replace", "move", "copy", or "test"; other values are errors.
json.exception.[parse_error](@ref parse_error).106 | "parse error: array index '01' must not begin with '0'" | An array index in a JSON Pointer ([RFC 6901](https://tools.ietf.org/html/rfc6901)) may be `0` or any number wihtout a leading `0`.
json.exception.[parse_error](@ref parse_error).107 | "parse error: JSON pointer must be empty or begin with '/' - was: 'foo'" | A JSON Pointer must be a Unicode string containing a sequence of zero or more reference tokens, each prefixed by a `/` character.
json.exception.[parse_error](@ref parse_error).108 | "parse error: escape character '~' must be followed with '0' or '1'" | In a JSON Pointer, only `~0` and `~1` are valid escape sequences.
json.exception.[parse_error](@ref parse_error).109 | "parse error: array index 'one' is not a number" | A JSON Pointer array index must be a number.
json.exception.[invalid_iterator](@ref invalid_iterator).201 | "iterators are not compatible" | The iterators passed to constructor @ref basic_json(InputIT first, InputIT last) are not compatible, meaning they do not belong to the same container. Therefore, the range (@a first, @a last) is invalid.
json.exception.[invalid_iterator](@ref invalid_iterator).202 | "iterator does not fit current value" | In an erase or insert function, the passed iterator @a pos does not belong to the JSON value for which the function was called. It hence does not define a valid position for the deletion/insertion.
json.exception.[invalid_iterator](@ref invalid_iterator).203 | "iterators do not fit current value" | Either iterator passed to function @ref erase(IteratorType first, IteratorType last) does not belong to the JSON value from which values shall be erased. It hence does not define a valid range to delete values from.
json.exception.[invalid_iterator](@ref invalid_iterator).204 | "iterators out of range" | When an iterator range for a primitive type (number, boolean, or string) is passed to a constructor or an erase function, this range has to be exactly (@ref begin(), @ref end()), because this is the only way the single stored value is expressed. All other ranges are invalid.
json.exception.[invalid_iterator](@ref invalid_iterator).205 | "iterator out of range" | When an iterator for a primitive type (number, boolean, or string) is passed to an erase function, the iterator has to be the @ref begin() iterator, because it is the only way to address the stored value. All other iterators are invalid.
json.exception.[invalid_iterator](@ref invalid_iterator).206 | "cannot construct with iterators from null" | The iterators passed to constructor @ref basic_json(InputIT first, InputIT last) belong to a JSON null value and hence to not define a valid range.
json.exception.[invalid_iterator](@ref invalid_iterator).207 | "cannot use key() for non-object iterators" | The key() member function can only be used on iterators belonging to a JSON object, because other types do not have a concept of a key.
json.exception.[invalid_iterator](@ref invalid_iterator).208 | "cannot use operator[] for object iterators" | The operator[] to specify a concrete offset cannot be used on iterators belonging to a JSON object, because JSON objects are unordered.
json.exception.[invalid_iterator](@ref invalid_iterator).209 | "cannot use offsets with object iterators" | The offset operators (+, -, +=, -=) cannot be used on iterators belonging to a JSON object, because JSON objects are unordered.
json.exception.[invalid_iterator](@ref invalid_iterator).210 | "iterators do not fit" | The iterator range passed to the insert function are not compatible, meaning they do not belong to the same container. Therefore, the range (@a first, @a last) is invalid.
json.exception.[invalid_iterator](@ref invalid_iterator).211 | "passed iterators may not belong to container" | The iterator range passed to the insert function must not be a subrange of the container to insert to.
json.exception.[invalid_iterator](@ref invalid_iterator).212 | "cannot compare iterators of different containers" | When two iterators are compared, they must belong to the same container.
json.exception.[invalid_iterator](@ref invalid_iterator).213 | "cannot compare order of object iterators" | The order of object iterators cannot be compated, because JSON objects are unordered.
json.exception.[invalid_iterator](@ref invalid_iterator).214 | "cannot get value" | Cannot get value for iterator: Either the iterator belongs to a null value or it is an iterator to a primitive type (number, boolean, or string), but the iterator is different to @ref begin().
json.exception.[type_error](@ref type_error).301 | "cannot create object from initializer list" | To create an object from an initializer list, the initializer list must consist only of a list of pairs whose first element is a string. When this constraint is violated, an array is created instead.
json.exception.[type_error](@ref type_error).302 | "type must be object, but is array" | During implicit or explicit value conversion, the JSON type must be compatible to the target type. For instance, a JSON string can only be converted into string types, but not into numbers or boolean types.
json.exception.[type_error](@ref type_error).303 | "incompatible ReferenceType for get_ref, actual type is object" | To retrieve a reference to a value stored in a @ref basic_json object with @ref get_ref, the type of the reference must match the value type. For instance, for a JSON array, the @a ReferenceType must be @ref array_t&.
json.exception.[type_error](@ref type_error).304 | "cannot use at() with string" | The @ref at() member functions can only be executed for certain JSON types.
json.exception.[type_error](@ref type_error).305 | "cannot use operator[] with string" | The @ref operator[] member functions can only be executed for certain JSON types.
json.exception.[type_error](@ref type_error).306 | "cannot use value() with string" | The @ref value() member functions can only be executed for certain JSON types.
json.exception.[type_error](@ref type_error).307 | "cannot use erase() with string" | The @ref erase() member functions can only be executed for certain JSON types.
json.exception.[type_error](@ref type_error).308 | "cannot use push_back() with string" | The @ref push_back() and @ref operator+= member functions can only be executed for certain JSON types.
json.exception.[type_error](@ref type_error).309 | "cannot use insert() with" | The @ref insert() member functions can only be executed for certain JSON types.
json.exception.[type_error](@ref type_error).310 | "cannot use swap() with number" | The @ref swap() member functions can only be executed for certain JSON types.
json.exception.[type_error](@ref type_error).311 | "cannot use emplace_back() with string" | The @ref emplace_back() member function can only be executed for certain JSON types.
json.exception.[type_error](@ref type_error).313 | "invalid value to unflatten" | The @ref unflatten function converts an object whose keys are JSON Pointers back into an arbitrary nested JSON value. The JSON Pointers must not overlap, because then the resulting value would not be well defined.
json.exception.[type_error](@ref type_error).314 | "only objects can be unflattened" | The @ref unflatten function only works for an object whose keys are JSON Pointers.
json.exception.[type_error](@ref type_error).315 | "values in object must be primitive" | The @ref unflatten function only works for an object whose keys are JSON Pointers and whose values are primitive.
json.exception.[out_of_range](@ref out_of_range).401 | "array index 3 is out of range" | The provided array index @a i is larger than @a size-1.
json.exception.[out_of_range](@ref out_of_range).402 | "array index '-' (3) is out of range" | The special array index `-` in a JSON Pointer never describes a valid element of the array, but the index past the end.
json.exception.[out_of_range](@ref out_of_range).403 | "key 'foo' not found" | The provided key was not found in the JSON object.
json.exception.[out_of_range](@ref out_of_range).404 | "unresolved reference token 'foo'" | A reference token in a JSON Pointer could not be resolved.
json.exception.[out_of_range](@ref out_of_range).405 | "JSON pointer has no parent" | The JSON Patch operations 'remove' and 'add' can not be applied to the root element of the JSON value.
json.exception.other.500 | "unsuccessful" | A JSON Patch operation 'test' failed.
@since version 3.0.0
*/
class exception : public std::exception
{
public:
/// create exception with id an explanatory string
exception(int id_, const std::string& ename, const std::string& what_arg_)
: id(id_),
what_arg("[json.exception." + ename + "." + std::to_string(id_) + "] " + what_arg_)
{}
/// returns the explanatory string
virtual const char* what() const noexcept
{
return what_arg.c_str();
}
/// the id of the exception
const int id;
private:
/// the explanatory string
const std::string what_arg;
};
/*!
@brief exception indicating a parse error
This excpetion is thrown by the library when a parse error occurs. Parse
errors can occur during the deserialization of JSON text as well as when
using JSON Patch.
Exceptions have ids 1xx.
@since version 3.0.0
*/
class parse_error : public exception
{
public:
/*!
@brief create a parse error exception
@param[in] id_ the id of the exception
@param[in] byte_ the byte index where the error occured (or 0 if
the position cannot be determined)
@param[in] what_arg_ the explanatory string
*/
parse_error(int id_, size_t byte_, const std::string& what_arg_)
: exception(id_, "parse_error", "parse error" +
(byte_ != 0 ? (" at " + std::to_string(byte_)) : "") +
": " + what_arg_),
byte(byte_)
{}
/*!
@brief byte index of the parse error
The byte index of the last read character in the input file.
@note For an input with n bytes, 1 is the index of the first character
and n+1 is the index of the terminating null byte or the end of
file.
*/
const size_t byte;
};
/*!
@brief exception indicating errors with iterators
Exceptions have ids 2xx.
@since version 3.0.0
*/
class invalid_iterator : public exception
{
public:
invalid_iterator(int id_, const std::string& what_arg_)
: exception(id_, "invalid_iterator", what_arg_)
{}
};
/*!
@brief exception indicating executing a member function with a wrong type
Exceptions have ids 3xx.
@since version 3.0.0
*/
class type_error : public exception
{
public:
type_error(int id_, const std::string& what_arg_)
: exception(id_, "type_error", what_arg_)
{}
};
/*!
@brief exception indicating access out of the defined range
Exceptions have ids 4xx.
@since version 3.0.0
*/
class out_of_range : public exception
{
public:
out_of_range(int id_, const std::string& what_arg_)
: exception(id_, "out_of_range", what_arg_)
{}
};
/// @}
/*!
@brief returns the allocator associated with the container
*/
@ -3161,7 +3195,7 @@ class basic_json
return m_value.boolean;
}
JSON_THROW(std::domain_error("type must be boolean, but is " + type_name()));
JSON_THROW(type_error(302, "type must be boolean, but is " + type_name()));
}
/// get a pointer to the value (object)

View file

@ -110,6 +110,187 @@ This namespace collects some functions that could not be defined inside the
*/
namespace detail
{
////////////////
// exceptions //
////////////////
/*!
@brief general exception of the @ref basic_json class
Extension of std::exception objects with a member @a id for exception ids.
@since version 3.0.0
*/
class exception : public std::exception
{
public:
/// create exception with id an explanatory string
exception(int id_, const std::string& ename, const std::string& what_arg_)
: id(id_),
what_arg("[json.exception." + ename + "." + std::to_string(id_) + "] " + what_arg_)
{}
/// returns the explanatory string
virtual const char* what() const noexcept
{
return what_arg.c_str();
}
/// the id of the exception
const int id;
private:
/// the explanatory string
const std::string what_arg;
};
/*!
@brief exception indicating a parse error
This excpetion is thrown by the library when a parse error occurs. Parse
errors can occur during the deserialization of JSON text as well as when
using JSON Patch.
Member @a byte holds the byte index of the last read character in the input
file.
@note For an input with n bytes, 1 is the index of the first character
and n+1 is the index of the terminating null byte or the end of
file.
Exceptions have ids 1xx.
name / id | example massage | description
------------------------------ | --------------- | -------------------------
json.exception.[parse_error](@ref parse_error).101 | "parse error at 2: unexpected end of input; expected string literal" | This error indicates a syntax error while deserializing a JSON text. The error message describes that an unexpected token (character) was encountered, and the member @ref parse_error::byte indicates the error position.
json.exception.[parse_error](@ref parse_error).102 | "parse error at 14: missing or wrong low surrogate" | JSON uses the `\uxxxx` format to describe Unicode characters. Code points above above 0xFFFF are split into two `\uxxxx` entries ("surrogate pairs"). This error indicates that the surrogate pair is incomplete or contains an invalid code point.
json.exception.[parse_error](@ref parse_error).103 | "parse error: code points above 0x10FFFF are invalid" | Unicode supports code points up to 0x10FFFF. Code points above 0x10FFFF are invalid.
json.exception.[parse_error](@ref parse_error).104 | "parse error: JSON patch must be an array of objects" | [RFC 6902](https://tools.ietf.org/html/rfc6902) requires a JSON Patch document to be a JSON document that represents an array of objects.
json.exception.[parse_error](@ref parse_error).105 | "parse error: operation must have string member 'op'" | An operation of a JSON Patch document must contain exactly one "op" member, whose value indicates the operation to perform. Its value must be one of "add", "remove", "replace", "move", "copy", or "test"; other values are errors.
json.exception.[parse_error](@ref parse_error).106 | "parse error: array index '01' must not begin with '0'" | An array index in a JSON Pointer ([RFC 6901](https://tools.ietf.org/html/rfc6901)) may be `0` or any number wihtout a leading `0`.
json.exception.[parse_error](@ref parse_error).107 | "parse error: JSON pointer must be empty or begin with '/' - was: 'foo'" | A JSON Pointer must be a Unicode string containing a sequence of zero or more reference tokens, each prefixed by a `/` character.
json.exception.[parse_error](@ref parse_error).108 | "parse error: escape character '~' must be followed with '0' or '1'" | In a JSON Pointer, only `~0` and `~1` are valid escape sequences.
json.exception.[parse_error](@ref parse_error).109 | "parse error: array index 'one' is not a number" | A JSON Pointer array index must be a number.
@since version 3.0.0
*/
class parse_error : public exception
{
public:
/*!
@brief create a parse error exception
@param[in] id_ the id of the exception
@param[in] byte_ the byte index where the error occured (or 0 if
the position cannot be determined)
@param[in] what_arg_ the explanatory string
*/
parse_error(int id_, size_t byte_, const std::string& what_arg_)
: exception(id_, "parse_error", "parse error" +
(byte_ != 0 ? (" at " + std::to_string(byte_)) : "") +
": " + what_arg_),
byte(byte_)
{}
/*!
@brief byte index of the parse error
The byte index of the last read character in the input file.
@note For an input with n bytes, 1 is the index of the first character
and n+1 is the index of the terminating null byte or the end of
file.
*/
const size_t byte;
};
/*!
@brief exception indicating errors with iterators
Exceptions have ids 2xx.
name / id | example massage | description
------------------------------ | --------------- | -------------------------
json.exception.[invalid_iterator](@ref invalid_iterator).201 | "iterators are not compatible" | The iterators passed to constructor @ref basic_json(InputIT first, InputIT last) are not compatible, meaning they do not belong to the same container. Therefore, the range (@a first, @a last) is invalid.
json.exception.[invalid_iterator](@ref invalid_iterator).202 | "iterator does not fit current value" | In an erase or insert function, the passed iterator @a pos does not belong to the JSON value for which the function was called. It hence does not define a valid position for the deletion/insertion.
json.exception.[invalid_iterator](@ref invalid_iterator).203 | "iterators do not fit current value" | Either iterator passed to function @ref erase(IteratorType first, IteratorType last) does not belong to the JSON value from which values shall be erased. It hence does not define a valid range to delete values from.
json.exception.[invalid_iterator](@ref invalid_iterator).204 | "iterators out of range" | When an iterator range for a primitive type (number, boolean, or string) is passed to a constructor or an erase function, this range has to be exactly (@ref begin(), @ref end()), because this is the only way the single stored value is expressed. All other ranges are invalid.
json.exception.[invalid_iterator](@ref invalid_iterator).205 | "iterator out of range" | When an iterator for a primitive type (number, boolean, or string) is passed to an erase function, the iterator has to be the @ref begin() iterator, because it is the only way to address the stored value. All other iterators are invalid.
json.exception.[invalid_iterator](@ref invalid_iterator).206 | "cannot construct with iterators from null" | The iterators passed to constructor @ref basic_json(InputIT first, InputIT last) belong to a JSON null value and hence to not define a valid range.
json.exception.[invalid_iterator](@ref invalid_iterator).207 | "cannot use key() for non-object iterators" | The key() member function can only be used on iterators belonging to a JSON object, because other types do not have a concept of a key.
json.exception.[invalid_iterator](@ref invalid_iterator).208 | "cannot use operator[] for object iterators" | The operator[] to specify a concrete offset cannot be used on iterators belonging to a JSON object, because JSON objects are unordered.
json.exception.[invalid_iterator](@ref invalid_iterator).209 | "cannot use offsets with object iterators" | The offset operators (+, -, +=, -=) cannot be used on iterators belonging to a JSON object, because JSON objects are unordered.
json.exception.[invalid_iterator](@ref invalid_iterator).210 | "iterators do not fit" | The iterator range passed to the insert function are not compatible, meaning they do not belong to the same container. Therefore, the range (@a first, @a last) is invalid.
json.exception.[invalid_iterator](@ref invalid_iterator).211 | "passed iterators may not belong to container" | The iterator range passed to the insert function must not be a subrange of the container to insert to.
json.exception.[invalid_iterator](@ref invalid_iterator).212 | "cannot compare iterators of different containers" | When two iterators are compared, they must belong to the same container.
json.exception.[invalid_iterator](@ref invalid_iterator).213 | "cannot compare order of object iterators" | The order of object iterators cannot be compated, because JSON objects are unordered.
json.exception.[invalid_iterator](@ref invalid_iterator).214 | "cannot get value" | Cannot get value for iterator: Either the iterator belongs to a null value or it is an iterator to a primitive type (number, boolean, or string), but the iterator is different to @ref begin().
@since version 3.0.0
*/
class invalid_iterator : public exception
{
public:
invalid_iterator(int id_, const std::string& what_arg_)
: exception(id_, "invalid_iterator", what_arg_)
{}
};
/*!
@brief exception indicating executing a member function with a wrong type
Exceptions have ids 3xx.
name / id | example massage | description
------------------------------ | --------------- | -------------------------
json.exception.[type_error](@ref type_error).301 | "cannot create object from initializer list" | To create an object from an initializer list, the initializer list must consist only of a list of pairs whose first element is a string. When this constraint is violated, an array is created instead.
json.exception.[type_error](@ref type_error).302 | "type must be object, but is array" | During implicit or explicit value conversion, the JSON type must be compatible to the target type. For instance, a JSON string can only be converted into string types, but not into numbers or boolean types.
json.exception.[type_error](@ref type_error).303 | "incompatible ReferenceType for get_ref, actual type is object" | To retrieve a reference to a value stored in a @ref basic_json object with @ref get_ref, the type of the reference must match the value type. For instance, for a JSON array, the @a ReferenceType must be @ref array_t&.
json.exception.[type_error](@ref type_error).304 | "cannot use at() with string" | The @ref at() member functions can only be executed for certain JSON types.
json.exception.[type_error](@ref type_error).305 | "cannot use operator[] with string" | The @ref operator[] member functions can only be executed for certain JSON types.
json.exception.[type_error](@ref type_error).306 | "cannot use value() with string" | The @ref value() member functions can only be executed for certain JSON types.
json.exception.[type_error](@ref type_error).307 | "cannot use erase() with string" | The @ref erase() member functions can only be executed for certain JSON types.
json.exception.[type_error](@ref type_error).308 | "cannot use push_back() with string" | The @ref push_back() and @ref operator+= member functions can only be executed for certain JSON types.
json.exception.[type_error](@ref type_error).309 | "cannot use insert() with" | The @ref insert() member functions can only be executed for certain JSON types.
json.exception.[type_error](@ref type_error).310 | "cannot use swap() with number" | The @ref swap() member functions can only be executed for certain JSON types.
json.exception.[type_error](@ref type_error).311 | "cannot use emplace_back() with string" | The @ref emplace_back() member function can only be executed for certain JSON types.
json.exception.[type_error](@ref type_error).313 | "invalid value to unflatten" | The @ref unflatten function converts an object whose keys are JSON Pointers back into an arbitrary nested JSON value. The JSON Pointers must not overlap, because then the resulting value would not be well defined.
json.exception.[type_error](@ref type_error).314 | "only objects can be unflattened" | The @ref unflatten function only works for an object whose keys are JSON Pointers.
json.exception.[type_error](@ref type_error).315 | "values in object must be primitive" | The @ref unflatten function only works for an object whose keys are JSON Pointers and whose values are primitive.
@since version 3.0.0
*/
class type_error : public exception
{
public:
type_error(int id_, const std::string& what_arg_)
: exception(id_, "type_error", what_arg_)
{}
};
/*!
@brief exception indicating access out of the defined range
Exceptions have ids 4xx.
name / id | example massage | description
------------------------------ | --------------- | -------------------------
json.exception.[out_of_range](@ref out_of_range).401 | "array index 3 is out of range" | The provided array index @a i is larger than @a size-1.
json.exception.[out_of_range](@ref out_of_range).402 | "array index '-' (3) is out of range" | The special array index `-` in a JSON Pointer never describes a valid element of the array, but the index past the end.
json.exception.[out_of_range](@ref out_of_range).403 | "key 'foo' not found" | The provided key was not found in the JSON object.
json.exception.[out_of_range](@ref out_of_range).404 | "unresolved reference token 'foo'" | A reference token in a JSON Pointer could not be resolved.
json.exception.[out_of_range](@ref out_of_range).405 | "JSON pointer has no parent" | The JSON Patch operations 'remove' and 'add' can not be applied to the root element of the JSON value.
@since version 3.0.0
*/
class out_of_range : public exception
{
public:
out_of_range(int id_, const std::string& what_arg_)
: exception(id_, "out_of_range", what_arg_)
{}
};
///////////////////////////
// JSON type enumeration //
///////////////////////////
@ -617,8 +798,7 @@ void get_arithmetic_value(const BasicJsonType& j, ArithmeticType& val)
}
default:
{
JSON_THROW(
std::domain_error("type must be number, but is " + j.type_name()));
JSON_THROW(type_error(302, "type must be number, but is " + j.type_name()));
}
}
}
@ -628,7 +808,7 @@ void from_json(const BasicJsonType& j, typename BasicJsonType::boolean_t& b)
{
if (not j.is_boolean())
{
JSON_THROW(std::domain_error("type must be boolean, but is " + j.type_name()));
JSON_THROW(type_error(302, "type must be boolean, but is " + j.type_name()));
}
b = *j.template get_ptr<const typename BasicJsonType::boolean_t*>();
}
@ -638,7 +818,7 @@ void from_json(const BasicJsonType& j, typename BasicJsonType::string_t& s)
{
if (not j.is_string())
{
JSON_THROW(std::domain_error("type must be string, but is " + j.type_name()));
JSON_THROW(type_error(302, "type must be string, but is " + j.type_name()));
}
s = *j.template get_ptr<const typename BasicJsonType::string_t*>();
}
@ -675,7 +855,7 @@ void from_json(const BasicJsonType& j, typename BasicJsonType::array_t& arr)
{
if (not j.is_array())
{
JSON_THROW(std::domain_error("type must be array, but is " + j.type_name()));
JSON_THROW(type_error(302, "type must be array, but is " + j.type_name()));
}
arr = *j.template get_ptr<const typename BasicJsonType::array_t*>();
}
@ -688,13 +868,13 @@ void from_json(const BasicJsonType& j, std::forward_list<T, Allocator>& l)
// (except when it's null.. ?)
if (j.is_null())
{
JSON_THROW(std::domain_error("type must be array, but is " + j.type_name()));
JSON_THROW(type_error(302, "type must be array, but is " + j.type_name()));
}
if (not std::is_same<T, BasicJsonType>::value)
{
if (not j.is_array())
{
JSON_THROW(std::domain_error("type must be array, but is " + j.type_name()));
JSON_THROW(type_error(302, "type must be array, but is " + j.type_name()));
}
}
for (auto it = j.rbegin(), end = j.rend(); it != end; ++it)
@ -744,7 +924,7 @@ void from_json(const BasicJsonType& j, CompatibleArrayType& arr)
{
if (j.is_null())
{
JSON_THROW(std::domain_error("type must be array, but is " + j.type_name()));
JSON_THROW(type_error(302, "type must be array, but is " + j.type_name()));
}
// when T == BasicJsonType, do not check if value_t is correct
@ -752,7 +932,7 @@ void from_json(const BasicJsonType& j, CompatibleArrayType& arr)
{
if (not j.is_array())
{
JSON_THROW(std::domain_error("type must be array, but is " + j.type_name()));
JSON_THROW(type_error(302, "type must be array, but is " + j.type_name()));
}
}
from_json_array_impl(j, arr, priority_tag<1> {});
@ -764,7 +944,7 @@ void from_json(const BasicJsonType& j, CompatibleObjectType& obj)
{
if (not j.is_object())
{
JSON_THROW(std::domain_error("type must be object, but is " + j.type_name()));
JSON_THROW(type_error(302, "type must be object, but is " + j.type_name()));
}
auto inner_object = j.template get_ptr<const typename BasicJsonType::object_t*>();
@ -814,7 +994,7 @@ void from_json(const BasicJsonType& j, ArithmeticType& val)
}
default:
{
JSON_THROW(std::domain_error("type must be number, but is " + j.type_name()));
JSON_THROW(type_error(302, "type must be number, but is " + j.type_name()));
}
}
}
@ -1046,6 +1226,33 @@ class basic_json
template<typename T, typename SFINAE>
using json_serializer = JSONSerializer<T, SFINAE>;
////////////////
// exceptions //
////////////////
/// @name exceptions
/// Classes to implement user-defined exceptions.
/// @{
/*
name / id | example massage | description
------------------------------ | --------------- | -------------------------
json.exception.other.500 | "unsuccessful" | A JSON Patch operation 'test' failed.
*/
/// @copydoc detail::parse_error
using parse_error = detail::parse_error;
/// @copydoc detail::invalid_iterator
using invalid_iterator = detail::invalid_iterator;
/// @copydoc detail::type_error
using type_error = detail::type_error;
/// @copydoc detail::out_of_range
using out_of_range = detail::out_of_range;
/// @}
/////////////////////
// container types //
/////////////////////
@ -1088,179 +1295,6 @@ class basic_json
/// @}
public:
////////////////
// exceptions //
////////////////
/// @name exceptions
/// Classes to implement user-defined exceptions.
/// @{
/*!
@brief general exception of the @ref basic_json class
Extension of std::exception objects with a member @a id for exception ids.
name / id | example massage | description
------------------------------ | --------------- | -------------------------
json.exception.[parse_error](@ref parse_error).101 | "parse error at 2: unexpected end of input; expected string literal" | This error indicates a syntax error while deserializing a JSON text. The error message describes that an unexpected token (character) was encountered, and the member @ref parse_error::byte indicates the error position.
json.exception.[parse_error](@ref parse_error).102 | "parse error at 14: missing or wrong low surrogate" | JSON uses the `\uxxxx` format to describe Unicode characters. Code points above above 0xFFFF are split into two `\uxxxx` entries ("surrogate pairs"). This error indicates that the surrogate pair is incomplete or contains an invalid code point.
json.exception.[parse_error](@ref parse_error).103 | "parse error: code points above 0x10FFFF are invalid" | Unicode supports code points up to 0x10FFFF. Code points above 0x10FFFF are invalid.
json.exception.[parse_error](@ref parse_error).104 | "parse error: JSON patch must be an array of objects" | [RFC 6902](https://tools.ietf.org/html/rfc6902) requires a JSON Patch document to be a JSON document that represents an array of objects.
json.exception.[parse_error](@ref parse_error).105 | "parse error: operation must have string member 'op'" | An operation of a JSON Patch document must contain exactly one "op" member, whose value indicates the operation to perform. Its value must be one of "add", "remove", "replace", "move", "copy", or "test"; other values are errors.
json.exception.[parse_error](@ref parse_error).106 | "parse error: array index '01' must not begin with '0'" | An array index in a JSON Pointer ([RFC 6901](https://tools.ietf.org/html/rfc6901)) may be `0` or any number wihtout a leading `0`.
json.exception.[parse_error](@ref parse_error).107 | "parse error: JSON pointer must be empty or begin with '/' - was: 'foo'" | A JSON Pointer must be a Unicode string containing a sequence of zero or more reference tokens, each prefixed by a `/` character.
json.exception.[parse_error](@ref parse_error).108 | "parse error: escape character '~' must be followed with '0' or '1'" | In a JSON Pointer, only `~0` and `~1` are valid escape sequences.
json.exception.[parse_error](@ref parse_error).109 | "parse error: array index 'one' is not a number" | A JSON Pointer array index must be a number.
json.exception.[invalid_iterator](@ref invalid_iterator).201 | "iterators are not compatible" | The iterators passed to constructor @ref basic_json(InputIT first, InputIT last) are not compatible, meaning they do not belong to the same container. Therefore, the range (@a first, @a last) is invalid.
json.exception.[invalid_iterator](@ref invalid_iterator).202 | "iterator does not fit current value" | In an erase or insert function, the passed iterator @a pos does not belong to the JSON value for which the function was called. It hence does not define a valid position for the deletion/insertion.
json.exception.[invalid_iterator](@ref invalid_iterator).203 | "iterators do not fit current value" | Either iterator passed to function @ref erase(IteratorType first, IteratorType last) does not belong to the JSON value from which values shall be erased. It hence does not define a valid range to delete values from.
json.exception.[invalid_iterator](@ref invalid_iterator).204 | "iterators out of range" | When an iterator range for a primitive type (number, boolean, or string) is passed to a constructor or an erase function, this range has to be exactly (@ref begin(), @ref end()), because this is the only way the single stored value is expressed. All other ranges are invalid.
json.exception.[invalid_iterator](@ref invalid_iterator).205 | "iterator out of range" | When an iterator for a primitive type (number, boolean, or string) is passed to an erase function, the iterator has to be the @ref begin() iterator, because it is the only way to address the stored value. All other iterators are invalid.
json.exception.[invalid_iterator](@ref invalid_iterator).206 | "cannot construct with iterators from null" | The iterators passed to constructor @ref basic_json(InputIT first, InputIT last) belong to a JSON null value and hence to not define a valid range.
json.exception.[invalid_iterator](@ref invalid_iterator).207 | "cannot use key() for non-object iterators" | The key() member function can only be used on iterators belonging to a JSON object, because other types do not have a concept of a key.
json.exception.[invalid_iterator](@ref invalid_iterator).208 | "cannot use operator[] for object iterators" | The operator[] to specify a concrete offset cannot be used on iterators belonging to a JSON object, because JSON objects are unordered.
json.exception.[invalid_iterator](@ref invalid_iterator).209 | "cannot use offsets with object iterators" | The offset operators (+, -, +=, -=) cannot be used on iterators belonging to a JSON object, because JSON objects are unordered.
json.exception.[invalid_iterator](@ref invalid_iterator).210 | "iterators do not fit" | The iterator range passed to the insert function are not compatible, meaning they do not belong to the same container. Therefore, the range (@a first, @a last) is invalid.
json.exception.[invalid_iterator](@ref invalid_iterator).211 | "passed iterators may not belong to container" | The iterator range passed to the insert function must not be a subrange of the container to insert to.
json.exception.[invalid_iterator](@ref invalid_iterator).212 | "cannot compare iterators of different containers" | When two iterators are compared, they must belong to the same container.
json.exception.[invalid_iterator](@ref invalid_iterator).213 | "cannot compare order of object iterators" | The order of object iterators cannot be compated, because JSON objects are unordered.
json.exception.[invalid_iterator](@ref invalid_iterator).214 | "cannot get value" | Cannot get value for iterator: Either the iterator belongs to a null value or it is an iterator to a primitive type (number, boolean, or string), but the iterator is different to @ref begin().
json.exception.[type_error](@ref type_error).301 | "cannot create object from initializer list" | To create an object from an initializer list, the initializer list must consist only of a list of pairs whose first element is a string. When this constraint is violated, an array is created instead.
json.exception.[type_error](@ref type_error).302 | "type must be object, but is array" | During implicit or explicit value conversion, the JSON type must be compatible to the target type. For instance, a JSON string can only be converted into string types, but not into numbers or boolean types.
json.exception.[type_error](@ref type_error).303 | "incompatible ReferenceType for get_ref, actual type is object" | To retrieve a reference to a value stored in a @ref basic_json object with @ref get_ref, the type of the reference must match the value type. For instance, for a JSON array, the @a ReferenceType must be @ref array_t&.
json.exception.[type_error](@ref type_error).304 | "cannot use at() with string" | The @ref at() member functions can only be executed for certain JSON types.
json.exception.[type_error](@ref type_error).305 | "cannot use operator[] with string" | The @ref operator[] member functions can only be executed for certain JSON types.
json.exception.[type_error](@ref type_error).306 | "cannot use value() with string" | The @ref value() member functions can only be executed for certain JSON types.
json.exception.[type_error](@ref type_error).307 | "cannot use erase() with string" | The @ref erase() member functions can only be executed for certain JSON types.
json.exception.[type_error](@ref type_error).308 | "cannot use push_back() with string" | The @ref push_back() and @ref operator+= member functions can only be executed for certain JSON types.
json.exception.[type_error](@ref type_error).309 | "cannot use insert() with" | The @ref insert() member functions can only be executed for certain JSON types.
json.exception.[type_error](@ref type_error).310 | "cannot use swap() with number" | The @ref swap() member functions can only be executed for certain JSON types.
json.exception.[type_error](@ref type_error).311 | "cannot use emplace_back() with string" | The @ref emplace_back() member function can only be executed for certain JSON types.
json.exception.[type_error](@ref type_error).313 | "invalid value to unflatten" | The @ref unflatten function converts an object whose keys are JSON Pointers back into an arbitrary nested JSON value. The JSON Pointers must not overlap, because then the resulting value would not be well defined.
json.exception.[type_error](@ref type_error).314 | "only objects can be unflattened" | The @ref unflatten function only works for an object whose keys are JSON Pointers.
json.exception.[type_error](@ref type_error).315 | "values in object must be primitive" | The @ref unflatten function only works for an object whose keys are JSON Pointers and whose values are primitive.
json.exception.[out_of_range](@ref out_of_range).401 | "array index 3 is out of range" | The provided array index @a i is larger than @a size-1.
json.exception.[out_of_range](@ref out_of_range).402 | "array index '-' (3) is out of range" | The special array index `-` in a JSON Pointer never describes a valid element of the array, but the index past the end.
json.exception.[out_of_range](@ref out_of_range).403 | "key 'foo' not found" | The provided key was not found in the JSON object.
json.exception.[out_of_range](@ref out_of_range).404 | "unresolved reference token 'foo'" | A reference token in a JSON Pointer could not be resolved.
json.exception.[out_of_range](@ref out_of_range).405 | "JSON pointer has no parent" | The JSON Patch operations 'remove' and 'add' can not be applied to the root element of the JSON value.
json.exception.other.500 | "unsuccessful" | A JSON Patch operation 'test' failed.
@since version 3.0.0
*/
class exception : public std::exception
{
public:
/// create exception with id an explanatory string
exception(int id_, const std::string& ename, const std::string& what_arg_)
: id(id_),
what_arg("[json.exception." + ename + "." + std::to_string(id_) + "] " + what_arg_)
{}
/// returns the explanatory string
virtual const char* what() const noexcept
{
return what_arg.c_str();
}
/// the id of the exception
const int id;
private:
/// the explanatory string
const std::string what_arg;
};
/*!
@brief exception indicating a parse error
This excpetion is thrown by the library when a parse error occurs. Parse
errors can occur during the deserialization of JSON text as well as when
using JSON Patch.
Exceptions have ids 1xx.
@since version 3.0.0
*/
class parse_error : public exception
{
public:
/*!
@brief create a parse error exception
@param[in] id_ the id of the exception
@param[in] byte_ the byte index where the error occured (or 0 if
the position cannot be determined)
@param[in] what_arg_ the explanatory string
*/
parse_error(int id_, size_t byte_, const std::string& what_arg_)
: exception(id_, "parse_error", "parse error" +
(byte_ != 0 ? (" at " + std::to_string(byte_)) : "") +
": " + what_arg_),
byte(byte_)
{}
/*!
@brief byte index of the parse error
The byte index of the last read character in the input file.
@note For an input with n bytes, 1 is the index of the first character
and n+1 is the index of the terminating null byte or the end of
file.
*/
const size_t byte;
};
/*!
@brief exception indicating errors with iterators
Exceptions have ids 2xx.
@since version 3.0.0
*/
class invalid_iterator : public exception
{
public:
invalid_iterator(int id_, const std::string& what_arg_)
: exception(id_, "invalid_iterator", what_arg_)
{}
};
/*!
@brief exception indicating executing a member function with a wrong type
Exceptions have ids 3xx.
@since version 3.0.0
*/
class type_error : public exception
{
public:
type_error(int id_, const std::string& what_arg_)
: exception(id_, "type_error", what_arg_)
{}
};
/*!
@brief exception indicating access out of the defined range
Exceptions have ids 4xx.
@since version 3.0.0
*/
class out_of_range : public exception
{
public:
out_of_range(int id_, const std::string& what_arg_)
: exception(id_, "out_of_range", what_arg_)
{}
};
/// @}
/*!
@brief returns the allocator associated with the container
*/
@ -3161,7 +3195,7 @@ class basic_json
return m_value.boolean;
}
JSON_THROW(std::domain_error("type must be boolean, but is " + type_name()));
JSON_THROW(type_error(302, "type must be boolean, but is " + type_name()));
}
/// get a pointer to the value (object)

View file

@ -78,28 +78,28 @@ TEST_CASE("value conversion")
SECTION("exception in case of a non-object type")
{
CHECK_THROWS_AS(json(json::value_t::null).get<json::object_t>(), std::logic_error);
CHECK_THROWS_AS(json(json::value_t::array).get<json::object_t>(), std::logic_error);
CHECK_THROWS_AS(json(json::value_t::string).get<json::object_t>(), std::logic_error);
CHECK_THROWS_AS(json(json::value_t::boolean).get<json::object_t>(), std::logic_error);
CHECK_THROWS_AS(json(json::value_t::number_integer).get<json::object_t>(), std::logic_error);
CHECK_THROWS_AS(json(json::value_t::number_unsigned).get<json::object_t>(), std::logic_error);
CHECK_THROWS_AS(json(json::value_t::number_float).get<json::object_t>(), std::logic_error);
CHECK_THROWS_AS(json(json::value_t::null).get<json::object_t>(), json::type_error);
CHECK_THROWS_AS(json(json::value_t::array).get<json::object_t>(), json::type_error);
CHECK_THROWS_AS(json(json::value_t::string).get<json::object_t>(), json::type_error);
CHECK_THROWS_AS(json(json::value_t::boolean).get<json::object_t>(), json::type_error);
CHECK_THROWS_AS(json(json::value_t::number_integer).get<json::object_t>(), json::type_error);
CHECK_THROWS_AS(json(json::value_t::number_unsigned).get<json::object_t>(), json::type_error);
CHECK_THROWS_AS(json(json::value_t::number_float).get<json::object_t>(), json::type_error);
CHECK_THROWS_WITH(json(json::value_t::null).get<json::object_t>(),
"type must be object, but is null");
"[json.exception.type_error.302] type must be object, but is null");
CHECK_THROWS_WITH(json(json::value_t::array).get<json::object_t>(),
"type must be object, but is array");
"[json.exception.type_error.302] type must be object, but is array");
CHECK_THROWS_WITH(json(json::value_t::string).get<json::object_t>(),
"type must be object, but is string");
"[json.exception.type_error.302] type must be object, but is string");
CHECK_THROWS_WITH(json(json::value_t::boolean).get<json::object_t>(),
"type must be object, but is boolean");
"[json.exception.type_error.302] type must be object, but is boolean");
CHECK_THROWS_WITH(json(json::value_t::number_integer).get<json::object_t>(),
"type must be object, but is number");
"[json.exception.type_error.302] type must be object, but is number");
CHECK_THROWS_WITH(json(json::value_t::number_unsigned).get<json::object_t>(),
"type must be object, but is number");
"[json.exception.type_error.302] type must be object, but is number");
CHECK_THROWS_WITH(json(json::value_t::number_float).get<json::object_t>(),
"type must be object, but is number");
"[json.exception.type_error.302] type must be object, but is number");
}
}
@ -161,9 +161,9 @@ TEST_CASE("value conversion")
std::forward_list<json> a = j.get<std::forward_list<json>>();
CHECK(json(a) == j);
CHECK_THROWS_AS(json(json::value_t::null).get<std::forward_list<json>>(), std::logic_error);
CHECK_THROWS_AS(json(json::value_t::null).get<std::forward_list<json>>(), json::type_error);
CHECK_THROWS_WITH(json(json::value_t::null).get<std::forward_list<json>>(),
"type must be array, but is null");
"[json.exception.type_error.302] type must be array, but is null");
}
SECTION("std::vector<json>")
@ -171,16 +171,16 @@ TEST_CASE("value conversion")
std::vector<json> a = j.get<std::vector<json>>();
CHECK(json(a) == j);
CHECK_THROWS_AS(json(json::value_t::null).get<std::vector<json>>(), std::logic_error);
CHECK_THROWS_AS(json(json::value_t::null).get<std::vector<json>>(), json::type_error);
CHECK_THROWS_WITH(json(json::value_t::null).get<std::vector<json>>(),
"type must be array, but is null");
"[json.exception.type_error.302] type must be array, but is null");
#if not defined(JSON_NOEXCEPTION)
SECTION("reserve is called on containers that supports it")
{
// making the call to from_json throw in order to check capacity
std::vector<float> v;
CHECK_THROWS_AS(nlohmann::from_json(j, v), std::logic_error);
CHECK_THROWS_AS(nlohmann::from_json(j, v), json::type_error);
CHECK(v.capacity() == j.size());
// make sure all values are properly copied
@ -198,30 +198,30 @@ TEST_CASE("value conversion")
SECTION("exception in case of a non-array type")
{
CHECK_THROWS_AS(json(json::value_t::null).get<json::array_t>(), std::logic_error);
CHECK_THROWS_AS(json(json::value_t::object).get<json::array_t>(), std::logic_error);
CHECK_THROWS_AS(json(json::value_t::string).get<json::array_t>(), std::logic_error);
CHECK_THROWS_AS(json(json::value_t::boolean).get<json::array_t>(), std::logic_error);
CHECK_THROWS_AS(json(json::value_t::number_integer).get<json::array_t>(), std::logic_error);
CHECK_THROWS_AS(json(json::value_t::number_unsigned).get<json::array_t>(), std::logic_error);
CHECK_THROWS_AS(json(json::value_t::number_float).get<json::array_t>(), std::logic_error);
CHECK_THROWS_AS(json(json::value_t::null).get<json::array_t>(), json::type_error);
CHECK_THROWS_AS(json(json::value_t::object).get<json::array_t>(), json::type_error);
CHECK_THROWS_AS(json(json::value_t::string).get<json::array_t>(), json::type_error);
CHECK_THROWS_AS(json(json::value_t::boolean).get<json::array_t>(), json::type_error);
CHECK_THROWS_AS(json(json::value_t::number_integer).get<json::array_t>(), json::type_error);
CHECK_THROWS_AS(json(json::value_t::number_unsigned).get<json::array_t>(), json::type_error);
CHECK_THROWS_AS(json(json::value_t::number_float).get<json::array_t>(), json::type_error);
CHECK_THROWS_WITH(json(json::value_t::object).get<std::vector<int>>(),
"type must be array, but is object");
"[json.exception.type_error.302] type must be array, but is object");
CHECK_THROWS_WITH(json(json::value_t::null).get<json::array_t>(),
"type must be array, but is null");
"[json.exception.type_error.302] type must be array, but is null");
CHECK_THROWS_WITH(json(json::value_t::object).get<json::array_t>(),
"type must be array, but is object");
"[json.exception.type_error.302] type must be array, but is object");
CHECK_THROWS_WITH(json(json::value_t::string).get<json::array_t>(),
"type must be array, but is string");
"[json.exception.type_error.302] type must be array, but is string");
CHECK_THROWS_WITH(json(json::value_t::boolean).get<json::array_t>(),
"type must be array, but is boolean");
"[json.exception.type_error.302] type must be array, but is boolean");
CHECK_THROWS_WITH(json(json::value_t::number_integer).get<json::array_t>(),
"type must be array, but is number");
"[json.exception.type_error.302] type must be array, but is number");
CHECK_THROWS_WITH(json(json::value_t::number_unsigned).get<json::array_t>(),
"type must be array, but is number");
"[json.exception.type_error.302] type must be array, but is number");
CHECK_THROWS_WITH(json(json::value_t::number_float).get<json::array_t>(),
"type must be array, but is number");
"[json.exception.type_error.302] type must be array, but is number");
}
}
@ -280,28 +280,28 @@ TEST_CASE("value conversion")
SECTION("exception in case of a non-string type")
{
CHECK_THROWS_AS(json(json::value_t::null).get<json::string_t>(), std::logic_error);
CHECK_THROWS_AS(json(json::value_t::object).get<json::string_t>(), std::logic_error);
CHECK_THROWS_AS(json(json::value_t::array).get<json::string_t>(), std::logic_error);
CHECK_THROWS_AS(json(json::value_t::boolean).get<json::string_t>(), std::logic_error);
CHECK_THROWS_AS(json(json::value_t::number_integer).get<json::string_t>(), std::logic_error);
CHECK_THROWS_AS(json(json::value_t::number_unsigned).get<json::string_t>(), std::logic_error);
CHECK_THROWS_AS(json(json::value_t::number_float).get<json::string_t>(), std::logic_error);
CHECK_THROWS_AS(json(json::value_t::null).get<json::string_t>(), json::type_error);
CHECK_THROWS_AS(json(json::value_t::object).get<json::string_t>(), json::type_error);
CHECK_THROWS_AS(json(json::value_t::array).get<json::string_t>(), json::type_error);
CHECK_THROWS_AS(json(json::value_t::boolean).get<json::string_t>(), json::type_error);
CHECK_THROWS_AS(json(json::value_t::number_integer).get<json::string_t>(), json::type_error);
CHECK_THROWS_AS(json(json::value_t::number_unsigned).get<json::string_t>(), json::type_error);
CHECK_THROWS_AS(json(json::value_t::number_float).get<json::string_t>(), json::type_error);
CHECK_THROWS_WITH(json(json::value_t::null).get<json::string_t>(),
"type must be string, but is null");
"[json.exception.type_error.302] type must be string, but is null");
CHECK_THROWS_WITH(json(json::value_t::object).get<json::string_t>(),
"type must be string, but is object");
"[json.exception.type_error.302] type must be string, but is object");
CHECK_THROWS_WITH(json(json::value_t::array).get<json::string_t>(),
"type must be string, but is array");
"[json.exception.type_error.302] type must be string, but is array");
CHECK_THROWS_WITH(json(json::value_t::boolean).get<json::string_t>(),
"type must be string, but is boolean");
"[json.exception.type_error.302] type must be string, but is boolean");
CHECK_THROWS_WITH(json(json::value_t::number_integer).get<json::string_t>(),
"type must be string, but is number");
"[json.exception.type_error.302] type must be string, but is number");
CHECK_THROWS_WITH(json(json::value_t::number_unsigned).get<json::string_t>(),
"type must be string, but is number");
"[json.exception.type_error.302] type must be string, but is number");
CHECK_THROWS_WITH(json(json::value_t::number_float).get<json::string_t>(),
"type must be string, but is number");
"[json.exception.type_error.302] type must be string, but is number");
}
}
@ -342,28 +342,28 @@ TEST_CASE("value conversion")
SECTION("exception in case of a non-string type")
{
CHECK_THROWS_AS(json(json::value_t::null).get<json::boolean_t>(), std::logic_error);
CHECK_THROWS_AS(json(json::value_t::object).get<json::boolean_t>(), std::logic_error);
CHECK_THROWS_AS(json(json::value_t::array).get<json::boolean_t>(), std::logic_error);
CHECK_THROWS_AS(json(json::value_t::string).get<json::boolean_t>(), std::logic_error);
CHECK_THROWS_AS(json(json::value_t::number_integer).get<json::boolean_t>(), std::logic_error);
CHECK_THROWS_AS(json(json::value_t::number_unsigned).get<json::boolean_t>(), std::logic_error);
CHECK_THROWS_AS(json(json::value_t::number_float).get<json::boolean_t>(), std::logic_error);
CHECK_THROWS_AS(json(json::value_t::null).get<json::boolean_t>(), json::type_error);
CHECK_THROWS_AS(json(json::value_t::object).get<json::boolean_t>(), json::type_error);
CHECK_THROWS_AS(json(json::value_t::array).get<json::boolean_t>(), json::type_error);
CHECK_THROWS_AS(json(json::value_t::string).get<json::boolean_t>(), json::type_error);
CHECK_THROWS_AS(json(json::value_t::number_integer).get<json::boolean_t>(), json::type_error);
CHECK_THROWS_AS(json(json::value_t::number_unsigned).get<json::boolean_t>(), json::type_error);
CHECK_THROWS_AS(json(json::value_t::number_float).get<json::boolean_t>(), json::type_error);
CHECK_THROWS_WITH(json(json::value_t::null).get<json::boolean_t>(),
"type must be boolean, but is null");
"[json.exception.type_error.302] type must be boolean, but is null");
CHECK_THROWS_WITH(json(json::value_t::object).get<json::boolean_t>(),
"type must be boolean, but is object");
"[json.exception.type_error.302] type must be boolean, but is object");
CHECK_THROWS_WITH(json(json::value_t::array).get<json::boolean_t>(),
"type must be boolean, but is array");
"[json.exception.type_error.302] type must be boolean, but is array");
CHECK_THROWS_WITH(json(json::value_t::string).get<json::boolean_t>(),
"type must be boolean, but is string");
"[json.exception.type_error.302] type must be boolean, but is string");
CHECK_THROWS_WITH(json(json::value_t::number_integer).get<json::boolean_t>(),
"type must be boolean, but is number");
"[json.exception.type_error.302] type must be boolean, but is number");
CHECK_THROWS_WITH(json(json::value_t::number_unsigned).get<json::boolean_t>(),
"type must be boolean, but is number");
"[json.exception.type_error.302] type must be boolean, but is number");
CHECK_THROWS_WITH(json(json::value_t::number_float).get<json::boolean_t>(),
"type must be boolean, but is number");
"[json.exception.type_error.302] type must be boolean, but is number");
}
}
@ -598,22 +598,22 @@ TEST_CASE("value conversion")
SECTION("exception in case of a non-number type")
{
CHECK_THROWS_AS(json(json::value_t::null).get<json::number_integer_t>(), std::logic_error);
CHECK_THROWS_AS(json(json::value_t::object).get<json::number_integer_t>(), std::logic_error);
CHECK_THROWS_AS(json(json::value_t::array).get<json::number_integer_t>(), std::logic_error);
CHECK_THROWS_AS(json(json::value_t::string).get<json::number_integer_t>(), std::logic_error);
CHECK_THROWS_AS(json(json::value_t::boolean).get<json::number_integer_t>(), std::logic_error);
CHECK_THROWS_AS(json(json::value_t::null).get<json::number_integer_t>(), json::type_error);
CHECK_THROWS_AS(json(json::value_t::object).get<json::number_integer_t>(), json::type_error);
CHECK_THROWS_AS(json(json::value_t::array).get<json::number_integer_t>(), json::type_error);
CHECK_THROWS_AS(json(json::value_t::string).get<json::number_integer_t>(), json::type_error);
CHECK_THROWS_AS(json(json::value_t::boolean).get<json::number_integer_t>(), json::type_error);
CHECK_THROWS_WITH(json(json::value_t::null).get<json::number_integer_t>(),
"type must be number, but is null");
"[json.exception.type_error.302] type must be number, but is null");
CHECK_THROWS_WITH(json(json::value_t::object).get<json::number_integer_t>(),
"type must be number, but is object");
"[json.exception.type_error.302] type must be number, but is object");
CHECK_THROWS_WITH(json(json::value_t::array).get<json::number_integer_t>(),
"type must be number, but is array");
"[json.exception.type_error.302] type must be number, but is array");
CHECK_THROWS_WITH(json(json::value_t::string).get<json::number_integer_t>(),
"type must be number, but is string");
"[json.exception.type_error.302] type must be number, but is string");
CHECK_THROWS_WITH(json(json::value_t::boolean).get<json::number_integer_t>(),
"type must be number, but is boolean");
"[json.exception.type_error.302] type must be number, but is boolean");
CHECK_NOTHROW(json(json::value_t::number_float).get<json::number_integer_t>());
CHECK_NOTHROW(json(json::value_t::number_float).get<json::number_unsigned_t>());
@ -857,22 +857,22 @@ TEST_CASE("value conversion")
SECTION("exception in case of a non-string type")
{
CHECK_THROWS_AS(json(json::value_t::null).get<json::number_float_t>(), std::logic_error);
CHECK_THROWS_AS(json(json::value_t::object).get<json::number_float_t>(), std::logic_error);
CHECK_THROWS_AS(json(json::value_t::array).get<json::number_float_t>(), std::logic_error);
CHECK_THROWS_AS(json(json::value_t::string).get<json::number_float_t>(), std::logic_error);
CHECK_THROWS_AS(json(json::value_t::boolean).get<json::number_float_t>(), std::logic_error);
CHECK_THROWS_AS(json(json::value_t::null).get<json::number_float_t>(), json::type_error);
CHECK_THROWS_AS(json(json::value_t::object).get<json::number_float_t>(), json::type_error);
CHECK_THROWS_AS(json(json::value_t::array).get<json::number_float_t>(), json::type_error);
CHECK_THROWS_AS(json(json::value_t::string).get<json::number_float_t>(), json::type_error);
CHECK_THROWS_AS(json(json::value_t::boolean).get<json::number_float_t>(), json::type_error);
CHECK_THROWS_WITH(json(json::value_t::null).get<json::number_float_t>(),
"type must be number, but is null");
"[json.exception.type_error.302] type must be number, but is null");
CHECK_THROWS_WITH(json(json::value_t::object).get<json::number_float_t>(),
"type must be number, but is object");
"[json.exception.type_error.302] type must be number, but is object");
CHECK_THROWS_WITH(json(json::value_t::array).get<json::number_float_t>(),
"type must be number, but is array");
"[json.exception.type_error.302] type must be number, but is array");
CHECK_THROWS_WITH(json(json::value_t::string).get<json::number_float_t>(),
"type must be number, but is string");
"[json.exception.type_error.302] type must be number, but is string");
CHECK_THROWS_WITH(json(json::value_t::boolean).get<json::number_float_t>(),
"type must be number, but is boolean");
"[json.exception.type_error.302] type must be number, but is boolean");
CHECK_NOTHROW(json(json::value_t::number_integer).get<json::number_float_t>());
CHECK_NOTHROW(json(json::value_t::number_unsigned).get<json::number_float_t>());
@ -954,8 +954,8 @@ TEST_CASE("value conversion")
SECTION("exception in case of a non-object type")
{
CHECK_THROWS_AS((json().get<std::map<std::string, int>>()), std::logic_error);
CHECK_THROWS_WITH((json().get<std::map<std::string, int>>()), "type must be object, but is null");
CHECK_THROWS_AS((json().get<std::map<std::string, int>>()), json::type_error);
CHECK_THROWS_WITH((json().get<std::map<std::string, int>>()), "[json.exception.type_error.302] type must be object, but is null");
}
}
@ -1023,17 +1023,17 @@ TEST_CASE("value conversion")
SECTION("exception in case of a non-object type")
{
CHECK_THROWS_AS((json().get<std::list<int>>()), std::logic_error);
CHECK_THROWS_AS((json().get<std::vector<int>>()), std::logic_error);
CHECK_THROWS_AS((json().get<std::vector<json>>()), std::logic_error);
CHECK_THROWS_AS((json().get<std::list<json>>()), std::logic_error);
CHECK_THROWS_AS((json().get<std::list<int>>()), json::type_error);
CHECK_THROWS_AS((json().get<std::vector<int>>()), json::type_error);
CHECK_THROWS_AS((json().get<std::vector<json>>()), json::type_error);
CHECK_THROWS_AS((json().get<std::list<json>>()), json::type_error);
// does type really must be an array? or it rather must not be null?
// that's what I thought when other test like this one broke
CHECK_THROWS_WITH((json().get<std::list<int>>()), "type must be array, but is null");
CHECK_THROWS_WITH((json().get<std::vector<int>>()), "type must be array, but is null");
CHECK_THROWS_WITH((json().get<std::vector<json>>()), "type must be array, but is null");
CHECK_THROWS_WITH((json().get<std::list<json>>()), "type must be array, but is null");
CHECK_THROWS_WITH((json().get<std::list<int>>()), "[json.exception.type_error.302] type must be array, but is null");
CHECK_THROWS_WITH((json().get<std::vector<int>>()), "[json.exception.type_error.302] type must be array, but is null");
CHECK_THROWS_WITH((json().get<std::vector<json>>()), "[json.exception.type_error.302] type must be array, but is null");
CHECK_THROWS_WITH((json().get<std::list<json>>()), "[json.exception.type_error.302] type must be array, but is null");
}
}
}