From 1885f4bc5f7a223ebdbd83cbf1177e01cc984353 Mon Sep 17 00:00:00 2001 From: Kirill Lokotkov Date: Wed, 25 Jan 2023 18:33:40 +0300 Subject: [PATCH 1/4] Fix for printing long doubles bug in dump_float When you use long double as a floating point type with the current version of this file and try to dump json it prints trash instead of actual number. This if-else fixes the problem. On using long double you just need to add an 'L' modifier before 'g' in format string. --- include/nlohmann/detail/output/serializer.hpp | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/include/nlohmann/detail/output/serializer.hpp b/include/nlohmann/detail/output/serializer.hpp index 02bfaa548..c3e8d291d 100644 --- a/include/nlohmann/detail/output/serializer.hpp +++ b/include/nlohmann/detail/output/serializer.hpp @@ -832,7 +832,12 @@ class serializer // the actual conversion // NOLINTNEXTLINE(cppcoreguidelines-pro-type-vararg,hicpp-vararg) - std::ptrdiff_t len = (std::snprintf)(number_buffer.data(), number_buffer.size(), "%.*g", d, x); + std::ptrdiff_t len; + if constexpr (std::is_same_v) { + len = (std::snprintf)(number_buffer.data(), number_buffer.size(), "%.*Lg", d, x); + } else { + len = (std::snprintf)(number_buffer.data(), number_buffer.size(), "%.*g", d, x); + } // negative value indicates an error JSON_ASSERT(len > 0); From ee0ceac55d8e5801a745b2d3a00e92da82f0f308 Mon Sep 17 00:00:00 2001 From: Kirill Lokotkov Date: Wed, 25 Jan 2023 18:44:28 +0300 Subject: [PATCH 2/4] C++11 compatibility --- include/nlohmann/detail/output/serializer.hpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/include/nlohmann/detail/output/serializer.hpp b/include/nlohmann/detail/output/serializer.hpp index c3e8d291d..f18339fe7 100644 --- a/include/nlohmann/detail/output/serializer.hpp +++ b/include/nlohmann/detail/output/serializer.hpp @@ -833,7 +833,7 @@ class serializer // the actual conversion // NOLINTNEXTLINE(cppcoreguidelines-pro-type-vararg,hicpp-vararg) std::ptrdiff_t len; - if constexpr (std::is_same_v) { + if (std::is_same::value) { len = (std::snprintf)(number_buffer.data(), number_buffer.size(), "%.*Lg", d, x); } else { len = (std::snprintf)(number_buffer.data(), number_buffer.size(), "%.*g", d, x); From 66026849965a9f2fe3eeb3c7ae61c7df8dd32e41 Mon Sep 17 00:00:00 2001 From: Kirill Lokotkov Date: Wed, 25 Jan 2023 19:04:11 +0300 Subject: [PATCH 3/4] Shorter solution --- include/nlohmann/detail/output/serializer.hpp | 9 +++------ 1 file changed, 3 insertions(+), 6 deletions(-) diff --git a/include/nlohmann/detail/output/serializer.hpp b/include/nlohmann/detail/output/serializer.hpp index f18339fe7..e6dabac75 100644 --- a/include/nlohmann/detail/output/serializer.hpp +++ b/include/nlohmann/detail/output/serializer.hpp @@ -832,12 +832,9 @@ class serializer // the actual conversion // NOLINTNEXTLINE(cppcoreguidelines-pro-type-vararg,hicpp-vararg) - std::ptrdiff_t len; - if (std::is_same::value) { - len = (std::snprintf)(number_buffer.data(), number_buffer.size(), "%.*Lg", d, x); - } else { - len = (std::snprintf)(number_buffer.data(), number_buffer.size(), "%.*g", d, x); - } + std::ptrdiff_t len = (std::snprintf)(number_buffer.data(), number_buffer.size(), + std::is_same::value ? "%.*Lg" : "%.*g", + d, x); // negative value indicates an error JSON_ASSERT(len > 0); From 988c050e66c5ba4e9a357765c3534638b9f83aab Mon Sep 17 00:00:00 2001 From: rusloker Date: Thu, 26 Jan 2023 11:34:11 +0300 Subject: [PATCH 4/4] Applied amalgamate --- include/nlohmann/detail/output/serializer.hpp | 2 +- single_include/nlohmann/json.hpp | 4 +++- 2 files changed, 4 insertions(+), 2 deletions(-) diff --git a/include/nlohmann/detail/output/serializer.hpp b/include/nlohmann/detail/output/serializer.hpp index e6dabac75..3aa4b2b86 100644 --- a/include/nlohmann/detail/output/serializer.hpp +++ b/include/nlohmann/detail/output/serializer.hpp @@ -832,7 +832,7 @@ class serializer // the actual conversion // NOLINTNEXTLINE(cppcoreguidelines-pro-type-vararg,hicpp-vararg) - std::ptrdiff_t len = (std::snprintf)(number_buffer.data(), number_buffer.size(), + std::ptrdiff_t len = (std::snprintf)(number_buffer.data(), number_buffer.size(), std::is_same::value ? "%.*Lg" : "%.*g", d, x); diff --git a/single_include/nlohmann/json.hpp b/single_include/nlohmann/json.hpp index 2448bf22d..81c3645e8 100644 --- a/single_include/nlohmann/json.hpp +++ b/single_include/nlohmann/json.hpp @@ -18754,7 +18754,9 @@ class serializer // the actual conversion // NOLINTNEXTLINE(cppcoreguidelines-pro-type-vararg,hicpp-vararg) - std::ptrdiff_t len = (std::snprintf)(number_buffer.data(), number_buffer.size(), "%.*g", d, x); + std::ptrdiff_t len = (std::snprintf)(number_buffer.data(), number_buffer.size(), + std::is_same::value ? "%.*Lg" : "%.*g", + d, x); // negative value indicates an error JSON_ASSERT(len > 0);