-
-
Notifications
You must be signed in to change notification settings - Fork 1.5k
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
properly fix #13196: json serialization with option for lossless roundtrip #13364
properly fix #13196: json serialization with option for lossless roundtrip #13364
Conversation
12f7d83
to
be6a9ed
Compare
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
LGTM
Let's instead use ryu asap and backport it. |
…now override default precision
PTAL I've turned relevant json apis now accept a
so users can decide for their own application, and there is no breaking change.
this could take an unspecified amount of time (ryu is not exactly simple, look at their source code); plus it should work for all supported targets (vm, c, js probably too); vm likely will use a vmcallback for sure. in the meantime, this PR works right now for ppl needing roundtrip safety as an option, and it'll be by default too once ryu (#13365) is implemented |
But this is not a quick fix. It's an API extension and you use c_sprintf twice, so everything runs a little slower. |
reasonable IMO
fixed, found a much better way |
Since you were so eager to revert my PR where your reasoning included this complaint:
May I complain here with "it can blow up size of json messages", too? And no I disagree, this is not a proper fix #13196. Unlike your PR here, I analyzed what the root cause for #13196 is. And the root cause for it is lossy string conversion of floating point numbers. I fixed that. If the default stringification is lossy, then it is no surprise that lossy stringification is used where it shouldn't be used, for example in json. So I made sure that the default string representation is always lossless, and the json issue was fixed as a consequence unlike this fix which just fixes the symptom. Of course that breaks tests that expect a very specific string representation of a floating point number. But the string representation of a floating point number isn't unique, and people know this. If people still insist on wring tests that depend on a very specific string representation of a floating point number, then those are badly written tests that should be fixed. Breaking those tests is a price I am willing to pay. Btw I am still in favor of #13276 over this and But whatever... |
with this PR, this is now up to the user, depending on
Not a single language I've ever used prints float literal 0.6 as 0.59999999999999998: python, D, C, C++, go, node js, matlab, octave, java...
performance is a separate issue. And we already have means to avoid intermediate string objects today for performance sensitive code |
Do I get you right, by default json serialization isn't correct, but you provide an option to the user to enable correctness. |
I've explained this in detail in #13364 (comment) |
But the real issue is that |
The idea that we add parsing modes for JSON is a good one. I'm not sure how the API works, but it lends the programmer a consistent tool to solve problems of It can be argued that a better option is no option, but I don't think it can be argued that no option is a better option for Nim today given the complexities of the problem and the stability of the language. |
This pull request has been automatically marked as stale because it has not had recent activity. If you think it is still a valid PR, please rebase it on the latest devel; otherwise it will be closed. Thank you for your contributions. |
this properly fixes #13196 without affecting how nim prints/stringifies floats, so that:
echo 0.6
is now 0.6 again, unlike after #13276future PR's
implement https://github.com/ulfjack/ryu to keep short representations eg for json => see #13365