richie5um / vscode-sort-json

VSCode Extension to Sort JSON objects
https://marketplace.visualstudio.com/items?itemName=richie5um2.vscode-sort-json
MIT License
107 stars 20 forks source link

Large integers are modified after sort #58

Closed Shapedsundew9 closed 2 years ago

Shapedsundew9 commented 3 years ago

For example: { "big_+_int": 9223372036854775807, "big_-_int": -9223372036854775808 } becomes { "big_-_int": -9223372036854776000, "big_+_int": 9223372036854776000 } after using an alphanumeric sort (not tried other sort keys)

VS Studio version Version: 1.60.0 Commit: e7d7e9a9348e6a8cc8c03f877d39cb72e5dfb1ff Date: 2021-09-01T10:43:02.136Z Electron: 13.1.8 Chrome: 91.0.4472.164 Node.js: 14.16.0 V8: 9.1.269.39-electron.0 OS: Linux x64 5.4.0-81-generic

SortJSON version Released on 28/06/2016, 07:50:49 Last updated 20/03/2021, 09:12:18 Identifier richie5um2.vscode-sort-json

rogerluan commented 2 years ago

I'm facing the same issue. SUPER weird, and I was analyzing the data (comparing with another similar json) and couldn't figure out why the IDs were different, until I realized the json sorter was changing the numbers.

Here's how to reproduce:

Input JSON file:

{
    "id": 804184974949614240
}

Sort it via "Sort JSON" action:

image

Now the value will be:

{
    "id": 804184974949614200
}

Note the difference is the end "…240" vs "…200".

Hope this helps 🙏

richie5um commented 2 years ago

Thanks. I’ll take a look.

richie5um commented 2 years ago

This looks to be a general js issue, as I can repro without my code...

image

richie5um commented 2 years ago

image

https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Number/MAX_SAFE_INTEGER

You should be using BigInt for numbers greater than the safe value, but they don't stringify.

image

rogerluan commented 2 years ago

@richie5um Nice find, it's weird that js's int has a max value of 53 bits instead of 64... But I'm confused, what is the solution here? Shouldn't vscode-sort-json implementation interpret all integers as BigInt then?

richie5um commented 2 years ago

It appears, from my brief investigation, that BigInt is a Javascript thing, and it isn't part of the JSON spec. So, unfortunately, nothing I can do in sort-json. Let me know if you think otherwise.

rogerluan commented 2 years ago

I don't know the inner works of this plugin, but if I were to guess, I'd say that it interprets the text in the text editor, possibly map its json into a data structure (like a js json-like structure, which idk what it'd be - in other languages those are called hashes, or dictionaries), and then you manipulate the data structure and print it out in a different order, following predefined rules.

My idea is that it is interpreting numbers (integers, more specifically) as javascript's Int, and it could instead interpret them as (or wrap them in) BigInt. Wouldn't that solve it?

You're right in assuming that there's no BigInt representation in json... to JSON there's no limit to how large an integer can be, it could have 5000 digits, but the limitation would be on the interpreter's end (e.g. javascript web page, iOS app (Swift), Android app (Java/Kotlin), etc). The latter two can interpret integers up to 64 bits without no extra work, fwiw - probably that's the same for most of the other programming languages. So this json sorter/formatter should be able to interpret at least 64-bit integers... With larger integers like 128 or 256 or arbitrary-length ones being nice-to-haves.

WDYT @richie5um ?

richie5um commented 2 years ago

Thank you for the detailed suggestion... however... the tool is intended for sorting JSON, so I think the current logic - albeit insufficient in your needs - is correct.