json_view: get<T>() of a boolean throws for the exact number types, as basic_json does (#5992)
* json_view: get<T>() of a boolean throws for number_integer_t, number_unsigned_t and number_float_t like basic_json (#5988) basic_json::get<T>() of a boolean throws type_error.302 for exactly number_integer_t, number_unsigned_t and number_float_t (dedicated from_json() overloads) and gives 0/1 for every other arithmetic type. The view returned 0/1 for all of them, contradicting its documentation. The view now throws the same exception (same message, with path/bytes decoration) for these three types. basic_json is unchanged; its behavior is documented and pinned by a test. Signed-off-by: Niels Lohmann <mail@nlohmann.me> * tests: skip throwing json_view bool get<T>() combinations without exceptions (#5988) check_boolean_get_parity() called get<T>() for number_integer_t, number_unsigned_t and number_float_t in its JSON_NOEXCEPTION branch, but a boolean throws type_error.302 for exactly these types, so JSON_THROW aborted test-json_view_cpp11/cpp17 in ci_test_noexceptions. Skip these combinations there; the throwing checks stay in the exceptions-enabled build. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01N5NasAgbDGG28MqXjQvLqp Signed-off-by: Claude <noreply@anthropic.com> * Expect the byte range of the root in the json_view boolean get<T>() test With JSON_DIAGNOSTIC_POSITIONS, the type_error.302 message starts with "(bytes 0-4)"; build the expected text with json_view_test::message(), as the other tests of the root value do. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01N5NasAgbDGG28MqXjQvLqp Signed-off-by: Claude <noreply@anthropic.com> --------- Signed-off-by: Niels Lohmann <mail@nlohmann.me> Signed-off-by: Claude <noreply@anthropic.com> Co-authored-by: Claude <noreply@anthropic.com>
This commit is contained in:
1 parent
494a74c242
commit
bfe69c73c2
6 files changed
+185
-7
No files matched your search
@@ -1260,6 +1260,44 @@ TEST_CASE("value conversion")
|
||||
#undef JSON_HAS_CPP_17
|
||||
#endif
|
||||
|
||||
TEST_CASE("get<T>() of a boolean (#5988)")
|
||||
{
|
||||
// a boolean converts to 0/1 for every arithmetic type except number_integer_t, number_unsigned_t and
|
||||
// number_float_t, whose dedicated from_json() overloads throw
|
||||
const json t = true;
|
||||
const json f = false;
|
||||
|
||||
SECTION("0 and 1")
|
||||
{
|
||||
// long and unsigned long are left out: they are std::int64_t and std::uint64_t on some platforms
|
||||
CHECK(t.get<bool>() == true);
|
||||
CHECK(f.get<bool>() == false);
|
||||
CHECK(t.get<char>() == 1);
|
||||
CHECK(t.get<signed char>() == 1);
|
||||
CHECK(t.get<unsigned char>() == 1);
|
||||
CHECK(t.get<short>() == 1); // NOLINT(google-runtime-int)
|
||||
CHECK(t.get<unsigned short>() == 1); // NOLINT(google-runtime-int)
|
||||
CHECK(t.get<int>() == 1);
|
||||
CHECK(t.get<unsigned>() == 1);
|
||||
CHECK(t.get<float>() == 1.0F);
|
||||
CHECK(t.get<long double>() == 1.0L);
|
||||
CHECK(f.get<int>() == 0);
|
||||
CHECK(f.get<float>() == 0.0F);
|
||||
}
|
||||
|
||||
#if !defined(JSON_NOEXCEPTION)
|
||||
SECTION("number_integer_t, number_unsigned_t and number_float_t throw")
|
||||
{
|
||||
CHECK_THROWS_WITH_AS(t.get<json::number_integer_t>(), "[json.exception.type_error.302] type must be number, but is boolean", json::type_error&);
|
||||
CHECK_THROWS_WITH_AS(t.get<json::number_unsigned_t>(), "[json.exception.type_error.302] type must be number, but is boolean", json::type_error&);
|
||||
CHECK_THROWS_WITH_AS(t.get<json::number_float_t>(), "[json.exception.type_error.302] type must be number, but is boolean", json::type_error&);
|
||||
CHECK_THROWS_WITH_AS(f.get<json::number_integer_t>(), "[json.exception.type_error.302] type must be number, but is boolean", json::type_error&);
|
||||
CHECK_THROWS_WITH_AS(f.get<json::number_unsigned_t>(), "[json.exception.type_error.302] type must be number, but is boolean", json::type_error&);
|
||||
CHECK_THROWS_WITH_AS(f.get<json::number_float_t>(), "[json.exception.type_error.302] type must be number, but is boolean", json::type_error&);
|
||||
}
|
||||
#endif
|
||||
}
|
||||
|
||||
#ifdef JSON_HAS_CPP_14
|
||||
#undef JSON_HAS_CPP_14
|
||||
#endif
|
||||
|
||||
Reference in new issue
Block a user