RexSkz / json-diff-kit

A better JSON differ & viewer, support LCS diff for arrays and recognise some changes as "modification" apart from simple "remove"+"add".
https://json-diff-kit.js.org
MIT License
140 stars 12 forks source link

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

Open njzydark opened 9 months ago

njzydark commented 9 months ago

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
RexSkz commented 9 months ago

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?

njzydark commented 9 months ago

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).