From e07686f0c72fd7a13ec1b8e3a13aa0b2d2f592c8 Mon Sep 17 00:00:00 2001 From: chenguoping Date: Wed, 25 Mar 2020 15:56:45 +0800 Subject: [PATCH] update array_index() and add testcases --- include/nlohmann/detail/json_pointer.hpp | 4 ++-- single_include/nlohmann/json.hpp | 4 ++-- test/src/unit-json_pointer.cpp | 28 ++++++++++++++++++++++++ 3 files changed, 32 insertions(+), 4 deletions(-) diff --git a/include/nlohmann/detail/json_pointer.hpp b/include/nlohmann/detail/json_pointer.hpp index 0acf924e..ebf3ca8a 100644 --- a/include/nlohmann/detail/json_pointer.hpp +++ b/include/nlohmann/detail/json_pointer.hpp @@ -349,9 +349,9 @@ class json_pointer { res = std::stoi(s, &processed_chars); } - JSON_CATCH(std::invalid_argument&) + JSON_CATCH(std::out_of_range&) { - JSON_THROW(detail::parse_error::create(109, 0, "array index '" + s + "' is not a number")); + JSON_THROW(detail::out_of_range::create(404, "unresolved reference token '" + s + "'")); } // check if the string was completely read diff --git a/single_include/nlohmann/json.hpp b/single_include/nlohmann/json.hpp index 5c93426e..686cd52c 100644 --- a/single_include/nlohmann/json.hpp +++ b/single_include/nlohmann/json.hpp @@ -10423,9 +10423,9 @@ class json_pointer { res = std::stoi(s, &processed_chars); } - JSON_CATCH(std::invalid_argument&) + JSON_CATCH(std::out_of_range&) { - JSON_THROW(detail::parse_error::create(109, 0, "array index '" + s + "' is not a number")); + JSON_THROW(detail::out_of_range::create(404, "unresolved reference token '" + s + "'")); } // check if the string was completely read diff --git a/test/src/unit-json_pointer.cpp b/test/src/unit-json_pointer.cpp index 3cfd8dad..1655a458 100644 --- a/test/src/unit-json_pointer.cpp +++ b/test/src/unit-json_pointer.cpp @@ -332,6 +332,34 @@ TEST_CASE("JSON pointers") CHECK_THROWS_WITH(j_const.at("/one"_json_pointer) == 1, "[json.exception.parse_error.109] parse error: array index 'one' is not a number"); + CHECK_THROWS_AS(j["/+1"_json_pointer] = 1, json::parse_error&); + CHECK_THROWS_WITH(j["/+1"_json_pointer] = 1, + "[json.exception.parse_error.109] parse error: array index '+1' is not a number"); + CHECK_THROWS_AS(j_const["/+1"_json_pointer] == 1, json::parse_error&); + CHECK_THROWS_WITH(j_const["/+1"_json_pointer] == 1, + "[json.exception.parse_error.109] parse error: array index '+1' is not a number"); + + CHECK_THROWS_AS(j["/1+1"_json_pointer] = 1, json::out_of_range&); + CHECK_THROWS_WITH(j["/1+1"_json_pointer] = 1, + "[json.exception.out_of_range.404] unresolved reference token '1+1'"); + CHECK_THROWS_AS(j_const["/1+1"_json_pointer] == 1, json::out_of_range&); + CHECK_THROWS_WITH(j_const["/1+1"_json_pointer] == 1, + "[json.exception.out_of_range.404] unresolved reference token '1+1'"); + + CHECK_THROWS_AS(j["/111111111111111111111111"_json_pointer] = 1, json::out_of_range&); + CHECK_THROWS_WITH(j["/111111111111111111111111"_json_pointer] = 1, + "[json.exception.out_of_range.404] unresolved reference token '111111111111111111111111'"); + CHECK_THROWS_AS(j_const["/111111111111111111111111"_json_pointer] == 1, json::out_of_range&); + CHECK_THROWS_WITH(j_const["/111111111111111111111111"_json_pointer] == 1, + "[json.exception.out_of_range.404] unresolved reference token '111111111111111111111111'"); + + CHECK_THROWS_AS(j.at("/one"_json_pointer) = 1, json::parse_error&); + CHECK_THROWS_WITH(j.at("/one"_json_pointer) = 1, + "[json.exception.parse_error.109] parse error: array index 'one' is not a number"); + CHECK_THROWS_AS(j_const.at("/one"_json_pointer) == 1, json::parse_error&); + CHECK_THROWS_WITH(j_const.at("/one"_json_pointer) == 1, + "[json.exception.parse_error.109] parse error: array index 'one' is not a number"); + CHECK_THROWS_AS(j.contains("/one"_json_pointer), json::parse_error&); CHECK_THROWS_WITH(j.contains("/one"_json_pointer), "[json.exception.parse_error.109] parse error: array index 'one' is not a number");