Parse/stringify bigints to JSON

Currently, JSON.stringify only accepts numbers and JSON.parse only returns numbers. Could this be updated to support bigints?

JSON.parseStrict would return bigints for 1/1.0/etc., and JSON.stringifyStrict would emit floats as 1.0 and bigints as 1. Otherwise, they're basically identical.

The use case is 1. differentiating between the two (uncommon but sometimes useful), 2. being able to read very large integers accurately, and 3. better interop with dynamic but strongly-typed languages like Kotlin that also run in the browser with broad high-level JS interop.

See
https://github.com/tc39/proposal-json-parse-with-source

Felt like there was a proposal down this vein. I just couldn't remember where it was.

What about add an option in the JSON.stringify/parse?
e.g. JSON.parse(str, {asBigInt: true})

1 Like