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

Don't remove quotes from keys #41

Closed gmccullo closed 3 years ago

gmccullo commented 3 years ago

Sorting strips quotes off property keys. That's an error.

All property names must be surrounded by double quotes. ~ Google JSON Style Guide

richie5um commented 3 years ago

When the quotes are stripped on using sort-json, that usually means your JSON is not itself strict JSON (as-in, not strict according to the internal JSON parser) - I've included two parser which means you can sort JavaScript JSON (aka without quotes and with comments etc...).

Can you post an example of the JSON you are trying to sort here?

Thanks.

gmccullo commented 3 years ago

I used it to sort my settings file. I would argue the extensions should not alter the keys regardless. Anyway here's a minimum repro:

BEFORE:

{
    "editor.rulers": [
        {
            "color": "#eee",
            "column": 20
        },
    ],
    "todo-tree.general.tags": [
        "BUG:",
    ],
}

AFTER:

{
    "editor.rulers": [
        {
            color: "#eee",
            column: 20
        }
    ],
    "todo-tree.general.tags": [
        "BUG:"
    ]
}
richie5um commented 3 years ago

Hmm.. That does look wrong. Let me take a look. Thanks.

richie5um commented 3 years ago

The problem is the trailing commas. Looks like that breaks the built-in JSON parser.

This works fine for me... { "editor.rulers": [ { "color": "eee", "column": 20 } ], "todo-tree.general.tags": [ "BUG:" ] }

richie5um commented 3 years ago

FWIW, I do plan at some point to switch over to a different parser (that can handle things like this, and comments) - the babel parser seemed best last time I looked. But, I've not had time to do the work :-|.

richie5um commented 3 years ago

New parser implemented.