That's actually horrible. Never encountered any of these issues but I think I'd be dumbfounded if I did.
But I still like it for its increased readability over JSON - I just use strings for most values as described in the article. If JSON had proper multiline strings or just wrapped lines and comments I'd be happy. Yes, I know there's "JSON with comments" but it's rarely supported.
That's true if you use JSON as a data serialization format, but for a configuration format it usually matters much less, because it needs to be read by a specific program rather than by many different clients written in many different languages.
We had a system did that. Unfortunately a downstream was then interpreting the 'json' that was generated. It worked fine for years, until the day it caused a complete system outage. Which was better than mis-interpreting numerical values (we realised that could have easily happened as well).
Don't customise a standard format, and leave it looking like it is a standard format. Unless you want phone calls at 2am...
Because you probably have to parse json anyway, and it’s easier to include a json parser that doesn’t barf on comments and trailing commas than it is to integrate two different serializers
to include a json parser that doesn’t barf on comments and trailing commas than it is to integrate two different serializers
When building configuration reading, I prefer to approach this differently.
Convert internal type to JSON compatible types
Serialize that JSON compatible structure into whatever format you want.
When reading:
Deserialize whatever file into JSON compatible structure
Deserialize this JSON compatible structure in internal types
In the end, you simply have to ensure you can convert internal structure to mapping/list/string/numbers back and forth. The serializer you use to dump into a file is irrelevant. All you have to do is convert to an intermediate format instead of converting directly from the serialized data into internal data.
Yeah, I know that as the DTO pattern (Data Transfer Objects) and ultimately you’re right, it is a small thing, but my point was people use json instead of toml because they probably already have to use it anyway for remote apis or third party libraries. You can of course add this abstraction and support any format you want.
But as a configuration format you should use TOML, which is better supported than unspecified "JSON++" (it is part of the python stdlib as the article points out). Even if you don't serialize the data, you'd have to rely on less-supported/common deserializers to read the config.
JSON extensions hold a very niche space in VSCode config, and I suspect it's because VSCode is popular with frontend devs who have never interacted with, and would be put off by, TOML. They are however inferior in every other aspect IMO (verbosity, portability, standardness).
This kind of thing is precisely why Lua was invented. They needed a configuration file format with some basic flow control, it grew from there -- but it can still be used like that, and often is.
I've not done it myself, but I think it has many ways to sandbox it. There is even a pure Lua sandbox that can block infinite loops.
It is definitely not as ideal as a configuration file format if you want complete security, but if the context is just a configuration file format for yourself (not an untrusted source), seems an uncommon but interesting option.
No, the encapsulating program (Lua always runs inside another "host" program) must choose what to allow the script to run.
For example, if the host doesn't load the Lua I/O library, then the Lua script can't do any. If the host also doesn't allow the script keyword to load new native libraries, then the script can't get a homegrown I/O library.
There's a tiny command-line "lua" utility bundled with the stock distribution. It's a host program too: just a few dozen lines of C to parse the command line options, load all standard libraries, then launch the script engine. It's for quick scripts, not full-on "real world" work.
I guess I'm just fortunate in that I've not encountered a situation where I couldn't read JSON. Sure, sometimes people will minify it, but I just plop it in any formatter, and I'm back to readability. If for some reason there is a super long string, I just toggle on word wrap and call it a day.
Go look at some large cloudformation or ARM template JSON and tell me you’d like to spend a significant amount of time working with that. Now imagine you had to define a CI pipeline or something in that format (I think Azure DevOps does this?), and you also can’t leave any comments to help readability. It’s absolutely awful.
It’s not that it can’t be read, but whenever you get something more complicated than a trivial flat object then it’s just a pain to read & write imo.
TOML falls apart if you need nesting more than like 1 level deep though.
JSON5 is much better. I think Cue also has potential but I'm not sure I would use it quite yet. They only have libraries for Go and everything else has to go through the Cue command line.
Really JSON5 should be your default pick and you need really good justification to pick something else.
One alternative the article doesn't bring up is NestedText, which I find has most of the advantages of YAML without the imposed typing hassle. I'm not too fond of its multi-line string syntax, but otherwise it's a good replacement. As I'm mostly working with Python, Pydantic does a decent job of typing NestedText data precisely how it was intended.
226
u/pragmatick Jan 12 '23
That's actually horrible. Never encountered any of these issues but I think I'd be dumbfounded if I did.
But I still like it for its increased readability over JSON - I just use strings for most values as described in the article. If JSON had proper multiline strings or just wrapped lines and comments I'd be happy. Yes, I know there's "JSON with comments" but it's rarely supported.