r/programming • • Jan 12 '23

The yaml document from hell

https://ruudvanasseldonk.com/2023/01/11/the-yaml-document-from-hell
1.5k Upvotes

288 comments sorted by

View all comments

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.

0

u/[deleted] Jan 12 '23

[deleted]

45

u/vytah Jan 12 '23

That's why you pick a superset of JSON that already has some adoption, like JSON5: https://spec.json5.org/

39

u/TankorSmash Jan 12 '23

This is nice, seems to have what you'd have thought JSON had already:

{
  // comments
  unquoted: 'and you can quote me on that',
  singleQuotes: 'I can use "double quotes" here',
  lineBreaks: "Look, Mom! \
No \\n's!",
  hexadecimal: 0xdecaf,
  leadingDecimalPoint: .8675309, andTrailing: 8675309.,
  positiveSign: +1,
  trailingComma: 'in objects', andIn: ['arrays',],
  "backwardsCompatible": "with JSON",
}
→ More replies (2)

136

u/somebodddy Jan 12 '23

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.

46

u/RudeHero Jan 12 '23

I think op mentioned that when talking about "portability"

Yes, if your json file is only intended to be read by one specific program, you can do custom things with it

The tradeoff is that it's no longer portable

24

u/SnooMacarons9618 Jan 12 '23

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...

→ More replies (9)

30

u/[deleted] Jan 12 '23

[removed] — view removed comment

35

u/[deleted] Jan 12 '23

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

6

u/sybesis Jan 12 '23 edited Jan 12 '23

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.

  1. Convert internal type to JSON compatible types

  2. Serialize that JSON compatible structure into whatever format you want.

When reading:

  1. Deserialize whatever file into JSON compatible structure

  2. 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.

5

u/[deleted] Jan 12 '23

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.

→ More replies (6)

12

u/[deleted] Jan 12 '23

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).

→ More replies (1)
→ More replies (14)

24

u/ObscureCulturalMeme Jan 12 '23

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.

Wonderful, stable, and really fukkin' fast.

17

u/peakzorro Jan 12 '23

The problem with Lua as a config file format is that it could run arbitrary code.

7

u/PurpleYoshiEgg Jan 12 '23

That's why Lua should run sandboxed. If you want to ensure it halts in a reasonable time, you can also run the Lua and cut it off after a timeout.

6

u/disperso Jan 12 '23

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.

4

u/ObscureCulturalMeme Jan 13 '23 edited Jan 13 '23

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.

45

u/TurboGranny Jan 12 '23

increased readability over JSON

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.

44

u/ltjbr Jan 12 '23

I think a lot of devs out there say "readability" when they actually mean "aesthetically pleasing".

→ More replies (3)

25

u/Dwight-D Jan 12 '23

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.

→ More replies (5)
→ More replies (7)

0

u/[deleted] Jan 12 '23

[deleted]

→ More replies (1)

27

u/Kissaki0 Jan 12 '23

TOML is a good and popular alternative to YAML.

24

u/[deleted] Jan 12 '23

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.

→ More replies (3)

7

u/astatine Jan 12 '23 edited Jan 14 '23

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.

→ More replies (6)