-
Notifications
You must be signed in to change notification settings - Fork 69
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
bug: parsing big integers in JSON data
loses precision
#489
Comments
I think your Steps to Reproduce are a little misleading. You type there in verbatim a long number. JavaScript engine, parse a file, and use IEEE 754 Standard to parse a let e = new CloudEvent({ source: 'example', type: 'example', data: 1524831183200260097n }) The problem exists, though. It lies in parsers implemented in this library. Here's simple jest for const { HTTP } = require('cloudevents')
test('Parse big numbers from HTTP', () => {
let ce = HTTP.toEvent({
headers: { 'content-type': 'application/cloudevents+json' },
body: `{"id": 1524831183200260097}`
})
expect(ce.id).toBe(1524831183200260097n);
}) This test fails:
|
This issue is stale because it has been open 30 days with no activity. |
@cardil you are correct to point out the error in my example. But your example would not produce a valid CE even if we could handle big integers, because the required type for the I think this is still a problem, but if something is sending a big int for the |
An event may have data that contains a BigInt. The builtin `JSON` parser for JavaScript does not handle the `BigInt` types. The introduced `json-bigint` dependency (34k) does. Fixes: cloudevents#489 Signed-off-by: Lance Ball <lball@redhat.com>
An event may have data that contains a BigInt. The builtin `JSON` parser for JavaScript does not handle the `BigInt` types. The introduced `json-bigint` dependency (34k) does. Fixes: cloudevents#489 Signed-off-by: Lance Ball <lball@redhat.com>
This issue is stale because it has been open 30 days with no activity. |
An event may have data that contains a BigInt. The builtin `JSON` parser for JavaScript does not handle the `BigInt` types. The introduced `json-bigint` dependency (34k) does. Fixes: cloudevents#489 Signed-off-by: Lance Ball <lball@redhat.com>
* fix: handle big integers in incoming events An event may have data that contains a BigInt. The builtin `JSON` parser for JavaScript does not handle the `BigInt` types. The introduced `json-bigint` dependency (34k) does. Fixes: #489 Signed-off-by: Lance Ball <lball@redhat.com>
Describe the Bug
When parsing JSON data, if a JSON field value is a number, and that number is really big, JavaScript loses precision. For example, the Twitter API exposes the Tweet ID. This is a large number that exceeds the integer space of
Number
. You can see this by simply assigning a large number to a simple variable (it's not actually JSON that is the problem, it's JavaScript itself).Steps to Reproduce
Expected Behavior
I expect the data to not lose precision.
Additional context
This can be resolved by using
json-bigint
The text was updated successfully, but these errors were encountered: