Skip to content

feat: opt-in safe JSON parsing for large integer IDs (v1.7.0) - #2

Draft
JJ-Cro wants to merge 1 commit into
masterfrom
update07102026
Draft

JJ-Cro wants to merge 1 commit into
masterfrom
update07102026

Conversation

@JJ-Cro

@JJ-Cro JJ-Cro commented Oct 7, 2026

Copy link
Copy Markdown
Owner

Summary

Proof of concept for sieblyio/gateio-api#101: Gate order IDs above Number.MAX_SAFE_INTEGER lose precision when responses are parsed with JSON.parse. This adds an opt-in, dependency-free text scanner and wires it into REST (axios) and WebSocket message parsing.

Changelog (1.7.0)

  • src/lib/jsonParse.ts: quoteLossyIntegers(text) walks JSON text (respecting strings/escapes) and wraps only integer literals whose magnitude exceeds Number.MAX_SAFE_INTEGER in quotes. parseJSONSafeIds(text) = JSON.parse(quoteLossyIntegers(text)).
  • REST (BaseRestClient): optional keepIdsAsString and customParseJSONFn on RestClientOptions (default off). When enabled, uses responseType: 'text' and a transformResponse that parses with the scanner or custom fn.
  • WebSocket (WebsocketClient.resolveEmittableEvents): same options on WSClientConfigurableOptions for the single JSON.parse path.
  • Tests: test/lib/jsonParse.test.ts covers root/nested/array ids, negatives, strings (including escapes), decimals/exponents, safe ints, and parity with JSON.parse when no lossy ints.
  • Benchmark: scripts/benchmark-json-parse.ts (run after npm run build).

Benchmark (1000 bid + 1000 ask levels, 200 iterations)

Parser Total Per op
JSON.parse 130.07 ms 0.650 ms/op
parseJSONSafeIds 485.31 ms 2.427 ms/op

Ratio: ~3.73× slower than plain JSON.parse on this payload (expected: extra scan pass before parse).

npm run build && node --experimental-strip-types scripts/benchmark-json-parse.ts

Usage

const rest = new RestClient({ keepIdsAsString: true });
const ws = new WebsocketClient({ keepIdsAsString: true });
// or: customParseJSONFn: (text) => parseJSONSafeIds(text)

Response typings are unchanged; with the flag off, behaviour is identical to before.

CI / local checks

  • npm run lint — pass
  • npm run build — pass
  • npm test — 11/11 new jsonParse tests pass; 23/30 total pass (7 existing test/REST/private.test.ts failures: missing API keys in this environment, unchanged by this PR)

Notes

  • No new dependencies.
  • CHANGELOG.md not edited (per fork workflow); release notes above.
Open in Web Open in Cursor 

Changelog:
- Add quoteLossyIntegers / parseJSONSafeIds scanner (no new dependencies)
- REST: keepIdsAsString + customParseJSONFn via axios text + transformResponse
- WebSocket: same options in resolveEmittableEvents JSON.parse path
- Unit tests for scanner edge cases; benchmark script for 1000-level order book

Addresses sieblyio#101 (PoC on JJ-Cro fork).

Co-authored-by: Jerko J <JJ-Cro@users.noreply.github.com>
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