DEV Community

Discussion on: Parsing Config Files The Right Way

Collapse
 
dmerejkowsky profile image
Dimitri Merejkowsky

Thanks for the feedback! Few remarks.

About JSON not being well-defined

See seriot.ch/parsing_json.php. True, most of the time you won't have any problem using different implementations of JSON parsers, but the devil is in the details. (Things like dates, text encoding, trailing comas or not-a-number floats).

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

you need a validator to be fairly sure that your config file is ok,

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.

If you mess up with the indentation the file is still considered valid YAML.

True. That's an argument that always comes back when you talk about whitespace significance. Python has the same problem, but personally I don't care that much. Fortunately, if this is an issue for you there are lots of alternatives.

avoid deeply nested (YAML) documents,

This is good advice and it applies to any configuration file ;)

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.