Merge pull request #547 from BrendanDrewDaqri/master

resolves #546: Ensure floating point values on input render as floats on output
This commit is contained in:
Christopher Dunn 2016-10-28 01:22:38 -05:00 committed by GitHub
commit 0e24e3c64f
2 changed files with 12 additions and 6 deletions

View File

@ -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) {

View File

@ -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.