diff --git a/include/nlohmann/detail/output/binary_writer.hpp b/include/nlohmann/detail/output/binary_writer.hpp index ee59af149..451bb4f15 100644 --- a/include/nlohmann/detail/output/binary_writer.hpp +++ b/include/nlohmann/detail/output/binary_writer.hpp @@ -1593,6 +1593,14 @@ class binary_writer { oa->write_character(to_char_type(0xFD)); } + else if (std::isnan(j.m_value.number_float)) + { + oa->write_character(to_char_type(0x8E)); + oa->write_character(to_char_type(0x7F)); + oa->write_character(to_char_type(0x80)); + oa->write_character(to_char_type(0x00)); + oa->write_character(to_char_type(0x01)); + } else { // write float with prefix @@ -1839,9 +1847,9 @@ class binary_writer #pragma GCC diagnostic push #pragma GCC diagnostic ignored "-Wfloat-equal" #endif - if (static_cast(n) >= static_cast(std::numeric_limits::lowest()) && - static_cast(n) <= static_cast((std::numeric_limits::max)()) && - static_cast(static_cast(n)) == static_cast(n)) + if (std::isnan(n) || std::isinf(n) || (static_cast(n) >= static_cast(std::numeric_limits::lowest()) && + static_cast(n) <= static_cast((std::numeric_limits::max)()) && + static_cast(static_cast(n)) == static_cast(n))) { switch (format) { diff --git a/single_include/nlohmann/json.hpp b/single_include/nlohmann/json.hpp index eae9dedb4..1c13057e1 100644 --- a/single_include/nlohmann/json.hpp +++ b/single_include/nlohmann/json.hpp @@ -15167,6 +15167,14 @@ class binary_writer { oa->write_character(to_char_type(0xFD)); } + else if (std::isnan(j.m_value.number_float)) + { + oa->write_character(to_char_type(0x8E)); + oa->write_character(to_char_type(0x7F)); + oa->write_character(to_char_type(0x80)); + oa->write_character(to_char_type(0x00)); + oa->write_character(to_char_type(0x01)); + } else { // write float with prefix @@ -15413,9 +15421,9 @@ class binary_writer #pragma GCC diagnostic push #pragma GCC diagnostic ignored "-Wfloat-equal" #endif - if (static_cast(n) >= static_cast(std::numeric_limits::lowest()) && - static_cast(n) <= static_cast((std::numeric_limits::max)()) && - static_cast(static_cast(n)) == static_cast(n)) + if (std::isnan(n) || std::isinf(n) || (static_cast(n) >= static_cast(std::numeric_limits::lowest()) && + static_cast(n) <= static_cast((std::numeric_limits::max)()) && + static_cast(static_cast(n)) == static_cast(n))) { switch (format) { diff --git a/test/src/unit-bon8.cpp b/test/src/unit-bon8.cpp index 9f6848cf6..06cf57073 100644 --- a/test/src/unit-bon8.cpp +++ b/test/src/unit-bon8.cpp @@ -468,6 +468,30 @@ TEST_CASE("BON8") const auto result = json::to_bon8(j); CHECK(result == expected); } + + SECTION("NAN") + { + json j = NAN; + std::vector expected = {0x8E, 0x7F, 0x80, 0x00, 0x01}; + const auto result = json::to_bon8(j); + CHECK(result == expected); + } + + SECTION("infinity") + { + json j = INFINITY; + std::vector expected = {0x8E, 0x7F, 0x80, 0x00, 0x00}; + const auto result = json::to_bon8(j); + CHECK(result == expected); + } + + SECTION("-infinity") + { + json j = -INFINITY; + std::vector expected = {0x8E, 0xFF, 0x80, 0x00, 0x00}; + const auto result = json::to_bon8(j); + CHECK(result == expected); + } } SECTION("floats")