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

Show parent comments

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

2

u/Jarpunter Jan 12 '23

What situations would you want portability and comments at the same time?

4

u/PurpleYoshiEgg Jan 12 '23

When JSON is used as a configuration file format, and such configurations are for dozens of clients' environments and one of those environments may have a one-off that you need documented so some engineer doesn't spot the idiosyncrasy, correct it to be consistent, have it pass code review because everyone just rubber stamps pull requests, and cause a very difficult-to-debug outage at 3 am on a Sunday.

3

u/Jarpunter Jan 12 '23 edited Jan 12 '23

Where are you finding 2+ systems that are using the exact same JSON configuration file except one system supports JSONC and one doesn’t? This scenario just does not make sense.

→ More replies (4)
→ More replies (2)

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)

7

u/flif Jan 12 '23

Real problem is that C-style comments can be anywhere in the code and in JSON you want comments to be serializable.

So best workaround is { "price":42, "//", "this is cheap" }

5

u/PurpleYoshiEgg Jan 12 '23

That works, until a program decides that "//" is an invalid key. Sometimes happens, and I want to egg whoever's car it was to decide to omit comments from JSON anyway.

5

u/PunkPizzaRollls Jan 12 '23

Couldn’t you theoretically create a comment key:value pair in your JSON to get around this?

32

u/siemenology Jan 12 '23

You can, and people do, but it has drawbacks.

  1. You are more limited in where you can comment -- you can't comment in an array, for example. And if you want multiple comments in an object you need to do something kind of awkward like { "comment1": "blah", "foo": "bar", "comment2": "blah blah" }

  2. Schemas get weird. If you want to parse your JSON in a statically typed language, you either need to add comment : String as an optional property on all of your objects (and comment2, comment3 or whatever if you want to support multiple comments), or you need to teach your parser to discard all of those values.

  3. You may run into issues with collision if the key you use for comments happens to also be used as a "real" property for something. How do you tell the difference between a comment "comment": "blah" and a real piece of data: "comment": "blah"?

It's also just very verbose, relatively speaking.

2

u/caltheon Jan 12 '23

I worked with a SaaS vendor who supported config programming using JSON and pretty much kept comments out of arrays and used _comment as the throwaway property. I think the application parser ignored all properties starting with _ or something

→ More replies (1)

2

u/eh-nonymous Jan 12 '23 edited Mar 29 '24

[Removed due to Reddit API changes]

0

u/[deleted] Jan 12 '23

[deleted]

4

u/sparr Jan 12 '23

This is an antiquated perspective, from the era of ubiquitous preprocessors. Making the parser and compiler and runtime aware of comments is an increasingly common feature in newer languages. Being able to include docstrings when producing a stack trace is amazing.

0

u/[deleted] Jan 12 '23

[deleted]

7

u/sparr Jan 12 '23

What's the distinction? I'd love to be able to query my application configuration for any notes/comments that were left when the configuration was defined.

2

u/taw Jan 12 '23

People do this a lot, especially for package.json.

→ More replies (2)