Skip to content

fix(sdk): Add proper BigInt serialization for JSON API responses - #258

Merged
Kevin737866 merged 2 commits into
Kevin737866:mainfrom
Vincent6581:fix/issue-193-bigint-serialization
Sep 4, 2026
Merged

fix(sdk): Add proper BigInt serialization for JSON API responses#258
Kevin737866 merged 2 commits into
Kevin737866:mainfrom
Vincent6581:fix/issue-193-bigint-serialization

Conversation

@Vincent6581

Copy link
Copy Markdown
Contributor

Summary

JSON.stringify throws TypeError: Do not know how to serialize a BigInt. This adds first-class BigInt ⇄ JSON support, per issue #193.

Acceptance criteria

Criterion Status
BigInt serialized as strings in all JSON responses bigIntReplacer / serializeBigInts() render every bigint as a decimal string
Deserialization parses strings back to BigInt bigIntReviver (tagged) and createBigIntReviver(keys) (plain strings by field name)
Custom JSON replacer/reviver exported bigIntReplacer, taggedBigIntReplacer, bigIntReviver, createBigIntReviver
Consistent across all client classes StellarRWASDK.toJSONSafe() / .stringifyJSON() / .parseJSON() — one path all client results flow through

Two modes

Plain-string — for external APIs where big integers are strings:

import { bigIntReplacer, serializeBigInts } from 'stellar-rwa-sdk';

JSON.stringify({ amount: 123n, fees: [1n, 2n] }, bigIntReplacer);
// {"amount":"123","fees":["1","2"]}

serializeBigInts(portfolio); // deep copy, bigint -> string, handles nested objects/arrays/Map/Set

Tagged (lossless round-trip) — for caches / persistence:

import { stringifyJSON, parseJSON } from 'stellar-rwa-sdk';

const json = stringifyJSON({ amount: 123n, nested: { x: [5n] } });
// {"amount":{"$bigint":"123"},"nested":{"x":[{"$bigint":"5"}]}}

const back = parseJSON(json);
typeof back.amount;        // "bigint"
back.nested.x[0] === 5n;   // true

Round-trips correctly through nested objects and arrays (negative values and values beyond Number.MAX_SAFE_INTEGER included).

Restoring from plain strings when you know the schema

import { createBigIntReviver } from 'stellar-rwa-sdk';

JSON.parse(body, createBigIntReviver(['amount', 'totalSupply', 'perTokenAmount']));

Files changed

  • sdk/src/types.ts — replacers, revivers, serializeBigInts, stringifyJSON / parseJSON, BIGINT_TAG / TaggedBigInt / isTaggedBigInt
  • sdk/src/index.ts — re-exports + StellarRWASDK.toJSONSafe() / .stringifyJSON() / .parseJSON()

Closes #193

JSON.stringify throws on bigint. Adds first-class BigInt <-> JSON support:

- bigIntReplacer: JSON.stringify replacer rendering every bigint (and
  bignumber.js-like objects) as a decimal string - for external API
  responses
- serializeBigInts(value): deep, non-mutating conversion of every bigint
  to a string across nested objects, arrays, Map and Set
- taggedBigIntReplacer / bigIntReviver + stringifyJSON / parseJSON:
  lossless round-trip via a {"$bigint":"<decimal>"} tag
- createBigIntReviver(keys, { onlyUnsafe? }): reviver that restores
  BigInt for known field names from plain-string output
- isTaggedBigInt / BIGINT_TAG / TaggedBigInt helpers
- StellarRWASDK.toJSONSafe() / .stringifyJSON() / .parseJSON() so every
  client's results serialize consistently

Files:
- sdk/src/types.ts
- sdk/src/index.ts

Closes Kevin737866#193
@drips-wave

drips-wave Bot commented Aug 27, 2026

Copy link
Copy Markdown

@Vincent6581 Great news! 🎉 Based on an automated assessment of this PR, the linked Wave issue(s) no longer count against your application limits.

You can now already apply to more issues while waiting for a review of this PR. Keep up the great work! 🚀

Learn more about application limits

@Kevin737866

Copy link
Copy Markdown
Owner

@Vincent6581 conflict

@Kevin737866
Kevin737866 merged commit c7c71d8 into Kevin737866:main Sep 4, 2026
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.

fix(sdk): Add proper BigInt serialization for JSON API responses

2 participants