-
Notifications
You must be signed in to change notification settings - Fork 170
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
Decimal should be unquoted in produced JSON #71
Comments
You could do this with: defimpl Jason.Encoder, for: Decimal do
def encode(decimal, _opts) do
Decimal.to_string(decimal, :normal)
end
end Also note that |
Thank you! As for the decode - that I fully understand :) |
Hey, sorry, Did so, but it seems that the customer implementation is not being called. |
It seems like Jason bypasses the protocols for the types it supports out of the box. So my proposal doesn't work, sorry. |
@michalmuskala, I am willing to help here - I really like to see an option to:
But I am not sure how to proceed. Since all encoder/decoder options are internally passed as function arguments, introducing new explicit option for Decimal handling means to litter the code everywhere. Another approach is to put Application-level (may hit float/decimal performance) or Jason module level flag. Then, again, I saw nowhere such a thing and it may be out of sync with the general project structure. Do you have any suggestions? |
This encoding is intentional. The Encoding decimals as strings is the only encoding that would not downgrade decimal precision back to the float precision - the exact reason for using decimals instead of floats in the first place. If the loss of precision is not an issue - you might as well use floats in the first place. Because of those reasons, I do not plan to merge any changes in this area at the moment. Thank you for raising the issue. |
Hey, thanks for the response! I do understand that point - compatibility. Thanks :) |
Hi @michalmuskala! Recently I've been implementing parts of the OpenRTB protocol and appreciate that
We know in its pure form a JSON number is as exact as can be before it's decoded. It only matters what the decoder does.
That's admittedly true for JavaScript, but it's not the only consumer of JSON data. It's the responsibility of the consuming code to use proper decoding libraries. For instance, if we communicated with a Java server it would have to use
That's not quite correct. It depends on the consuming parser/language.
Precision is indeed an issue, and sometimes the JSON response must contain JSON floats again according to the requirements. It's strange to me that we should send back
I hope you will reconsider! While changing the default behaviour would be a breaking change, providing an encoding option would satisfy both use cases. |
Decimal
should result in unquoted values in the produced JSON. Any Decimal type should be aligned with the behaviour of Integer and Float.Currently we have the following:
Can we have Jason not put quotes around the value and produce the following JSON instead?
Given that Decimal is in fact a number, and none of the ECMA/RFC standards define number (decimal/integer) boundaries, it is strange to map it to a string.
The text was updated successfully, but these errors were encountered: