Skip to content

Commit

Permalink
fixed #70
Browse files Browse the repository at this point in the history
  • Loading branch information
nlohmann committed May 6, 2015
1 parent 1580eee commit 17609f2
Show file tree
Hide file tree
Showing 4 changed files with 55 additions and 5 deletions.
3 changes: 2 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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.
- [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.
- [Eric Cornelius](https://github.com/EricMCornelius) pointed out a bug in the handling with NaN and infinity values.

Thanks a lot for helping out!

Expand All @@ -395,7 +396,7 @@ $ make
$ ./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).
18 changes: 16 additions & 2 deletions src/json.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -431,7 +431,14 @@ class basic_json
/// create a floating-point number (explicit)
inline basic_json(const number_float_t& 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)
template<typename T, typename = typename
Expand All @@ -441,7 +448,14 @@ class basic_json
>
inline basic_json(const T value) noexcept
: 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
inline basic_json(list_init_t init, bool type_deduction = true,
Expand Down
18 changes: 16 additions & 2 deletions src/json.hpp.re2c
Original file line number Diff line number Diff line change
Expand Up @@ -431,7 +431,14 @@ class basic_json
/// create a floating-point number (explicit)
inline basic_json(const number_float_t& 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)
template<typename T, typename = typename
Expand All @@ -441,7 +448,14 @@ class basic_json
>
inline basic_json(const T value) noexcept
: 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
inline basic_json(list_init_t init, bool type_deduction = true,
Expand Down
21 changes: 21 additions & 0 deletions test/unit.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -8450,4 +8450,25 @@ TEST_CASE("regression tests")
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());
}
}
}

0 comments on commit 17609f2

Please sign in to comment.