DEV Community

Discussion on: Parsing Config Files The Right Way

Collapse
 
maxart2501 profile image
Massimo Artizzu • Edited

YAML is a superset of JSON but its specification is more precise.

I'm not sure I'm following you here. The format specification of JSON is flawless (as far as we know); the parser implementation specification, on the other hand, is left with more freedom as it's intended as an interoperable format and thus the result depends on the language that has to deal with it.

Now, I doubt YAML is re-defining JSON format spec, but maybe you're talking about the implentation?

Anyway, the page you posted, although it provides a lot of useful tests, it's all about edge cases. Now, it's probably very odd if you have an edge case in a configuration file.

Not sure what you mean by that. Personally, whenever I'm editing json, xml or yaml files, I have a linter that tells me if the syntax is OK.

I have to clarify indeed: I meant that you need a schema to validate your YAML. As an example, consider the following:

{
  "brands": {
    "BMW": [ "Z4" ],
    "Chevrolet": [ "Matiz" ],
  "Ferrari": [ "458" ]
  }
}

If I mess up the indentation, I still have a good JSON. If I do the same with YAML it's quite different:

brands:
  BMW: [ "Z4" ]
  Chevrolet: [ "Matiz" ]
Ferrari: [ "458" ]

A linter wouldn't catch it. That's why I suggest to keep config files as flat as possible :/

Thread Thread
 
dmerejkowsky profile image
Dimitri Merejkowsky • Edited

you're talking about the implementation?

Oh yes. Sorry.

If I mess up the indentation, I still have a good JSON. If I do the same with YAML it's quite different

Right. I see what you mean. Again, nothing new under the sun. You have exactly the same problem in Python.

Trivia: in a big Python project I was working on for quite a long time I only had a few bugs caused by incorrect indentation, but I can see why it's a big deal for lots of people :)

And the solution is the same: keep your code "flat" by using nice little helper functions.