-
Notifications
You must be signed in to change notification settings - Fork 450
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix(cli):resolve data precision loss in JSON messages
- Loading branch information
Showing
4 changed files
with
46 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
const JSONBigNumber = require('json-bigint') | ||
const JSONBigInt = require('json-bigint')({ useNativeBigInt: true }) | ||
|
||
export const jsonParse: typeof JSON.parse = (...args: any[]) => { | ||
try { | ||
// When JSON only contains integers, use the native bigint type. | ||
return JSONBigInt.parse(...args) | ||
} catch (_error) { | ||
// When JSON contains floating-point numbers, use the bignumber library. | ||
// The numbers in the parsed JSON Object will be represented as BigNumber Objects. | ||
return JSONBigNumber.parse(...args) | ||
} | ||
} | ||
|
||
export const jsonStringify: typeof JSON.stringify = (...args: any[]) => { | ||
try { | ||
// When JSON only contains integers, use the native bigint type. | ||
return JSONBigInt.stringify(...args) | ||
} catch (_error) { | ||
// When JSON contains floating-point numbers, use the bignumber library. | ||
// Integers will be represented in scientific notation. | ||
return JSONBigNumber.stringify(...args) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters