3 ms·
At this point, I even prefer plain JSON to YAML. What pushed me over the edge is that "deno fmt" comes with a JSON formatter, but not a YAML formatter. It's a
by jrockway 3y ago
At this point, I even prefer plain JSON to YAML. What pushed me over the edge is that "deno fmt" comes with a JSON formatter, but not a YAML formatter. It's a single binary that runs in milliseconds. For YAML auto-formatting you basically have to use Prettier, and Prettier depends on half of NPM and takes a good 2 seconds to startup and run. So, I literally moved every YAML file in our repository at work that could be JSON to JSON and I think everyone has been much happier. Or, at least I have been, and nobody has complained to me about it.
Various editors also support a $schema tag in the JSON. I added this feature to our product (which has a flow that invokes your editor on a JSON file), and it works great. You can just press tab and make a config file without reading the docs. Truly wonderful.
YAML has this too with the YAML language server, but you need your tab key to indent stuff, so the ergonomics are pretty un-fun. JSON isn't perfect, but at least the text "no" is true.
- matsemann 3y agoAt work we're currently expanding to another country. Which means that many services now need a country label etc., which is fun when you're adding "no" to all our existing services. Luckily it's quick to catch, but man... why?
- throwaway894345 3y agoYeah, I'm pretty sure there are exactly two substantive problems with JSON for (static) configuration file use cases, which are comments and multiline strings (especially with sane handling of indentation). YAML fixes these, but it adds so much complexity in the process including such a predictable footgun of unquoted strings (the no/false problem is particularly glaring/absurd, but it's also easy to forget to quote other boolean values or numbers in a long list of other strings).
- radlad 3y agoCan I add "trailing commas are invalid" to the list?
- throwaway894345 3y agoPlease do.
- MathMonkeyMan 3y agoSolutions abound, but one option is to use either Javascript (config.js): // comments! ({ no_quotes: [1, 2, (() => /* code! */)()], ... }) Or, let the whole thing be a function. Then your config can have parameters, maybe mapped from environment variables or something. ({foo, bar, ...kwargs}) => ({ datacenter: foo === 'old' ? 'useast1' : 'uswest', ... }) Can do as Crockford says, and write in the JSON subset of Javascript, but with comments, and convert it to JSON by running it through a JS minifier. I think you need parens around the object, though, or else it looks like a code block (boooo...): ({ // This is very much like JSON "foo": [1, 2, "bar"] }) Python also has JSON-like syntax, so you could use config.py: { 'foo': [1, 2, 'bar'] } That would require a wrapper script. Or, you can have the self-contained convention: import json import sys # Yay, comments! json.dump({ # more comments! 'foo': [1, 2, 'bar'] }, sys.stdout)
- throwaway894345 3y agoYeah, I'm mostly just not sure I want to put a full programming language interpreter in my application, especially Python which is not designed to be embeddable. Moreover, I would really want something that is typed, like TypeScript, but libraries for embedded TypeScript interpreters are even more rare :/.
- MathMonkeyMan 3y agoI figure we'd leave it to the build/deploy/CI/development whatever system. I also don't want to extend or embed my application with a full-blown runtime if I don't have to. "source" config --> convert to JSON config "on the fly" --> app that expects JSON edit: I worked on a C++ team that used `std::system` to invoke the system python interpreter when loading a config file. My teammates weren't morons, either, it's just the simplest thing that worked and they knew that the config script and the surrounding file system were secure.
- LorenzoGood 3y agoOr you can produce Configuration with nix
- bobbylarrybobby 3y agojson5 is pretty good, if you can use it
- bilalq 3y agoI prefer JSON to YAML as well. The lack of comments is a problem though. But I feel like this is a false dichotomy. Both kind of suck for this need, but I can accept that JSON is at least reasonable to work with if you need language agnostic config.
- DecoySalamander 3y agoAFAIK Prettier has 0 dependencies and runs fast enough that triggering formatting on save wasn't ever noticeable (granted I've never tried it with YAML specifically). Curious what kind of setup you had to push it to 2 seconds - maybe bulk formatting in CI for whole repository?