Skip to content
Source
Sign inSign up
TanStack/devtools
Open History to see the last commit

json.ts

33 lines795 Bytes

1/**
2 * Safely stringify data that may contain BigInt values
3 * BigInt values are converted to objects with a special marker
4 */
5export function stringifyWithBigInt(data: any): string {
6 return JSON.stringify(data, (_key, value) => {
7 if (typeof value === 'bigint') {
8 return {
9 __type: 'bigint',
10 value: value.toString(),
11 }
12 }
13 return value
14 })
16​
17/**
18 * Parse JSON and restore BigInt values
19 * Objects with __type: 'bigint' are converted back to BigInt
20 */
21export function parseWithBigInt(json: string): any {
22 return JSON.parse(json, (_key, value) => {
23 if (
24 value &&
25 typeof value === 'object' &&
26 value.__type === 'bigint' &&
27 typeof value.value === 'string'
28 ) {
29 return BigInt(value.value)
30 }
31 return value
32 })