Skip to content

Add setting precision for json writers and also add decimal places precision type. #752

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

Merged

Conversation

melonaerial
Copy link
Contributor

In thread of issue #648 was an idea to make precision settable. So, this PR do that and also add decimal places precision setting.

@@ -187,6 +190,8 @@ class JSONCPP_DEPRECATED("Use StreamWriterBuilder instead") JSON_API FastWriter
bool yamlCompatibilityEnabled_;
bool dropNullPlaceholders_;
bool omitEndingLineFeed_;
UInt precision_;
Copy link
Contributor

Choose a reason for hiding this comment

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

We cannot change the size of a class in a public header.

Let's concentrate on the newer CharReader and StreamWriter versions. (Everybody hates them because users do not like to type a few extra characters, but those classes be altered as often as we like without impacting binary-compatibility.)

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Fixed.

@cdunn2001
Copy link
Contributor

If you drop the changes to the deprecated Reader and Writer classes, then I like this change, though it's big enough to deserve a close review.

@melonaerial
Copy link
Contributor Author

These changes only for Writer. For this issue we don't need any changes for Reader.

// Allocate a buffer that is more than large enough to store the 16 digits of
// precision requested below.
char buffer[36];
int len = -1;

char formatString[15];
snprintf(formatString, sizeof(formatString), "%%.%ug", precision);
if (precisionType == PrecisionType::significantDigits) {
snprintf(formatString, sizeof(formatString), "%%.%ug", precision);
Copy link
Contributor

Choose a reason for hiding this comment

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

Shouldn't this be e instead of g?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I was thinking about saving old behaviour with g when used the shortest variant for float, because maybe some guys expect old behaviour from code. Maybe it is better to add third type for e ?

Copy link
Contributor

Choose a reason for hiding this comment

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

You're right. Preserving the old way by default is a good idea.

I don't have an opinion on a third type.

Let's drop this latest commit. Then we can merge.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Ok, i've dropped last commit. U can merge when test will be don.

Copy link
Contributor

@cdunn2001 cdunn2001 left a comment

Choose a reason for hiding this comment

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

Looks good. Anyone else have any thoughts?

@@ -1157,6 +1172,14 @@ StreamWriter* StreamWriterBuilder::newStreamWriter() const
} else {
throwRuntimeError("commentStyle must be 'All' or 'None'");
}
PrecisionType precisionType(significantDigits);
if (pt_str == "significant") {
Copy link
Contributor

Choose a reason for hiding this comment

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

These names seem long and ambiguous. Maybe exp and noexp are better names. Or something else?

Copy link
Contributor Author

@melonaerial melonaerial Mar 12, 2018

Choose a reason for hiding this comment

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

I used significant word for old-style precision formatting. see #752 (comment). Answer it please. If we delete old-style formatting - then of course it is better change names for exp and noexp.

@@ -1757,6 +1757,27 @@ JSONTEST_FIXTURE(ValueTest, precision) {
expected = "0.25634569487374054";
result = Json::writeString(b, v);
JSONTEST_ASSERT_STRING_EQUAL(expected, result);

b.settings_["precision"] = 5;
b.settings_["precisionType"] = "decimal";
Copy link
Contributor

Choose a reason for hiding this comment

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

Maybe floatPrecision and floatFormat?

Copy link
Contributor Author

@melonaerial melonaerial Mar 12, 2018

Choose a reason for hiding this comment

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

okay, but we broke compability for guys who alreafy using "precision" option for their programs, but I don't think there are much of such people.

Copy link
Contributor

Choose a reason for hiding this comment

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

Oh, you're right. I forgot about that.

Yes, we need to keep the word "precision". I guess "precisionType" is fine.

@cdunn2001 cdunn2001 merged commit a07fc53 into open-source-parsers:master Mar 13, 2018
@cdunn2001
Copy link
Contributor

If there are any complaints, we may have to revert to look more closely. But it looks good to me. Thanks!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants