Once you pass the JSON string through JSON.parse, you will lose precision due to the way the floating point math works . You will need to save this number as an object intended for storing arbitrary precision numbers , and before parsing it, you will need to bother with the string itself. The easiest way is with regular expressions. JSON is context free grammar, and regular expressions work with regular grammars, so a warning applies:
WARNING: PARSING CFG WITH REGEX CAN SUMMON LOAD
This regular expression should turn the numbers in your JSON into strings:
let stringedJSON = origJSON.replace(/:\s*([-+Ee0-9.]+)/g, ': "uniqueprefix$1"');
, , , data:42
.
, , stringedJSON
- {"foo": "uniqueprefix0.00000017", "bar": "an actual string"}
. JSON.parse
, uniqueprefix0.00000017
- , . JSON.parse
reviver
, . , :
let o = JSON.parse(stringedJSON, (key, value) => {
if (typeof value !== 'string') return value;
if (!value.startsWith('uniqueprefix')) return value;
value = value.slice('uniqueprefix'.length);
return new Big(value);
});