This commit is contained in:
Niels 2015-05-06 19:52:12 +02:00
parent 1580eee4ed
commit 17609f244d
4 changed files with 55 additions and 5 deletions

View file

@ -383,6 +383,7 @@ I deeply appreciate the help of the following people.
- [Aaron Burghardt](https://github.com/aburgh) implemented code to parse streams incrementally. Furthermore, he greatly improved the parser class by allowing the definition of a filter function to discard undesired elements while parsing. - [Aaron Burghardt](https://github.com/aburgh) implemented code to parse streams incrementally. Furthermore, he greatly improved the parser class by allowing the definition of a filter function to discard undesired elements while parsing.
- [Daniel Kopeček](https://github.com/dkopecek) fixed a bug in the compilation with GCC 5.0. - [Daniel Kopeček](https://github.com/dkopecek) fixed a bug in the compilation with GCC 5.0.
- [Florian Weber](https://github.com/Florianjw) fixed a bug in and improved the performance of the comparison operators. - [Florian Weber](https://github.com/Florianjw) fixed a bug in and improved the performance of the comparison operators.
- [Eric Cornelius](https://github.com/EricMCornelius) pointed out a bug in the handling with NaN and infinity values.
Thanks a lot for helping out! Thanks a lot for helping out!
@ -395,7 +396,7 @@ $ make
$ ./json_unit $ ./json_unit
=============================================================================== ===============================================================================
All tests passed (4558 assertions in 19 test cases) All tests passed (4673 assertions in 19 test cases)
``` ```
For more information, have a look at the file [.travis.yml](https://github.com/nlohmann/json/blob/master/.travis.yml). For more information, have a look at the file [.travis.yml](https://github.com/nlohmann/json/blob/master/.travis.yml).

View file

@ -431,7 +431,14 @@ class basic_json
/// create a floating-point number (explicit) /// create a floating-point number (explicit)
inline basic_json(const number_float_t& value) inline basic_json(const number_float_t& value)
: m_type(value_t::number_float), m_value(value) : m_type(value_t::number_float), m_value(value)
{} {
// replace infinity and NAN by null
if (not std::isfinite(value))
{
m_type = value_t::null;
m_value = json_value();
}
}
/// create a floating-point number (implicit) /// create a floating-point number (implicit)
template<typename T, typename = typename template<typename T, typename = typename
@ -441,7 +448,14 @@ class basic_json
> >
inline basic_json(const T value) noexcept inline basic_json(const T value) noexcept
: m_type(value_t::number_float), m_value(number_float_t(value)) : m_type(value_t::number_float), m_value(number_float_t(value))
{} {
// replace infinity and NAN by null
if (not std::isfinite(value))
{
m_type = value_t::null;
m_value = json_value();
}
}
/// create a container (array or object) from an initializer list /// create a container (array or object) from an initializer list
inline basic_json(list_init_t init, bool type_deduction = true, inline basic_json(list_init_t init, bool type_deduction = true,

View file

@ -431,7 +431,14 @@ class basic_json
/// create a floating-point number (explicit) /// create a floating-point number (explicit)
inline basic_json(const number_float_t& value) inline basic_json(const number_float_t& value)
: m_type(value_t::number_float), m_value(value) : m_type(value_t::number_float), m_value(value)
{} {
// replace infinity and NAN by null
if (not std::isfinite(value))
{
m_type = value_t::null;
m_value = json_value();
}
}
/// create a floating-point number (implicit) /// create a floating-point number (implicit)
template<typename T, typename = typename template<typename T, typename = typename
@ -441,7 +448,14 @@ class basic_json
> >
inline basic_json(const T value) noexcept inline basic_json(const T value) noexcept
: m_type(value_t::number_float), m_value(number_float_t(value)) : m_type(value_t::number_float), m_value(number_float_t(value))
{} {
// replace infinity and NAN by null
if (not std::isfinite(value))
{
m_type = value_t::null;
m_value = json_value();
}
}
/// create a container (array or object) from an initializer list /// create a container (array or object) from an initializer list
inline basic_json(list_init_t init, bool type_deduction = true, inline basic_json(list_init_t init, bool type_deduction = true,

View file

@ -8450,4 +8450,25 @@ TEST_CASE("regression tests")
CHECK(j == R"(["\"foo\""])"_json); CHECK(j == R"(["\"foo\""])"_json);
} }
} }
SECTION("issue #70 - Handle infinity and NaN cases")
{
SECTION("NAN value")
{
CHECK(json(NAN) == json());
}
SECTION("overflows")
{
CHECK(json(std::exp(800)) == json());
CHECK(json(HUGE_VALF) == json());
CHECK(json(HUGE_VAL) == json());
CHECK(json(HUGE_VALL) == json());
}
SECTION("infinity")
{
CHECK(json(INFINITY) == json());
}
}
} }