by fabiospampinato · 7y ago · 0 comments
IMHO JSON5 (JSON with comments basically) is fine for config files, VS Code is using this and they seem to be doing alright.
Addressing the article's points:
- Lack of comments: not an issue with JSON5.
- Readability: I think it's pretty readable. Are the quotes, semicolons and commas really an issue? Not for me.
- Strictness: just edit it with an editor that points at the errors quickly. Escaping quotes might be annoying, but I can't think of a possible alternative format where escaping isn't required at all.
- Lack of programmability: I've never had any problem with this, and again pretty complex and customizable applications like VS Code seem to be doing fine without this. This may even be considered a feature, as it forces you to keep the configuration well separated from the actual logic.
Because it was designed to be a human-editable configuration file format, it has a simple and intuitive format that is easy for both humans and machines:
[Section]
; comment
Key1=Value
; comment
Key2=Value
[Section2]
Key1=Value
It is also easy for machines to non-destructively edit, maintaining existing comments and formatting by humans, and applications take advantage of that. That is a big plus in my opinion.Good for: comments, readability if keys limited, familiarity.
Comments might work for a parser but not on another. What's the point when you can do better with TOML?
Yeah, but now you have to find a tool that supports that. Easy on small projects, but try being at a company supporting many languages and frameworks.
> - Readability: I think it's pretty readable. Are the quotes, semicolons and commas really an issue? Not for me.
If you haven't tried TOML, you're missing out. Why on earth would you put up with syntax (hard to author, at that) that isn't necessary? Syntax distracts from the actual problem that this is trying to solve. JSON is fine for machine read/write, but horrible on humans.
> - Strictness: just edit it with an editor that points at the errors quickly. Escaping quotes might be annoying, but I can't think of a possible alternative format where escaping isn't required at all.
What if you have an issue in prod and need to fix something quick? You might only be able to open it up in a stripped-down vi, or if the machine is locked down you might only be able to materialize a plain text view. What if not everyone uses the same tool? This imposes a further constraint. Yet more concessions pointing to how horrible JSON is for humans.
> - Lack of programmability: I've never had any problem with this, and again pretty complex and customizable applications like VS Code seem to be doing fine without this.
Can you imagine projects where this might be an issue? These aren't the only codebases in the world. And just because VSCode appears to be doing fine doesn't mean they don't second guess that decision. They might just be locked in at this point.
TOML is decent but if you need nested data it gets super awkward.
> What if you have an issue in prod and need to fix something quick? You might only be able to open it up in a stripped-down vi, or if the machine is locked down you might only be able to materialize a plain text view. What if not everyone uses the same tool? This imposes a further constraint. Yet more concessions pointing to how horrible JSON is for humans.
JSON5 is pretty easy to edit correctly with any text editor. Not much harder than TOML.
> Can you imagine projects where [lack of programmability] might be an issue? These aren't the only codebases in the world. And just because VSCode appears to be doing fine doesn't mean they don't second guess that decision. They might just be locked in at this point.
Does TOML allow programmability that JSON5 doesn't?
And if you're editing a file, you can usually tell by the fact that there are comments in it. :)
- static config
- environment specification
- runtime feature flagging
- semi-static control plane config
- rollout weights
Etc.All of these have different characteristics.
Infrequently changed, a priori config that doesn't need to be changed in emergencies is suitable for baking in at compile time.
Just fix it quick somewhere that’s not prod. Snowflaking has not been a good practice for a long time now, so arguing that a certain tool makes it easier is a bit of a moot point.
There are formats that are easier to edit than others and formats that are easier to parse than others. But sooner or later all of them end up being created by an ill-suited template engine or embedding a programming language in the syntax (cloudformation, ansible, hcl2).
Why not treat the config file as a generated artifact that is readable but not written by a human. Tools like jsonnet (https://jsonnet.org/), dhall-lang (https://dhall-lang.org/), or my personal side project ucg (https://ucg.marzhillstudios.com/) allow you reuse values and safely generate your configuration formats with comments and logic all while bypassing the pitfalls of most formats.
They give you a purpose built language for creating configurations with comments and logic w/o putting that burden on your application later. And it allows you to ignore the parts of a particular format that cause you problems.
It's just a config file, no need to overthink this with translation layers IMO.
If you only have one application then it's overkill. When you have 10's of applications it's a life saver.
I think this makes sense as far as it goes.
The first choice, no matter what the config file format, should be editing within a tool. The tool can check values, be accessible, assist in coordination, etc.
However, I think it's essential that the intermediate format be human readable and have the ability to contain comments. Because if something in the toolchain goes wrong, determining what the settings are right now on a particular system will be very useful. Being able to modify those settings by hand, though not your first choice, may save time and money and (depending on the system) lives.
Also, if the tool breaks, you can still change configs (though obviously this is not what you want).
Essentially, you should have a tool that edits config values with full explanations of their purpose. That tool should output a machine-readable format (JSON5, YAML, TOML) that supports comments with each value preceded with the comments containing their explanations and, ideally, a list of comments made when changing config values with previous values listed.
You get the best of "both worlds."
I am the author. It depends on your purpose; in general, I would say "the simplest solution that meets all needs", where "all needs" depends on what you're doing. In Python, this could be the configparser module, or parsing a Python file (again, depending on requirements).
As a matter of taste, I prefer minimal syntax. I wrote a library with this syntax a while ago[1], although it's clearly not the best solution for every case.
I should probably write a separate article about this.
[1]: https://github.com/carpetsmoker/sconfig#what-does-it-look-li...
> Lack of comments: not an issue with JSON5.
JSON5 isn't JSON though. It's JSON5. None of the standard JSON parsers can deal with JSON5, you need a different JSON5 parser.
> I think it's pretty readable
A matter of taste.
> just edit it with an editor that points at the errors quickly
It seems to me that having to rely on such tools for issues such as this is not a great idea, especially not if it's easy to do better.
> I can't think of a possible alternative format where escaping isn't required at all.
There is a difference between "escaping isn't required at all" vs. "having to constantly and awkwardly escape a commonly used character".
> Lack of programmability [..] VS Code seem to be doing fine without this.
I've not used VSCode, As I'm a Vim guy. I can't imagine configuring Vim with just declarative configuration as so many useful parts come from being able to program Vim. This could be separated out to just an "extension" or "plugin" system, but I really like being able to just stick one or two lines of code in my vimrc and have it do something really useful, without writing a full plugin.
There are trade offs with the various formats. Think about your use cases and pick the one that fits them the best.
TOML is my personal favorite.
YAML feels like 10 steps back; no parser I've ever used has been able to accurately give me a line number for an error, white space issues make it impossible to work with without special editor features, and some of the types are just cryptic. I spend most of my time that's spent editing configs in vim over SSH and it can be challenging at times.
TOML resolves a lot of these issues in my experience but I'd still argue that by virtue of its capability, is still quite complex when trying to define configs for new applications.
The only complaint is that the name TOML isn't as cool as other formats.
When trying out a CLI I've been writing recently, this time on Windows... the TOML config file (generated on first run) wasn't usable.
The problem turned out to be TOML not handling single slashes "\" in strings, instead treating them as escape characters.
Annoying as on Windows the paths are (eg): "C:\Program Files\stuff". Which then needs changing to "C:\\Program Files\\stuff" to work ok.
I can see both good and bad points of TOML's string handling there. But it's annoying it needs a workaround for one of the signature aspects (slash vs backslash) of windows. Especially when paths are very common in config files, regardless of OS. :/
A sqlite file isn't an ascii text file that you can edit with vi, so that suggestion is likely to get a lot of jeering, but it's a serious suggestion. It's already being used for configuration on all mobile platforms. There exists a wide range of applications, particularly for windows users, where the common user is not a developer and is not meant to be editing configuration by hand.
Furthermore, access to the sqlite3 binary is very nearly as widespread as access to a vi binary. And self-styled developers who don't know SQL aside, it actually is a rather convenient format to edit by hand. Machine transformation of the config is obviously straight forward, and sqlite databases tend to be self-documenting anyway. Sane table and row names go a long way (obfuscated tables and rows would be roughly equivalent to obfuscated keys in an ini file...) and if that's not enough, comments for tables and rows can be stored in the schema.
The one downside is strictness; sqlite doesn't enforce types. It does however have data constraints, which help.
Seems like YAML won.
But TOML seems to be heavily used in Rust.
Also there is SANE.
${service}.${env}.example.comThanks for pointing me to that - I'm working in Scala on server-side and JS on browser-side for a couple of years now, and sharing config would be nice. Still need object merging and value concatenation, which hocon-js doesn't support, but I'll keep an eye on it!
How about "J4X" (JSON for XML), that lets you embed XML in JSON the same way the ever-popular "E4X" (ECMAScript for XML) lets you embed XML in ECMAScript?
https://developer.mozilla.org/en-US/docs/Archive/Web/E4X
J4X Example:
<!DOCTYPE j4x PUBLIC "-//W3C//DTD J4X 1.0 Transitional//EN" "http://www.w3.org/TR/j4x/DTD/j4x-1.0-transitional.dtd">
{
april: <fools late="13 days"/> <!-- sorry! -->
}One nifty way of doing this is custom quote sequence.
In C++ it works like R"delimiter( raw_characters )delimiter"
As delimiter can be anything it can be chosen not to conflict with the payload.
Bash has had the same trick for here documents for a long while.
Many years ago I worked at a job that made an XML tag-based language that was turing complete with the intent of it making their configuration "smarter". It ended up making the entire project much more difficult to debug, trace through, and even understand at all.
Link for HJSON: https://hjson.org/
Compare to JSON5: https://json5.org/
HEREDOC-like format:
#qweqwe123" string where everything is unquoted and is terminated by a " followed by qweqwe123. you can chose a short relevant sequence of alphanumeric chars as needed"qweqwe123#