I'm trying to tackle the issue with JSON.parse() rounding big numbers. I know why this is happening but am looking for away around it.
I was thinking a regex which could parse the JSON text and turn all the big ints into strings.
I'm using JavaScript in a Windows 8 app and its got to be dealt with client side.
Got me stumped so far.
The main issue is I have to do this after I receive the response from an XMLHTTPRequest and cant change the original format of the JSON
e.g
{ "data" : {
"username":"Brad", "userID":941022167561310208, "location":"London"
}
}
toJSON() calls the object's toISOString() method, which returns a string representing the Date object's value. This method is generally intended to, by default, usefully serialize Date objects during JSON serialization, which can then be deserialized using the Date() constructor or Date. parse() as the reviver of JSON.
The maximum length of JSON strings. The default is 2097152 characters, which is equivalent to 4 MB of Unicode string data.
Example - Parsing JSONUse the JavaScript function JSON.parse() to convert text into a JavaScript object: const obj = JSON.parse('{"name":"John", "age":30, "city":"New York"}'); Make sure the text is in JSON format, or else you will get a syntax error.
BigInt is one of the standard built-in types in Typescript. At the moment TypedJSON does not support parsing BigInt out of the box. The underlying problem is also that the standard JSON. parse() does not support parsing bigint - it coerces bigints into number , which loses precision.
Disclaimer:
This is an old answer, and the question is even older. You shouldn't ever be parsing JSON with regular expressions - it's too brittle of a solution, and is liable to break at random. The right way to do this is to parse the entire JSON string, then use code to convert string values to numbers, e.g. parseInt(jsonData.data.userID)
.
An even better way to do it is to fix it on your back-end so your client doesn't have to do this.
Here's the regex to wrap all integers with quotes. If you wanted to do this only for long strings, change +
to {9,}
(or any int).
var json = ('{"data":{"username":"Brad","userID":941022167561310208,"location":"London","test":"3908349804","test2":"This already exists in 034093049034 quotes"},"list":[3409823408,3409823408,"A list 234908234"]}');
json = json.replace(/([\[:])?(\d+)([,\}\]])/g, "$1\"$2\"$3");
json = JSON.parse(json);
See example: http://jsfiddle.net/remus/6YMYT/
Edit Updated to accommodate lists as well.
Here's the regex in action: http://regex101.com/r/qJ3mD2
I know this is a very old question, but this is the solution you can use if you are using npm. Javascript can handle bigInt of value 9007199254740991
docs. You can use this npm package to parse JSON's containing large numbers.
Install your package
npm i json-bigint
Then use it like this.
var JSONbig = require('json-bigint');
let result = JSONbig.parse(<your-json>);
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With