Skip to content

Commit 1a6426a

Browse files
committed
Merge pull request open-source-parsers#21 from hiharin/master
Hmmm. Not ideal. A round-trip should reproduce the original, but null -> NaN -> ? But I guess it's no worse than it was. The different behavior for Win CE is troubling, but it only affects people who are using these extreme values. I've worked with Inf/NaN before, so I understand your pain.
2 parents 1ac2295 + bc5dbc6 commit 1a6426a

File tree

1 file changed

+24
-1
lines changed

1 file changed

+24
-1
lines changed

src/lib_json/json_writer.cpp

Lines changed: 24 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
#include <string.h>
1414
#include <sstream>
1515
#include <iomanip>
16+
#include <math.h>
1617

1718
#if defined(_MSC_VER) && _MSC_VER >= 1400 // VC++ 8.0
1819
// Disable warning about strdup being deprecated.
@@ -79,7 +80,29 @@ std::string valueToString(double value) {
7980
sprintf_s(buffer, sizeof(buffer), "%.16g", value);
8081
#endif
8182
#else
82-
snprintf(buffer, sizeof(buffer), "%.16g", value);
83+
if ( isfinite( value ))
84+
{
85+
snprintf(buffer, sizeof(buffer), "%.16g", value);
86+
}
87+
else
88+
{
89+
// IEEE standard states that NaN values will not compare to themselves
90+
if ( value != value)
91+
{
92+
snprintf(buffer, sizeof(buffer), "null");
93+
}
94+
else if ( value < 0)
95+
{
96+
snprintf(buffer, sizeof(buffer), "-1e+9999");
97+
}
98+
else
99+
{
100+
snprintf(buffer, sizeof(buffer), "1e+9999");
101+
}
102+
// nothing more to do, return.
103+
return buffer;
104+
}
105+
83106
#endif
84107
fixNumericLocale(buffer, buffer + strlen(buffer));
85108
return buffer;

0 commit comments

Comments
 (0)