mirror of
https://github.com/nlohmann/json.git
synced 2025-07-24 02:21:01 +03:00
- If an overflow occurs during parsing a number from a JSON text, an exception (std::out_of_range for the moment, to be replaced by a user-defined exception #244) is thrown so that the overflow is detected early and roundtripping is guaranteed. - NaN and INF floating-point values can be stored in a JSON value and are not replaced by null. That is, the basic_json class behaves like double in this regard (no exception occurs). However, NaN and INF are serialized to “null”. - Adjusted test cases appropriately.
This commit is contained in:
24
src/json.hpp
24
src/json.hpp
@ -263,16 +263,8 @@ struct external_constructor<value_t::number_float>
|
||||
template<typename BasicJsonType>
|
||||
static void construct(BasicJsonType& j, typename BasicJsonType::number_float_t val) noexcept
|
||||
{
|
||||
// replace infinity and NAN by null
|
||||
if (not std::isfinite(val))
|
||||
{
|
||||
j = BasicJsonType{};
|
||||
}
|
||||
else
|
||||
{
|
||||
j.m_type = value_t::number_float;
|
||||
j.m_value = val;
|
||||
}
|
||||
j.m_type = value_t::number_float;
|
||||
j.m_value = val;
|
||||
j.assert_invariant();
|
||||
}
|
||||
};
|
||||
@ -6653,6 +6645,13 @@ class basic_json
|
||||
*/
|
||||
void dump_float(number_float_t x)
|
||||
{
|
||||
// NaN / inf
|
||||
if (not std::isfinite(x) or std::isnan(x))
|
||||
{
|
||||
o.write("null", 4);
|
||||
return;
|
||||
}
|
||||
|
||||
// special case for 0.0 and -0.0
|
||||
if (x == 0)
|
||||
{
|
||||
@ -11425,11 +11424,10 @@ basic_json_parser_74:
|
||||
result.m_type = value_t::number_float;
|
||||
result.m_value = val;
|
||||
|
||||
// replace infinity and NAN by null
|
||||
// throw in case of infinity or NAN
|
||||
if (not std::isfinite(result.m_value.number_float))
|
||||
{
|
||||
result.m_type = value_t::null;
|
||||
result.m_value = basic_json::json_value();
|
||||
JSON_THROW(std::out_of_range("number overflow: " + get_token_string()));
|
||||
}
|
||||
|
||||
return true;
|
||||
|
Reference in New Issue
Block a user