richie5um / vscode-sort-json

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

floats are converted to ints #65

Closed Spastika closed 2 years ago

Spastika commented 2 years ago

WHAT I DID AND WHAT HAPPENED

When I use ver 1.20.0 to sort the ORIGINAL object below by key-length, all the floating-point values are converted to integers as per the OUTPUT object.

ORIGINAL

{
   "Neque": 1.0,
   "porro": 1.0,
   "quisquam": 1.0,
   "est": 1.0,
   "qui": 1.0,
   "dolorem": 1.0,
   "ipsum": 1.0,
   "quia": 1.0,
   "dolor": 1.0,
   "sit": 1.0,
   "amet": 1.0
}

OUTPUT

{
   "est": 1,
   "qui": 1,
   "sit": 1,
   "quia": 1,
   "amet": 1,
   "Neque": 1,
   "porro": 1,
   "ipsum": 1,
   "dolor": 1,
   "dolorem": 1,
   "quisquam": 1
}

WHAT I EXPECTED

I would have expected the values and their types to be preserved:

{
   "est": 1.0,
   "qui": 1.0,
   "sit": 1.0,
   "quia": 1.0,
   "amet": 1.0,
   "Neque": 1.0,
   "porro": 1.0,
   "ipsum": 1.0,
   "dolor": 1.0,
   "dolorem": 1.0,
   "quisquam": 1.0
}
richie5um commented 2 years ago

I've done some investigating, and because all numbers in javascript (hence json) are the same type, this is how Javascript handles stringifying of JSON - which is what my extension does. Any values after the dot are preserved, so 1 === 1.0 in this regard. Is there a problem with losing the ".0" ?

Spastika commented 2 years ago

It changes the underlying implied JSON data type. This could invalidate a JSON document against a specified JSON schema.

richie5um commented 2 years ago

JSON only supports 'number', so the data-type is the same.

Spastika commented 2 years ago

Okay, I see and concede - my mistake. Thanks a lot for taking the time investigate (and in the process educating me). (and thanks for vscode-sort-json)

richie5um commented 2 years ago

Thanks for the comment and messages - was useful for me too.