-
Notifications
You must be signed in to change notification settings - Fork 666
Description
Problem
The web-build-tools repo has used .json config files for a long time. We've always put // comments in these files, because the proposition of an undocumented config file is preposterous and unprofessional. :-P
However, GitHub's syntax highlighter has the differing opinion that JSON standard (despite ironically being a REQUEST FOR COMMENTS) does not allow comments. And despite the father of JSON himself saying that comments are fine. It's making us look bad, because all our config files get red highlighting everywhere, like this:
/**
* This is the main configuration file for Rush.
* For full documentation, please see https://rushjs.io
*/
{
"$schema": "https://developer.microsoft.com/json-schemas/rush/v5/rush.schema.json",
/**
* (Required) This specifies the version of the Rush engine to be used in this repo.
* Rush's "version selector" feature ensures that the globally installed tool will
* behave like this release, regardless of which version is installed globally.
*
* The common/scripts/install-run-rush.js automation script also uses this version.
*
* NOTE: If you upgrade to a new major version of Rush, you should replace the "v5"
* path segment in the "$schema" field for all your Rush config files. This will ensure
* correct error-underlining and tab-completion for editors such as VS Code.
*/
"rushVersion": "5.5.4",Investigation
Why doesn't JSON allow comments? It was originally intended as a machine-to-machine data format. Back when JSON was proposed, XML was already the accepted standard for large config files intended to be maintained by humans. Humans authoring JSON is a relatively new usage scenario that arose mainly with NodeJS, due to its lack of a decent XML library.
It seems like the JSON standard isn't going to fix this problem. I did some research, and here's the most obvious alternatives:
-
YAML: I don't like YAML because (1) the syntax has many redundant ways to express a given thing, so I've observed people constantly having trouble remembering exactly what the different symbols mean, and (2) parsing all this YAML grammar requires a library that is large and slow, and (3) YAML doesn't have a mature schema facility; they expect you to convert YAML to JSON and then use a JSON schema validator. I will say that I do like YAML as an output format (e.g. shrinkwrap.yaml) because it is concise and easy to diff, and the library allows you to disable the weird notations.
-
Hjson: This seems somewhat popular, however the syntax seems pretty counterintuitive and error-prone to me. For example,
x: ab efis translates to"x": "ab ef", whereasx: [ab ef]does not translate to"x": ["ab ef"]or"x": ["ab", "ef"]. -
jsonc: Not very popular. Its syntax is different from Hjson, and also seemed counterintuitive and error-prone. For example
hey: [this needs no commas]translates to"hey": ["this", "needs", "no", "commas"]and{a : b c : d}translates to{"a": "b", "c": "d"}. (Also the name is confusingly similar to https://github.com/json-c/json-c which is a JSON parser written in C.) -
JSON5: Out of the gate, JSON5 has the advantage that it's not trying to invent anything new. Their idea is to carve out a JSON-like subset of the ECMAScript 5 grammar that is already standardized and familiar to everyone. It's better than ECMAScript because this is a pure data representation (e.g. that can be described by a conventional JSON schema), and it can be read and rewritten with a relatively simple parser. Conveniently, the @microsoft/node-core-library
JsonFileAPI is based on jju which already supports JSON5. (I didn't realize this, but you can already put JSON5 notation inrush.jsonand Rush doesn't complain. Oops!)The one concern I had with JSON5 was that ECMAScript is continually evolving: Their file extension is
.json5-- will we have to come back every year and rename all our file extensions to.json6or.json7or .json8with each new JavaScript standard? Nobody does that with.jsfiles. I opened an issue to ask about this, and they had a reasonable argument that newer ECMAScript versions have relatively little to contribute to the syntaxes relevant to JSON.
Proposal
- Let's convert all our config files to use JSON5.
- Let's rename the file extension to
.json5so GitHub highlights it correctly - Let's fix the places where we write
.jsonwith non-JSON content (e.g. issue Ensure schema JSON documents are valid JSON #996)
What do you think? Comments welcome...
@mike-north @hogmoru @iclanton @daspek @patmill @dzearing @cliffkoh @kenotron
Metadata
Metadata
Assignees
Labels
Type
Projects
Status