4 ms·
I don't know. JSON isn't much better. { "servers": { "alpha": {"ip": "10.0.0.1", "role": "frontend"}, "beta": {"ip": "10.0.0.2", "rol
by jeroenhd 3y ago
I don't know. JSON isn't much better.
{
"servers": {
"alpha": {"ip": "10.0.0.1", "role": "frontend"},
"beta": {"ip": "10.0.0.2", "role": "backend"},
}
}
YAML also isn't great:
{
"servers": {
# Frontend server is called alpha
"alpha": {"ip": "10.0.0.1", "role": "frontend"},
# Backend server is called beta
"beta": {"ip": "10.0.0.2", "role": "backend"},
}
}
Or, depending on your preference:
servers:
# Frontend server is called alpha
alpha:
ip: "10.0.0.1"
role: "frontend"
# Backend server is called beta
beta:
ip: "10.0.0.2"
role: "backend"
I don't think XML is much better or worse:
<?xml version="1.0" encoding="UTF-8"?>
<servers>
<server id="alpha" ip="10.0.0.1" role="frontend" />
<server id="beta" ip="10.0.0.2" role="backend" />
</servers>
All of them suck in their own way. All of them work fine with autocomplete, type analysis, and autoformatting. The more things change, the more they stay the same.
JSON lacks comments, that's the biggest differentiator in my opinion.
- mianos 3y agoJson5, although not widely adopted. Has // comments and /* block comments. It also allows additional trailing , in lists. Something I hate but is a great feature if you use a macro system on top of it. (Like jinja2).
- jeroenhd 3y agoJSON5 does, but most software just does plain and simple JSON. I haven't seen it used outside some Javascript webdev environments. The JSON5 docs also seem to be specifically targeting Javascript development. If you're sticking to certain variants, you may as well use YAML, which supports JSON notation, as well as comments and various other improvements.
- mianos 3y agoIf you use python, there is an excellent json5 module. But true, it may not be as well supported by other languages. I am not sure I may as well be using yaml. I don't like it for the multiple reasons in the OP and this thread. If you are using python, I have found it to be quite easy to support both json5 and yaml, as well as converting between them for people who feel strongly about yaml. Not trivial but low effort.
- kortex 3y agoI've gotten bit by trailing commas enough times (both manual edits and writing generators) that I absolutely expect any reasonable syntax to tolerate them. It's just so much easier and more consistent to tolerate them.
- jen20 3y agoAs importantly, it reduces the noise in diffs when an item is added to a sequence.
- masklinn 3y agoYou can use prefix separators for that. Haskell-style.
- panick21_ 3y agoThat's what Clojure got right. Comma is whitespace. When you print a datastructure, it has commas. But they don't effect reading of that structure. Its brilliant and practical.
- themerone 3y agoThis is why I switched to leading commas.
- BoorishBears 3y agoWhy did you: - introduce the least idiomatic form of YAML as "depending on preference" - add an optional preamble to the XML example - add comments when there were none, but also not to all of them If you didn't artificially stretch the different examples to match, there'd be a much clearer difference between them all, especially considering the fact one of your three examples is a superset of the other. The odd one out can't even capture an integer vs a string without a schema.
- kelnos 3y agoI was puzzled by the GP's choice to write the first YAML example in a completely unidiomatic way. But I think the point of adding comments was just to show that comments are possible in some formats, and not others. Omitting possible comments from the XML example might have just been a sign of fatigue over this topic ;) At any rate, I find the (idiomatic) YAML example to be -- by far -- the most readable of all, including the GGP's TOML example.
- munksbeer 3y ago[dead]
- jeroenhd 3y ago- To showcase to the many people who aah JSON is more legible that you can use YAML as "legible" JSON - Most XML files I encounter come in this format. You can skip the preamble but it wouldn't match my real life experience. - All readable config files I encounter have comments. I forgot to add comments to the XML representation, but I can't edit my comment anymore. I think everyone who ever encountered XML knows how to add comments, though. JSON simply doesn't support comments unless you use a niche JSON derivative. As for the string versus integer problem: you always need a schema, or you'll run into very funny problems down the line. None of these formats intrinsically know what keys refer to an object and what keys refer to a string, that's all based on your schema anyway.
- BoorishBears 3y ago
- acedTrex 3y agoYaml is by far the most legible of those 3
- esbeeb 3y agoYour YAML example doesn't need double-quotes around the IPv4 addresses, but then very confusingly and problematically does need double-quotes around an IPv6 address, due to the colons. This creates a serious footgun in Ubuntu netplan, leaving a server totally unbootable, but simultaneously not triggering "netplan try" as any sort of parsing problem: https://news.ycombinator.com/item?id=37591110 https://news.ycombinator.com/item?id=37591110
- vbezhenar 3y agoCan you elaborate about colons? % echo '{"address": "2::0"}' | yq -P - address: 2::0 yq seems to be happy with unquoted colons. String with ": " inside have to be quoted, that's correct, however you won't have space in your IPv6 address unless I miss something.
- jeroenhd 3y agoAnd Javascript doesn't need semicolons in all but three exceptional cases, but I still consider it good form to strongly type values whenever possible. Getting in the habit of not doing so will lead to schema violations, like the Netplan problem you linked, which can crash the program trying to read your config. If it bails out at an unfortunate time, like most networking tools seem to do, you'll need to use a recovery boot image or serial console to fix your config.
- Alifatisk 3y ago> This creates a serious footgun in Ubuntu netplan, leaving a server totally unbootable, but simultaneously not triggering "netplan try" as any sort of parsing problem: Been there, done that. A good config file or linter should’ve complained and not allowed me to commit such misstake.
- ActorNightly 3y agoJSON is by far the easiest to reliably parse. It doesn't rely on tabs or spaces which YAML suffers from. XML is just more verbose JSON without an array object, and has some redundancy in spec which is not a good design. Lack of comments for JSON isn't a huge issue considering you can make the keys fairly verbose. And it would be actually pretty easy to add this into the spec, and parsers would still be backwards compatible.
- perryizgr8 3y ago> And it would be actually pretty easy to add this into the spec I present jsonc: https://www.npmjs.com/package/jsonc https://www.npmjs.com/package/jsonc
- gnfargbl 3y agoLack of comments is a massive issue for JSON as a config format. Making keys verbose tells you what a thing is, but comments are there to tell you why a thing is. The only real way around this seems to be to create some kind of a "_comment" key.
- Thorrez 3y agoYour first JSON example has a trailing comma. I thought that doesn't work.
- jeroenhd 3y agoCrap, you're right, JSON doesn't do that. I can't edit my comment anymore, though.
- dgan 3y agoJust for fun: (servers ;; lispy comment ((name alpha) (ip 10.0.0.0) (role backend) (notes "very large multiline string") ((name beta) (ip 10.0.0.1 10.0.0.2) (role frontend)) )
- marcyb5st 3y agoPrototxt/protobuffers for the win. Seriously though, having the definitions that validate your message is awesome.
- Too 3y agoNow add some multi-line strings. None of them handles it well. YAML has 7 different modes to do it so you will inevitable mix up and use the wrong one, otherwise it's actually the only option that supports it. Json requires inlining \n. Xml only does it with whitespace indentation.