-
Notifications
You must be signed in to change notification settings - Fork 481
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
Support Big Numbers #37
Comments
..continuing the discussion from #36 here.. @brimworks, thanks for providing the motivating example of an API that returns larger numbers as an opaque ID field. Unfortunately it shows one of the limitations of JSON for serialising data. The RFC (https://tools.ietf.org/html/rfc7159#section-6) notes it's best to stick within 64-bit floats for interoperability. However, upstream APIs exist that break this.. I'm still in favour of leaving the number field a number to avoid confusion and placing an undue burden on all developers to check for this occurrence. However, we could optionally add a metatable to the number like: This has several advantages:
In future this __jsonvalue field could be extended to allow arbitrary JSON to be substituted during encoding for a value. As mentioned on the pull request, if there is a need for arbitrary precision in future it should be relatively straight forward to link in support and replace the existing number encode/decode support. However, the dependency should only be added if someone has a use case that requires it. |
Erk.. wasn't thinking - numbers don't have individual metatables.. Back to:
Despite the complexity, option 2 is probably best since the lack of reliable round-trip support is typically painful (eg, arrays vs objects). |
What about option (4): Add a new option that allows numbers to be transformed from string into whatever type is desired? This means consumers of CJSON can choose option (1), (2), or (3) simply by defining a transform function as they see fit. Thanks, |
Upvoting this issue, as I suffer from data distortion too. For example, Viber API uses big numbers: It seems that initially proposed approach (just decode as strings) is the best. Anyway, one can manipulate this string in any fashion. Hope it will be resolved. |
While learning programming I tried to solve a coding challenge, where I had to send a big number to an API and ran into this problem, because lua-requests depends on lua-cjson. I hope it will be fixed in the future :) |
same issue, found another solution https://forums.solar2d.com/t/precision-lost-when-do-json-decode-a-very-large-integer-number/345152/5 http://regex.info/blog/lua/json treat big number as table |
Since all numbers are represented as double, larger numbers (like longs) get truncated. For example, the output of this code...
The text was updated successfully, but these errors were encountered: