From 89ab7eca7f3290d47770a569ae99982a7cee3a08 Mon Sep 17 00:00:00 2001 From: Brendan Drew Date: Thu, 27 Oct 2016 04:43:04 -0700 Subject: [PATCH] Ensure that the fact that a float was provided on input is preserved when writing output; update tests to reflect this fact --- src/lib_json/json_writer.cpp | 8 +++++++- src/test_lib_json/main.cpp | 10 +++++----- 2 files changed, 12 insertions(+), 6 deletions(-) diff --git a/src/lib_json/json_writer.cpp b/src/lib_json/json_writer.cpp index 1bb3b21..a03f0eb 100644 --- a/src/lib_json/json_writer.cpp +++ b/src/lib_json/json_writer.cpp @@ -139,7 +139,7 @@ namespace { JSONCPP_STRING valueToString(double value, bool useSpecialFloats, unsigned int precision) { // Allocate a buffer that is more than large enough to store the 16 digits of // precision requested below. - char buffer[32]; + char buffer[36]; int len = -1; char formatString[6]; @@ -150,6 +150,12 @@ JSONCPP_STRING valueToString(double value, bool useSpecialFloats, unsigned int p // concepts of reals and integers. if (isfinite(value)) { len = snprintf(buffer, sizeof(buffer), formatString, value); + + // try to ensure we preserve the fact that this was given to us as a double on input + if (!strstr(buffer, ".") && !strstr(buffer, "e")) { + strcat(buffer, ".0"); + } + } else { // IEEE standard states that NaN values will not compare to themselves if (value != value) { diff --git a/src/test_lib_json/main.cpp b/src/test_lib_json/main.cpp index 1a1216e..47cd981 100644 --- a/src/test_lib_json/main.cpp +++ b/src/test_lib_json/main.cpp @@ -479,7 +479,7 @@ JSONTEST_FIXTURE(ValueTest, integers) { JSONTEST_ASSERT_EQUAL(0.0, val.asDouble()); JSONTEST_ASSERT_EQUAL(0.0, val.asFloat()); JSONTEST_ASSERT_EQUAL(false, val.asBool()); - JSONTEST_ASSERT_STRING_EQUAL("0", val.asString()); + JSONTEST_ASSERT_STRING_EQUAL("0.0", val.asString()); // Zero (signed constructor arg) val = Json::Value(0); @@ -563,7 +563,7 @@ JSONTEST_FIXTURE(ValueTest, integers) { JSONTEST_ASSERT_EQUAL(0.0, val.asDouble()); JSONTEST_ASSERT_EQUAL(0.0, val.asFloat()); JSONTEST_ASSERT_EQUAL(false, val.asBool()); - JSONTEST_ASSERT_STRING_EQUAL("0", val.asString()); + JSONTEST_ASSERT_STRING_EQUAL("0.0", val.asString()); // 2^20 (signed constructor arg) val = Json::Value(1 << 20); @@ -646,7 +646,7 @@ JSONTEST_FIXTURE(ValueTest, integers) { JSONTEST_ASSERT_EQUAL((1 << 20), val.asDouble()); JSONTEST_ASSERT_EQUAL((1 << 20), val.asFloat()); JSONTEST_ASSERT_EQUAL(true, val.asBool()); - JSONTEST_ASSERT_STRING_EQUAL("1048576", + JSONTEST_ASSERT_STRING_EQUAL("1048576.0", normalizeFloatingPointStr(JsonTest::ToJsonString(val.asString()))); // -2^20 @@ -887,7 +887,7 @@ JSONTEST_FIXTURE(ValueTest, integers) { JSONTEST_ASSERT_EQUAL((Json::Int64(1) << 40), val.asDouble()); JSONTEST_ASSERT_EQUAL((Json::Int64(1) << 40), val.asFloat()); JSONTEST_ASSERT_EQUAL(true, val.asBool()); - JSONTEST_ASSERT_STRING_EQUAL("1099511627776", + JSONTEST_ASSERT_STRING_EQUAL("1099511627776.0", normalizeFloatingPointStr(JsonTest::ToJsonString(val.asString()))); // -2^40 @@ -1259,7 +1259,7 @@ JSONTEST_FIXTURE(ValueTest, nonIntegers) { // A 16-digit floating point number. val = Json::Value(2199023255552000.0f); JSONTEST_ASSERT_EQUAL(float(2199023255552000.0f), val.asFloat()); - JSONTEST_ASSERT_STRING_EQUAL("2199023255552000", + JSONTEST_ASSERT_STRING_EQUAL("2199023255552000.0", normalizeFloatingPointStr(JsonTest::ToJsonString(val.asString()))); // A very large floating point number.