Extend value to arrays when using JSON pointers (#5223)

*  extend value to arrays when using JSON pointers

Signed-off-by: Niels Lohmann <mail@nlohmann.me>

* 💚 avoid exceptions

Signed-off-by: Niels Lohmann <mail@nlohmann.me>

* 💚 avoid exceptions

Signed-off-by: Niels Lohmann <mail@nlohmann.me>

---------

Signed-off-by: Niels Lohmann <mail@nlohmann.me>
This commit is contained in:
Niels Lohmann
2026-07-02 06:35:35 +02:00
committed by GitHub
parent 730b57775d
commit ca49ab6123
7 changed files with 271 additions and 53 deletions
+1 -1
View File
@@ -55,5 +55,5 @@ int main()
// use every result so the references cannot be optimized away
return (a == 1 && last == 3 && b == 2 && lit.size() == 3
&& m.size() == 1 && !dumped.empty() && !os.str().empty())
? 0 : 1;
? 0 : 1;
}
+49 -8
View File
@@ -419,14 +419,6 @@ TEST_CASE_TEMPLATE("element access 2", Json, nlohmann::json, nlohmann::ordered_j
CHECK_THROWS_WITH_AS(j_nonobject_const.value("/foo"_json_pointer, 1), "[json.exception.type_error.306] cannot use value() with string", typename Json::type_error&);
}
SECTION("array")
{
Json j_nonobject(Json::value_t::array);
const Json j_nonobject_const(Json::value_t::array);
CHECK_THROWS_WITH_AS(j_nonobject.value("/foo"_json_pointer, 1), "[json.exception.type_error.306] cannot use value() with array", typename Json::type_error&);
CHECK_THROWS_WITH_AS(j_nonobject_const.value("/foo"_json_pointer, 1), "[json.exception.type_error.306] cannot use value() with array", typename Json::type_error&);
}
SECTION("number (integer)")
{
Json j_nonobject(Json::value_t::number_integer);
@@ -451,6 +443,55 @@ TEST_CASE_TEMPLATE("element access 2", Json, nlohmann::json, nlohmann::ordered_j
CHECK_THROWS_WITH_AS(j_nonobject_const.value("/foo"_json_pointer, 1), "[json.exception.type_error.306] cannot use value() with number", typename Json::type_error&);
}
}
SECTION("access on array type")
{
Json j_array = Json::array({j});
const Json j_array_const = Json::array({j});
CHECK(j_array.value("/0/integer"_json_pointer, 2) == 1);
CHECK(j_array.value("/0/integer"_json_pointer, 1.0) == Approx(1));
CHECK(j_array.value("/0/unsigned"_json_pointer, 2) == 1u);
CHECK(j_array.value("/0/unsigned"_json_pointer, 1.0) == Approx(1u));
CHECK(j_array.value("/0/null"_json_pointer, Json(1)) == Json());
CHECK(j_array.value("/0/boolean"_json_pointer, false) == true);
CHECK(j_array.value("/0/string"_json_pointer, "bar") == "hello world");
CHECK(j_array.value("/0/string"_json_pointer, std::string("bar")) == "hello world");
CHECK(j_array.value("/0/floating"_json_pointer, 12.34) == Approx(42.23));
CHECK(j_array.value("/0/floating"_json_pointer, 12) == 42);
CHECK(j_array.value("/0/object"_json_pointer, Json({{"foo", "bar"}})) == Json::object());
CHECK(j_array.value("/0/array"_json_pointer, Json({10, 100})) == Json({1, 2, 3}));
CHECK(j_array_const.value("/0/integer"_json_pointer, 2) == 1);
CHECK(j_array_const.value("/0/integer"_json_pointer, 1.0) == Approx(1));
CHECK(j_array_const.value("/0/unsigned"_json_pointer, 2) == 1u);
CHECK(j_array_const.value("/0/unsigned"_json_pointer, 1.0) == Approx(1u));
CHECK(j_array_const.value("/0/boolean"_json_pointer, false) == true);
CHECK(j_array_const.value("/0/string"_json_pointer, "bar") == "hello world");
CHECK(j_array_const.value("/0/string"_json_pointer, std::string("bar")) == "hello world");
CHECK(j_array_const.value("/0/floating"_json_pointer, 12.34) == Approx(42.23));
CHECK(j_array_const.value("/0/floating"_json_pointer, 12) == 42);
CHECK(j_array_const.value("/0/object"_json_pointer, Json({{"foo", "bar"}})) == Json::object());
CHECK(j_array_const.value("/0/array"_json_pointer, Json({10, 100})) == Json({1, 2, 3}));
// Test out-of-range array index
CHECK(j_array.value("/10"_json_pointer, 42) == 42);
CHECK(j_array_const.value("/10"_json_pointer, 42) == 42);
// Test "-" index (append position is invalid)
CHECK(j_array.value("/-"_json_pointer, 42) == 42);
CHECK(j_array_const.value("/-"_json_pointer, 42) == 42);
#if !defined(JSON_NOEXCEPTION)
// Test malformed index (non-numeric) throws parse_error
CHECK_THROWS_WITH_AS(j_array.value("/foo"_json_pointer, 1), "[json.exception.parse_error.109] parse error: array index 'foo' is not a number", typename Json::parse_error&);
CHECK_THROWS_WITH_AS(j_array_const.value("/foo"_json_pointer, 1), "[json.exception.parse_error.109] parse error: array index 'foo' is not a number", typename Json::parse_error&);
// Test leading-zero index throws parse_error
CHECK_THROWS_WITH_AS(j_array.value("/01"_json_pointer, 1), "[json.exception.parse_error.106] parse error: array index '01' must not begin with '0'", typename Json::parse_error&);
CHECK_THROWS_WITH_AS(j_array_const.value("/01"_json_pointer, 1), "[json.exception.parse_error.106] parse error: array index '01' must not begin with '0'", typename Json::parse_error&);
#endif
}
}
}