Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add define to set significant digits when writing json floats and dou… #891

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions docs/guides/json.md
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ A `wvalue` can be treated as an object or even a list (setting a value by using

JSON does not allow floating point values like `NaN` or `INF`, Crow will output `null` instead of `NaN` or `INF` when converting `wvalue` to a string. (`{"Key": NaN}` becomes `{"Key": null}`)

To serialize `#!cpp float` and `#!cpp double` with a given number of significant digits, say 6 (the default), use the macros `#!cpp #define CROW_JSON_FLOAT_PRECISION 6` and `#!cpp #define CROW_JSON_DOUBLE_PRECISION 6`.
<br><br>

Additionally, a `wvalue` can be initialized as an object using an initializer list, an example object would be `wvalue x = {{"a", 1}, {"b", 2}}`. Or as a list using `wvalue x = json::wvalue::list({1, 2, 3})`, lists can include any type that `wvalue` supports.<br><br>
Expand Down
17 changes: 13 additions & 4 deletions include/crow/json.h
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,15 @@

//#define CROW_JSON_NO_ERROR_CHECK
//#define CROW_JSON_USE_MAP
//#define CROW_JSON_FLOAT_PRECISION FLT_DIG
//#define CROW_JSON_DOUBLE_PRECISION DBL_DIG

#ifndef CROW_JSON_FLOAT_PRECISION
#define CROW_JSON_FLOAT_PRECISION 6
#endif
#ifndef CROW_JSON_DOUBLE_PRECISION
#define CROW_JSON_DOUBLE_PRECISION 6
#endif

#include <string>
#ifdef CROW_JSON_USE_MAP
Expand Down Expand Up @@ -1874,17 +1883,17 @@ namespace crow // NOTE: Already documented in "crow/app.h"
if (v.nt == num_type::Double_precision_floating_point)
{
#ifdef _MSC_VER
sprintf_s(outbuf, sizeof(outbuf), "%.*g", DECIMAL_DIG, v.num.d);
sprintf_s(outbuf, sizeof(outbuf), "%.*g", CROW_JSON_DOUBLE_PRECISION, v.num.d);
#else
snprintf(outbuf, sizeof(outbuf), "%.*g", DECIMAL_DIG, v.num.d);
snprintf(outbuf, sizeof(outbuf), "%.*g", CROW_JSON_DOUBLE_PRECISION, v.num.d);
#endif
}
else
{
#ifdef _MSC_VER
sprintf_s(outbuf, sizeof(outbuf), "%f", v.num.d);
sprintf_s(outbuf, sizeof(outbuf), "%.*g", CROW_JSON_FLOAT_PRECISION, v.num.d);
#else
snprintf(outbuf, sizeof(outbuf), "%f", v.num.d);
snprintf(outbuf, sizeof(outbuf), "%.*g", CROW_JSON_FLOAT_PRECISION, v.num.d);
#endif
}
char *p = &outbuf[0], *o = nullptr; // o is the position of the first trailing 0
Expand Down
36 changes: 26 additions & 10 deletions tests/unittest.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1124,22 +1124,38 @@ TEST_CASE("json::wvalue::wvalue(std::int64_t)")

TEST_CASE("json::wvalue::wvalue(float)")
{
float f = 4.2;
json::wvalue value = f;
{
float f = 3.14159265359;
json::wvalue value = f;

CHECK(value.t() == json::type::Number);
CHECK(value.dump() == "4.2");
CHECK(value.t() == json::type::Number);
CHECK(value.dump() == "3.14159");
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this is only true for CROW_JSON_FLOAT_PRECISION = 6 , so shouldn't this be part of test equation?

}
{
float f = 314159265.359;
json::wvalue value = f;

CHECK(value.t() == json::type::Number);
CHECK(value.dump() == "3.14159e+08");
}
} // json::wvalue::wvalue(float)

TEST_CASE("json::wvalue::wvalue(double)")
{
double d = 0.036303908355795146;
json::wvalue value = d;
{
double d = 0.036303908355795146;
json::wvalue value = d;

CHECK(value.t() == json::type::Number);
auto dumped_value = value.dump();
CROW_LOG_DEBUG << dumped_value;
CHECK(std::abs(utility::lexical_cast<double>(dumped_value) - d) < numeric_limits<double>::epsilon());
CHECK(value.t() == json::type::Number);
CHECK(value.dump() == "0.0363039");
}
{
double d = 36303908355795.146;
json::wvalue value = d;

CHECK(value.t() == json::type::Number);
CHECK(value.dump() == "3.63039e+13");
}
} // json::wvalue::wvalue(double)

TEST_CASE("json::wvalue::wvalue(char const*)")
Expand Down
Loading