From 71641088773d12b077a9f3985828974324933aef Mon Sep 17 00:00:00 2001 From: Florent Date: Wed, 27 May 2020 18:15:22 +0200 Subject: [PATCH 1/2] show bug when trying to stringify an object containing a double --- test/tests/LibStuffTest.cpp | 9 ++++++++- 1 file changed, 8 insertions(+), 1 deletion(-) diff --git a/test/tests/LibStuffTest.cpp b/test/tests/LibStuffTest.cpp index 99ca23aba..da9d05e97 100644 --- a/test/tests/LibStuffTest.cpp +++ b/test/tests/LibStuffTest.cpp @@ -62,9 +62,16 @@ struct LibStuff : tpunit::TestFixture { void testJSON() { // Floating point value tests - ASSERT_EQUAL(SToJSON("{\"imAFloat\":1.2}"), "{\"imAFloat\":1.2}"); + ASSERT_EQUAL(SToJSON("{\"imAFloat\":0.0000}"), "{\"imAFloat\":0.0000}"); ASSERT_EQUAL(SToJSON("{\"imAFloat\":-0.23456789}"), "{\"imAFloat\":-0.23456789}"); ASSERT_EQUAL(SToJSON("{\"imAFloat\":-123456789.23456789}"), "{\"imAFloat\":-123456789.23456789}"); + ASSERT_EQUAL(SToJSON("{\"object\":{\"imAFloat\":1.00}}"), "{\"object\":{\"imAFloat\":1.00}}"); + + STable test; + test["imAFloat"] = (double)0.0000; + string returnVal = SComposeJSONObject(test); + cout << "returnVal: " << returnVal << endl; + ASSERT_EQUAL(returnVal, "{\"imAFloat\":0.0000}"); // Scientific notation tests ASSERT_EQUAL(SToJSON("{\"science\":1.5e-8}"), "{\"science\":1.5e-8}"); From 81b8e4773dbe986e852c7c5d410038c9868bd476 Mon Sep 17 00:00:00 2001 From: Florent Date: Thu, 28 May 2020 12:53:56 +0200 Subject: [PATCH 2/2] Update SToJSON to handle floats and doubles (with 6 point precision) --- libstuff/libstuff.cpp | 7 ++++++- libstuff/libstuff.h | 3 ++- test/tests/LibStuffTest.cpp | 9 ++++----- 3 files changed, 12 insertions(+), 7 deletions(-) diff --git a/libstuff/libstuff.cpp b/libstuff/libstuff.cpp index 692f0d9ac..466ddc54f 100644 --- a/libstuff/libstuff.cpp +++ b/libstuff/libstuff.cpp @@ -1191,8 +1191,13 @@ extern const char* _SParseJSONValue(const char* ptr, const char* end, string& va string SToJSON(const string& value, const bool forceString) { // Is it an integer? - if (SToStr(SToInt64(value.c_str())) == value) + if (SToStr(SToInt64(value.c_str())) == value) { return value; + } + // Is it a float? + if (SToStr(SToFloat(value.c_str())) == value) { + return value; + } // Is it boolean? if (SIEquals(value, "true")) diff --git a/libstuff/libstuff.h b/libstuff/libstuff.h index a2483784a..f85a4fbeb 100644 --- a/libstuff/libstuff.h +++ b/libstuff/libstuff.h @@ -26,6 +26,7 @@ #include #include #include +#include #include #include #include @@ -378,7 +379,7 @@ string SHexStringFromBase32(const string& buffer); // **NOTE: Use 'ostringstream' because 'stringstream' leaks on VS2005 template inline string SToStr(const T& t) { ostringstream ss; - ss << t; + ss << fixed << showpoint << setprecision(6) << t; return ss.str(); } diff --git a/test/tests/LibStuffTest.cpp b/test/tests/LibStuffTest.cpp index da9d05e97..390abcfaf 100644 --- a/test/tests/LibStuffTest.cpp +++ b/test/tests/LibStuffTest.cpp @@ -67,11 +67,10 @@ struct LibStuff : tpunit::TestFixture { ASSERT_EQUAL(SToJSON("{\"imAFloat\":-123456789.23456789}"), "{\"imAFloat\":-123456789.23456789}"); ASSERT_EQUAL(SToJSON("{\"object\":{\"imAFloat\":1.00}}"), "{\"object\":{\"imAFloat\":1.00}}"); - STable test; - test["imAFloat"] = (double)0.0000; - string returnVal = SComposeJSONObject(test); - cout << "returnVal: " << returnVal << endl; - ASSERT_EQUAL(returnVal, "{\"imAFloat\":0.0000}"); + STable testFloats; + testFloats["imAFloat"] = (double)0.000; + string returnVal = SComposeJSONObject(testFloats); + ASSERT_EQUAL(returnVal, "{\"imAFloat\":0.000000}"); // Scientific notation tests ASSERT_EQUAL(SToJSON("{\"science\":1.5e-8}"), "{\"science\":1.5e-8}");