Skip to content
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

feat: add custom json serializer support #1

Merged
merged 1 commit into from
Feb 9, 2022
Merged

Conversation

mkazlauskas
Copy link
Member

Context

If you need to support numeric values larger than Number.MAX_SAFE_INTEGER, you can't use built-in JSON.parse/JSON.stringify.

While most JSON parsers assume numeric values have same precision restrictions as IEEE 754 double, JSON specification does not say anything about number precision. Any floating point number in decimal (optionally scientific) notation is valid JSON value. It's a good idea to serialize values which might fall out of IEEE 754 integer precision as strings in your JSON api, but { "value" : 9223372036854775807}, for example, is still a valid RFC4627 JSON string, and in most JS runtimes the result of JSON.parse is this object: { value: 9223372036854776000 }

source

Proposed solution

Add a new optional option: jsonSerializer

Copy link
Member

@rhyslbw rhyslbw 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. Seems like a solution that could be adopted upstream, and would be worth opening a PR so we can potentially drop the fork in the future.

$ yarn test
yarn run v1.22.17
$ yarn test:node && yarn test:dom
$ jest --testEnvironment node


 PASS  tests/document-node.test.ts (5.834 s)
 PASS  tests/gql.test.ts (5.846 s)
 PASS  tests/custom-fetch.test.ts (5.865 s)
 PASS  tests/general.test.ts (5.892 s)
 PASS  tests/batching.test.ts (5.9 s)
 PASS  tests/json-serializer.test.ts (5.939 s)
 PASS  tests/headers.test.ts (5.95 s)
 PASS  tests/endpoint.test.ts (5.958 s)
 PASS  tests/upload.test.ts (5.95 s)
 PASS  tests/signal.test.ts (6.067 s)

Test Suites: 10 passed, 10 total
Tests:       1 skipped55 passed, 56 total
Snapshots:   6 passed, 6 total
Time:        6.377 s
Ran all test suites.
$ jest --testEnvironment jsdom
 PASS  tests/document-node.test.ts (5.658 s)
 PASS  tests/upload.test.ts (5.694 s)
 PASS  tests/json-serializer.test.ts (5.681 s)
 PASS  tests/endpoint.test.ts (5.711 s)
 PASS  tests/batching.test.ts (5.719 s)
 PASS  tests/gql.test.ts (5.723 s)
 PASS  tests/general.test.ts (5.735 s)
 PASS  tests/custom-fetch.test.ts (5.747 s)
 PASS  tests/headers.test.ts (5.799 s)
 PASS  tests/signal.test.ts (5.829 s)

Test Suites: 10 passed, 10 total
Tests:       1 skipped, 55 passed, 56 total
Snapshots:   6 passed, 6 total
Time:        6.583 s, estimated 7 s
Ran all test suites.
Done in 14.22s.

@rhyslbw rhyslbw merged commit e792de2 into master Feb 9, 2022
@rhyslbw rhyslbw deleted the feat/json-serializer branch February 9, 2022 11:10
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants