![]() If your configuration doesn’t have any strings that are too long to fit on a line, this isn’t a problem. If you want newlines in the string, you have to escape them with “\n”, and what’s worse, if you want a string that carries over onto another line of the file, you are just out of luck. Long StringsĪnother problem with JSON as a configuration format is it doesn’t have any support for multi-line strings. If you need commas after each pair, it should at least accept trailing commas, since trailing commas make adding new entries to the end easier and lead to cleaner commit diffs. Speaking of commas, JSON doesn’t accept trailing commas. Generally, you will have a single key-value pair per line, so it would make sense to accept a newline as a delimiter. The commas between key-value pairs are also mostly unnecessary in config files. But, for a configuration file, the outermost braces are just useless clutter. In particular, for configuration files, the keys in objects are almost always identifiers, so the quotation marks around the keys are redundant.Īlso, JSON requires curly braces around the entire document, which is part of what makes it an (almost) subset of JavaScript and helps delimit different objects when multiple objects are sent over a stream. There is a lot of punctuation that doesn’t aid human readability, although it does make it easier to write implementations for machines. Low Signal to NoiseĬompared to many other configuration languages, JSON is pretty noisy. ![]() Its restrictiveness is part of what makes it easy to implement a JSON parser, but in my opinion, it also hurts the readability and, to a lesser extent, writability by humans. The JSON specification is pretty restrictive. However, this is non-standard, and many editors don’t properly handle comments in JSON files, which makes editing them a little harder. For example, Ruby’s JSON module and the Java Jackson library with the _COMMENTS feature enabled will handle JavaScript-style comments just fine in JSON input. Some JSON libraries do allow comments as input. Of course that does add some additional work to editing configuration, so if you are creating an application that parses a configuration file, don’t depend on your users being able to use that. If you are using an application that requires JSON configuration, I recommend that you do just that, especially if you already have any kind of build step before the configuration is used. Douglas Crockford (the inventor of JSON) suggests using a preprocessor to remove comments. However, this syntax isn’t very readable, and in order to include more than one comment in a single object, you need to use unique keys for each. One common workaround is to use a special key in an object for a comment, such as “//” or”_comment”. There are, of course, workarounds for adding comments to JSON. If you think of JSON as a data interchange format, then it doesn’t really make sense to have comments. Comments are necessary to annotate what different options are for and why a particular value was chosen and-perhaps most importantly-to temporarily comment out parts of the config while using a different configuration for testing and debugging. One feature that is absolutely vital for a configuration language is comments. ![]() ![]() It’s too bad that this ubiquitous format is so ill-suited for configuration. These are actually all pretty good reasons. And that’s not to mention all the existing tooling for JSON, including syntax highlighting, auto-formatting, validation tools, etc. Then there is the fact that developers and users are probably already familiar with JSON and don’t need to learn a new configuration format to use the product. Many languages have JSON support in the standard library, and those that don’t almost certainly have an easy-to-use JSON package readily available. The biggest reason is probably that it is easy to implement. There are several reasons why JSON is used for configuration files. Why is JSON popular as a config language? But as a configuration language, it falls short. It is a flexible format that is relatively easy for both machines and humans to read, and it’s a pretty good data interchange and storage format. However, JSON is actually a pretty terrible configuration language for a number of reasons. Perhaps the most obvious example is the package.json file used by npm and yarn, but there are many others, including CloudFormation (originally JSON only, but now supports YAML as well) and composer (PHP). Many projects use JSON for configuration files. Why JSON isn’t a Good Configuration Language
0 Comments
Leave a Reply. |