EDIT from 2019: Hi folks. I wrote this gist for myself and some friends, and it seems like it's gotten posted somewhere that's generated some (ahem, heated) discussion. The whitespace was correct when it was posted, and since then GitHub changed how it formats (thank you @anzdaddy for suggesting a formatting workaround) honestly this is a random throwaway gist from 2015, and someone more knowledgable about this comparison should just write a proper blog post about it. If you comment here I'll hopefully see it and stick a link to it up here. Cheers. @oconnor663<pre>
tags. Look at the raw text if you care about this. I'm sure someone could tell me how to fix it, but
Here's the canonical TOML example from the TOML README, and a YAML version of the same.
title = "TOML Example" [owner] name = "Tom Preston-Werner" dob = 1979-05-27T07:32:00-08:00 [database] server = "192.168.1.1" ports = [ 8001, 8001, 8002 ] connection_max = 5000 enabled = true [servers] [servers.alpha] ip = "10.0.0.1" dc = "eqdc10" [servers.beta] ip = "10.0.0.2" dc = "eqdc10" [clients] data = [ ["gamma", "delta"], [1, 2] ] hosts = [ "alpha", "omega" ] |
title: YAML Example owner: name: Tom Preston-Werner dob: 1979-05-27T07:32:00-08:00 database: server: 192.168.1.1 ports: [ 8001, 8001, 8002 ] connection_max: 5000 enabled: true servers: alpha: ip: 10.0.0.1 dc: eqdc10 beta: ip: 10.0.0.2 dc: eqdc10 clients: data: [ [gamma, delta], [1, 2] ] hosts: - alpha - omega |
Sensitivity to whitespace is not entirely just preference, it is a tradeoff. YAML and Python treat whitespace as a meaningful part of the syntax, with the intention of making correct documents (or programs in the case of Python) also look nice and convey, visually, semantics with the indentation. However, in doing so, incorrect whitespace (including some cases of using spaces instead of tabs) also means a broken document, and sometimes in a not-very-obvious way.
The fact that the OP accidentally posted the YAML snippet with incorrect whitespace is a great example of how this approach can bite you... It's a simple mistake that results in something being broken in a non-obvious way. This doesn't make the approach wrong, but it does mean that whitespace errors will break documents in sometimes strange ways. You have to decide if the visual nicety of enforced indentation is worth that price.
I vote for TOML because this whitespace issue is IMO the most relevant difference and I think TOML gets it right.
(And FWIW, I think the Go language syntax gets it right - the compiler treats most white space as the same but there is a canonical format which can be easily enforced with
gofmt
[usually you set your editor up to call this during each save] - so programs generally have nicely formatted whitespace, but throwing in some extra spaces or tabs does not break things. I wish more syntaxes would adopt this approach.)