json-diff-kit icon indicating copy to clipboard operation
json-diff-kit copied to clipboard

When json contains more than the maximum integer allowed by js, precision is lost

Open njzydark opened this issue 1 year ago • 2 comments

https://json-diff-kit.js.org/#l=%7B%0A++%22a%22%3A+90071992547409912345%0A%7D&r=%7B%0A++%22b%22%3A+90071992547409912345%0A%7D

image

njzydark avatar Feb 21 '24 09:02 njzydark

It seems a bit difficult to support large numbers if we do not write the parser ourselves.

Both integers and floats are valid in JSON, so we cannot just use the library json-bigint to parse the JSON string. Do you have any ideas?

RexSkz avatar Feb 21 '24 10:02 RexSkz

It seems a bit difficult to support large numbers if we do not write the parser ourselves.

Both integers and floats are valid in JSON, so we cannot just use the library json-bigint to parse the JSON string. Do you have any ideas?

I have now implemented it based on CodeMirror, because it accepts strings, so I can use JSONBigNumber for processing by myself. If I want to implement it in your library, the solution I came up with is:

  1. When creating a new Differ, add additional options for users to pass their own JSON stringify and parse methods.
  2. When creating a new Differ, add additional options to determine if the data is a BigNumber. This check is to prevent iterating through BigNumber objects (values processed using bignumber.js are special objects).

Because the underlying design of your library is to accept raw JSON objects instead of strings, the above method can only be handled by users through options, and cannot be integrated into your library (the values in the transmitted object must have been processed by bignumber, otherwise precision will be lost during transmission).

njzydark avatar Feb 22 '24 11:02 njzydark