YAML is a good format!

As a configuration file, YAML should be the most human-friendly to read and edit. Of course, the extensibility is not as good as JSON, even worse than XML, but there are some give and take, generally enough.

advantages

Let’s go straight to the example and see how it compares.

JSON

The same set of configurations would look like this in JSON:

{" id ": 100," name ":" test project ", "version" : "3.1", and "steps" : [{" id ": 18," action ":" Prepare "and" expects ": [{" id" : 238, "result": "GOOD" }, { "id": 239, "result": "PERFECT" } ] } ] }

While formatted and readable, it’s a lot harder to edit. This is just three levels of nesting, and the parenthesis alignment is a little scary.

XML

In XML, it is more complicated, like the following paragraph, which is a pain to read without syntax highlighting:

<? The XML version = "1.0" encoding = "utf-8"? > <project> <id datatype="int">100</id>: 100 <name datatype="string"> test item </name> <version datatype="string">3.1</version> <steps> <step> <id datatype="int">18</id>, <action datatype="string">Prepare</action>, <expects> <step> <id datatype="int">238</id> <result datatype="ResultType">GOOD</result> </step> <step> <id datatype="int">239</id> <result datatype="ResultType">PERFECT</result> </step> </expects> </step> </steps> </project>

Trying to type this code by hand is even more error-prone: various tags are not closed, the nesting hierarchy is messy, and so on. (Imagine logging into a server remotely and opening it with Vim or Nano.)

YAML

When YAML arrived, the world suddenly became very clear:

Steps: -id: 18 action: "Prepare" : -id: 238, result: high-id: Kripke: Kripke: Kripke: Kripke: Kripke: K 239, result: PERFECT

Is it very simple? In character count alone, JSON is 75% more than YAML, and XML 320% more! The main reason for this is that YAML uses eye-friendly return and TAB data delimiters directly, which makes editing almost like Markdown documents, a programmer’s boon.

disadvantages

There is no silver bullet, of course, and YAML has its inherent flaws.

  • As I mentioned at the beginning, it has poor extensibility — XML has an advantage in that it has attributes per tag and can define arbitrary “metadata,” such as datatype in the example, which is a common weakness of YAML and JSON. If necessary, the metadata is also placed in the data item, which is then identified by a special character prefix (similar to __dict__ in Python objects).
  • Second, it is formatted in such a strict way that it cannot be expressed in a “compact” form: for example, you can compress a JSON string into a single line and remove as much space as possible when you don’t need to read it, which YAML clearly cannot do.
  • There is also the lack of mature support for YAML in various languages, especially in the serialization/deserialization part, and the existing libraries are very inconvenient.

conclusion

If you don’t have strong third-party extensibility requirements, YAML is the best option for a configuration file. The trade-off, of course, is that you have to write a little serialization/deserialization code by hand at first.