add explicit conversion of null into float NaN

This commit is contained in:
Jonas Wittbrodt 2021-08-25 12:53:49 +02:00
parent 1fd2213fd2
commit 5c3738b9b1
2 changed files with 55 additions and 8 deletions

View File

@ -57,8 +57,21 @@ void get_arithmetic_value(const BasicJsonType& j, ArithmeticType& val)
val = static_cast<ArithmeticType>(*j.template get_ptr<const typename BasicJsonType::number_float_t*>()); val = static_cast<ArithmeticType>(*j.template get_ptr<const typename BasicJsonType::number_float_t*>());
break; break;
} }
case value_t::null: case value_t::null:
{
if(std::numeric_limits<ArithmeticType>::has_quiet_NaN)
{
val = std::numeric_limits<ArithmeticType>::quiet_NaN();
break;
}
else if(std::numeric_limits<ArithmeticType>::has_signaling_NaN)
{
val = std::numeric_limits<ArithmeticType>::signaling_NaN();
break;
}
// [[fallthrough]];
}
case value_t::object: case value_t::object:
case value_t::array: case value_t::array:
case value_t::string: case value_t::string:
@ -349,8 +362,21 @@ void from_json(const BasicJsonType& j, ArithmeticType& val)
val = static_cast<ArithmeticType>(*j.template get_ptr<const typename BasicJsonType::boolean_t*>()); val = static_cast<ArithmeticType>(*j.template get_ptr<const typename BasicJsonType::boolean_t*>());
break; break;
} }
case value_t::null: case value_t::null:
{
if(std::numeric_limits<ArithmeticType>::has_quiet_NaN)
{
val = std::numeric_limits<ArithmeticType>::quiet_NaN();
break;
}
else if(std::numeric_limits<ArithmeticType>::has_signaling_NaN)
{
val = std::numeric_limits<ArithmeticType>::signaling_NaN();
break;
}
// [[fallthrough]];
}
case value_t::object: case value_t::object:
case value_t::array: case value_t::array:
case value_t::string: case value_t::string:

View File

@ -1207,10 +1207,31 @@ TEST_CASE("value conversion")
CHECK(json(n).m_value.number_float == Approx(j.m_value.number_float)); CHECK(json(n).m_value.number_float == Approx(j.m_value.number_float));
} }
SECTION("exception in case of a non-string type") SECTION("null as NaN")
{ {
CHECK_THROWS_AS(json(json::value_t::null).get<json::number_float_t>(), json jNull(nullptr);
json::type_error&); SECTION("number_float_t")
{
auto n = jNull.get<json::number_float_t>();
CHECK(std::isnan(n));
}
SECTION("float")
{
auto n = jNull.get<float>();
CHECK(std::isnan(n));
}
SECTION("double")
{
auto n = jNull.get<double>();
CHECK(std::isnan(n));
}
}
SECTION("exception in case of a non-numeric type")
{
// 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>(), CHECK_THROWS_AS(json(json::value_t::object).get<json::number_float_t>(),
json::type_error&); json::type_error&);
CHECK_THROWS_AS(json(json::value_t::array).get<json::number_float_t>(), CHECK_THROWS_AS(json(json::value_t::array).get<json::number_float_t>(),
@ -1220,9 +1241,9 @@ TEST_CASE("value conversion")
CHECK_THROWS_AS(json(json::value_t::boolean).get<json::number_float_t>(), CHECK_THROWS_AS(json(json::value_t::boolean).get<json::number_float_t>(),
json::type_error&); json::type_error&);
CHECK_THROWS_WITH( // CHECK_THROWS_WITH(
json(json::value_t::null).get<json::number_float_t>(), // json(json::value_t::null).get<json::number_float_t>(),
"[json.exception.type_error.302] type must be number, but is null"); // "[json.exception.type_error.302] type must be number, but is null");
CHECK_THROWS_WITH( CHECK_THROWS_WITH(
json(json::value_t::object).get<json::number_float_t>(), json(json::value_t::object).get<json::number_float_t>(),
"[json.exception.type_error.302] type must be number, but is object"); "[json.exception.type_error.302] type must be number, but is object");