2

I am using ASP.NET core web application that fetches some data from database and returns a data transfer object serialized to JSON. I have noticed that when the data contains a big number (e.g. 70679185527693127) then the number is rounded when pretty-printed. I inspected the returned JSON using browser developer tools (Firefox & Chrome) and raw data is correct, but incorrect when pretty-printed. I have also tried to pretty-print the JSON using this website and the results are still the same. This is for some reason a problem for my Kendo grid that I am trying to show the data in as it shows the wrong rounded data. Why is that? Do I have to avoid using such big numbers?

Here is the very simplified JSON:

{"Data":[{"Id":70679185527693127}],"Total":1}

Here is the pretty-printed version:

{
  "Data": [
    {
      "Id": 70679185527693130
    }
  ],
  "Total": 1
}

See the difference in the values.

EDIT: Passing the value as a string works as expected (atleast using the mentioned website). Also a side question: why does the Kendo grid show the pretty-printed data instead of parsing the raw data which seem to be correct?

Popa611
  • 121
  • 2
  • 10
  • 1
    Did you try passing the number as a string ? ```{"Data":[{"Id":"70679185527693127"}],"Total":1}``` – Camille Jun 28 '21 at 07:05
  • @Camille passing the number as a string works as expected. This could be solution to my problem, but I am still intrigued as to why it probably cannot handle big numbers (that can still fit to 64bit variables). Also why Kendo grid uses the pretty-printed values, not raw data to display it. – Popa611 Jun 28 '21 at 07:09
  • 2
    Pretty print is realized via `JSON.parse()` + `JSON.stringify()`. The value of your Id exceeds `Number.MAX_SAFE_INTEGER` so `JSON.parse()` will possibly parse an incorrect value. I assume Kendo also uses JSON.parse() and thus will also display wrong values. [see also](https://stackoverflow.com/questions/47188449/json-max-int-number/47188576) – Stephan Bauer Jun 28 '21 at 07:13
  • @StephanBauer Thanks, I think this should be an answer, not a comment. =) – Popa611 Jun 28 '21 at 07:14

1 Answers1

2

Pretty print is realized via JSON.parse() + JSON.stringify().

The value of your Id exceeds Number.MAX_SAFE_INTEGER so JSON.parse() will possibly parse an incorrect value.
I assume Kendo also uses JSON.parse() and thus will also display wrong values.

See also this question for more information

Stephan Bauer
  • 9,120
  • 5
  • 36
  • 58