vscode-sort-json: Keys missing the double quotes after sorting
I tried sorting my vscode’s settings.json file, but I encountered a small problem.
The block that was
// ...
"search.exclude": {
".git": true,
"node_modules": true,
"bower_components": true,
"packages": true,
"**/bin": true,
"**/obj": true
},
// ...
But after the sort it was
// ...
"search.exclude": {
"**/bin": true,
"**/obj": true,
".git": true,
bower_components: true,
node_modules: true,
packages: true
},
// ...
As you can see, the double quotes are missing for the keys bower_components, node_modules and packages.
I think the extension got lost at the first key with underline and gave up entirely. The rest of the file was OK.
About this issue
- Original URL
- State: closed
- Created 7 years ago
- Reactions: 6
- Comments: 29 (14 by maintainers)
Commits related to this issue
- fix jsonc comment handling fixes #8 for commented jsonc — committed to reporter123/vscode-sort-json by reporter123 6 years ago
- fix jsonc comment handling (#19) fixes #8 for commented jsonc — committed to richie5um/vscode-sort-json by reporter123 6 years ago
I installed this extension explicitly to sort my vscode settings.json and have the same issue. Every key more than one level deep loses its quotes. I imagine this is a pretty common use case, and vscode settings use the
jsonclanguage setting (it also accepts ES6-style trailing commas). I echo the sentiment above; my default assumption would be that invalid JSON would fail to format, not format incorrectly. Sometimes the best fix for a bug is a better error message. 😃Sorting now handles comments (JSONC) in the latest version.
I agree that at the very least, instead of breaking the file it should warn the user and not perform the operation.
On Tue, Oct 22, 2019, 14:12 Yu Zhang notifications@github.com wrote:
I just had the same issue.
I see that there may be some differences between the VSCode’s JSON with comments and strict JSON format, which causes this bad output. But usually, users have big
settings.jsonfiles and are probably not aware of whether there are some easy-to-break parts (e.g. comments).One solution is to relax the strict JSON format, which you already discussed.
But I have an idea from a different angle, how about doing a quick check (e.g. whether there are comments, unexpected trailing commas in the JSON, etc.) and popping up a warning/confirmation before the formatting.
Try updating the extension 😃.
Ah. I understand now. This is because the extension uses the default JSON parser to start with, if that fails, it tries the JSON5 parser which allows non-quoted keys. Whichever parser works, it used for the output - hence why it is stripping the quotes.
I will look to see if the JSON5 parser can be forced to output in JSON format (i.e. with the quotes).
PS: I have considered adding logic to handle comments (remove them, sort, then try and place them back in the correct place). But, I really don’t have time to do that code 😃.