3 ms·
YAML - I am not a big fan these days. Perhaps I am have OCD on format, but with YAML ordering is “up to the user”. At least INI has a somewhat “header/section”
by yeukhon 9y ago
YAML - I am not a big fan these days. Perhaps I am have OCD on format, but with YAML ordering is “up to the user”. At least INI has a somewhat “header/section” so it looks more organized. The “yes Yes True TRUE true 1 => True (python)” is flexible but can be seen as negative if again you are like me OCD. You can enforce style guideline in your dev team, but for end-user, probably worth reconsidering your strategy.
The reasons I’d consider JSON for configuration are (1) when the configuration is really simple and short, and (2) I don’t want an extra dependency. You don’t want to handcraft for a larger data structure, and context switch between your terminal and JSON validator.
I like INI-style configuration file. But ConfigParser’s API is horrible, and everyone seems to like tweak and invent their own “INI” format.
Instead, for those really need a good configuration file, I recommend TOML [1].
For data file, either YAML or JSON are fine. But each comes with gotcha. Trailing comma in JSON is invalid (which is probably #1 “wtf what’s wrong with my json”). For YAML you need to be very careful with “do I want an int or do I want a string.”
[1]: https://github.com/toml-lang/toml https://github.com/toml-lang/toml
- sasa_buklijas 9y agoNow I use https://github.com/pasztorpisti/json-cfg https://github.com/pasztorpisti/json-cfg for storing configuration as JSON. json-cfg has the ability to add comments to JSON file.
- Kamshak 9y agoHow does TOML solve the “do I want an int or do I want a string." part? I only know YAML and this caught me a few times, would love to know how toml does it.
- yeukhon 9y agoNone of them do, sorry if I was being too casual. The reason I named YAML because in YAML you can just write name: bob where bob is assumed to be a string by the YAML parser. This is bad if you use Ansible because "bob" could be the name of a variable. bob: "I am bob" name: bob # at runtime Ansible sees 'name: "I am bob"' But both JSON and TOML need the users to be more explicit. So while users still need to be mindful of "1" vs 1, JSON and TOML don't assume as much as YAML does. Let me show you in Python. import toml import yaml s1 = "name: bob" yaml.load(s1) --> {'name': 'bob'} s2 = "name = bob" toml.loads(s2) Traceback (most recent call last): .... File ".../python2.7/site-packages/toml.py", line 664, in _load_value v = int(v) ValueError: invalid literal for int() with base 10: 'bob' s3 = "name = bob eve" toml.loads(s3) Traceback (most recent call last): raise TomlDecodeError("This float doesn't have a leading digit") See how a space and without space yield a different exception different? Not sure if it's an implementation problem, or the spec says so though. But the point is TOML doesn't assume "bob" without a quote is a string, which is a good thing.