diff --git a/doc/examples/at_json_pointer.cpp b/doc/examples/at_json_pointer.cpp index 0665e608c..6d1617e65 100644 --- a/doc/examples/at_json_pointer.cpp +++ b/doc/examples/at_json_pointer.cpp @@ -32,4 +32,14 @@ int main() j.at("/array/1"_json_pointer) = 21; // output the changed array std::cout << j["array"] << '\n'; + + // try to use an invalid JSON pointer + try + { + auto ref = j.at("/number/foo"_json_pointer); + } + catch (json::out_of_range& e) + { + std::cout << e.what() << '\n'; + } } diff --git a/doc/examples/at_json_pointer.link b/doc/examples/at_json_pointer.link index 7a7efa268..454023032 100644 --- a/doc/examples/at_json_pointer.link +++ b/doc/examples/at_json_pointer.link @@ -1 +1 @@ -online \ No newline at end of file +online \ No newline at end of file diff --git a/doc/examples/at_json_pointer.output b/doc/examples/at_json_pointer.output index 11913c723..a45737ad2 100644 --- a/doc/examples/at_json_pointer.output +++ b/doc/examples/at_json_pointer.output @@ -4,3 +4,4 @@ 2 "bar" [1,21] +[json.exception.out_of_range.404] unresolved reference token 'foo' diff --git a/doc/examples/at_json_pointer_const.cpp b/doc/examples/at_json_pointer_const.cpp index e3cfc5154..dab1b39c4 100644 --- a/doc/examples/at_json_pointer_const.cpp +++ b/doc/examples/at_json_pointer_const.cpp @@ -20,4 +20,14 @@ int main() std::cout << j.at("/array"_json_pointer) << '\n'; // output element with JSON pointer "/array/1" std::cout << j.at("/array/1"_json_pointer) << '\n'; + + // try to use an invalid JSON pointer + try + { + auto ref = j.at("/number/foo"_json_pointer); + } + catch (json::out_of_range& e) + { + std::cout << e.what() << '\n'; + } } diff --git a/doc/examples/at_json_pointer_const.link b/doc/examples/at_json_pointer_const.link index 9057e0b27..70e7cf868 100644 --- a/doc/examples/at_json_pointer_const.link +++ b/doc/examples/at_json_pointer_const.link @@ -1 +1 @@ -online \ No newline at end of file +online \ No newline at end of file diff --git a/doc/examples/at_json_pointer_const.output b/doc/examples/at_json_pointer_const.output index 7b9306bbc..21712e867 100644 --- a/doc/examples/at_json_pointer_const.output +++ b/doc/examples/at_json_pointer_const.output @@ -2,3 +2,4 @@ "foo" [1,2] 2 +[json.exception.out_of_range.404] unresolved reference token 'foo' diff --git a/doc/examples/back.cpp b/doc/examples/back.cpp index 70516e588..45f9483c7 100644 --- a/doc/examples/back.cpp +++ b/doc/examples/back.cpp @@ -5,7 +5,6 @@ using json = nlohmann::json; int main() { // create JSON values - json j_null; json j_boolean = true; json j_number_integer = 17; json j_number_float = 23.42; @@ -16,7 +15,6 @@ int main() json j_string = "Hello, world"; // call back() - //std::cout << j_null.back() << '\n'; // would throw std::cout << j_boolean.back() << '\n'; std::cout << j_number_integer.back() << '\n'; std::cout << j_number_float.back() << '\n'; @@ -25,4 +23,15 @@ int main() std::cout << j_array.back() << '\n'; //std::cout << j_array_empty.back() << '\n'; // undefined behavior std::cout << j_string.back() << '\n'; + + // back() called on a null value + try + { + json j_null; + j_null.back(); + } + catch (json::invalid_iterator& e) + { + std::cout << e.what() << '\n'; + } } diff --git a/doc/examples/back.link b/doc/examples/back.link index 0b0097805..15b1102ba 100644 --- a/doc/examples/back.link +++ b/doc/examples/back.link @@ -1 +1 @@ -online \ No newline at end of file +online \ No newline at end of file diff --git a/doc/examples/back.output b/doc/examples/back.output index 159ba0fc5..2990dbf0e 100644 --- a/doc/examples/back.output +++ b/doc/examples/back.output @@ -4,3 +4,4 @@ true 2 16 "Hello, world" +[json.exception.invalid_iterator.214] cannot get value diff --git a/src/json.hpp b/src/json.hpp index 9fb7690f3..5a3e6c3a7 100644 --- a/src/json.hpp +++ b/src/json.hpp @@ -4411,7 +4411,8 @@ class basic_json assertions**). @post The JSON value remains unchanged. - @throw invalid_iterator.214 when called on `null` value. + @throw invalid_iterator.214 when called on a `null` value. See example + below. @liveexample{The following code shows an example for `back()`.,back} @@ -12769,10 +12770,18 @@ basic_json_parser_74: @complexity Constant. - @throw parse_error.106 if an array index begins with '0' - @throw parse_error.109 if an array index was not a number - @throw out_of_range.402 if the array index '-' is used - @throw out_of_range.404 if the JSON pointer can not be resolved + @throw parse_error.106 if an array index in the passed JSON pointer @a ptr + begins with '0' + + @throw parse_error.109 if an array index in the passed JSON pointer @a ptr + is not a number + + @throw out_of_range.402 if the array index `-` is used in the passed JSON + pointer @a ptr. As `at` provides checked access (and no elements are + implicitly inserted), the index `-` is always invalid. + + @throw out_of_range.404 if the JSON pointer @a ptr can not be resolved; + see example below. @liveexample{The behavior is shown in the example.,at_json_pointer} @@ -12795,10 +12804,18 @@ basic_json_parser_74: @complexity Constant. - @throw parse_error.106 if an array index begins with '0' - @throw parse_error.109 if an array index was not a number - @throw out_of_range.402 if the array index '-' is used - @throw out_of_range.404 if the JSON pointer can not be resolved + @throw parse_error.106 if an array index in the passed JSON pointer @a ptr + begins with '0' + + @throw parse_error.109 if an array index in the passed JSON pointer @a ptr + is not a number + + @throw out_of_range.402 if the array index `-` is used in the passed JSON + pointer @a ptr. As `at` provides checked access (and no elements are + implicitly inserted), the index `-` is always invalid. + + @throw out_of_range.404 if the JSON pointer @a ptr can not be resolved; + see example below. @liveexample{The behavior is shown in the example.,at_json_pointer_const} diff --git a/src/json.hpp.re2c b/src/json.hpp.re2c index 22e128412..289e3f10a 100644 --- a/src/json.hpp.re2c +++ b/src/json.hpp.re2c @@ -4411,7 +4411,8 @@ class basic_json assertions**). @post The JSON value remains unchanged. - @throw invalid_iterator.214 when called on `null` value. + @throw invalid_iterator.214 when called on a `null` value. See example + below. @liveexample{The following code shows an example for `back()`.,back} @@ -11802,10 +11803,18 @@ class basic_json @complexity Constant. - @throw parse_error.106 if an array index begins with '0' - @throw parse_error.109 if an array index was not a number - @throw out_of_range.402 if the array index '-' is used - @throw out_of_range.404 if the JSON pointer can not be resolved + @throw parse_error.106 if an array index in the passed JSON pointer @a ptr + begins with '0' + + @throw parse_error.109 if an array index in the passed JSON pointer @a ptr + is not a number + + @throw out_of_range.402 if the array index `-` is used in the passed JSON + pointer @a ptr. As `at` provides checked access (and no elements are + implicitly inserted), the index `-` is always invalid. + + @throw out_of_range.404 if the JSON pointer @a ptr can not be resolved; + see example below. @liveexample{The behavior is shown in the example.,at_json_pointer} @@ -11828,10 +11837,18 @@ class basic_json @complexity Constant. - @throw parse_error.106 if an array index begins with '0' - @throw parse_error.109 if an array index was not a number - @throw out_of_range.402 if the array index '-' is used - @throw out_of_range.404 if the JSON pointer can not be resolved + @throw parse_error.106 if an array index in the passed JSON pointer @a ptr + begins with '0' + + @throw parse_error.109 if an array index in the passed JSON pointer @a ptr + is not a number + + @throw out_of_range.402 if the array index `-` is used in the passed JSON + pointer @a ptr. As `at` provides checked access (and no elements are + implicitly inserted), the index `-` is always invalid. + + @throw out_of_range.404 if the JSON pointer @a ptr can not be resolved; + see example below. @liveexample{The behavior is shown in the example.,at_json_pointer_const}