Recently I switched jobs, and as part of this change, I’ve been introduced to a whole new tech stack. RabbitMQ 🐰, Java Spring, Docker, etc. (meaning more subjects to write about 😂). Most of the technologies I use on a daily basis consume YAML as their configuration. In this post, I’ll try to illustrate what I’ve learned (and from where) while trying to understand this new world.
YAML stands for Y AML A in’t M arkup L anguage, originally named Yet Another Markup Language. The name was chosen because it requires much less markup than other traditional languages, such as XML. It distinguishes it as more data-oriented rather than markup-oriented. YAML spec says it better:
YAML™ (rhymes with “camel”) is a human-friendly, cross language, Unicode based data serialization language designed around the common native data structures of agile programming languages.
It has become widespread for writing configuration files because it uses a human-readable, intuitive, and flexible language. It can be used with almost any application that needs to send or store data (and has no code execution capabilities — Secure?). YAML is a superset of JSON, which means any valid JSON is a valid YAML file. It has several advantages over JSON; it can self-reference, support complex datatypes, embedded block literals, support comments, and more.
Furthermore, I’ve recently learned that one of the most common usages for YAML in the java world is an autogenerated code using swagger (API deployment). YAML defines the classes, and swagger generates the code. But that is a separate story for a different post.
The basic building block of YAML documents is a key-value pair. The nesting is based on indentation (similar to Python), making it resistant to delimiter collision. Just keep in mind, whatever you do, DO NOT USE TABS. Yaml hates tabs, and it won’t work. Tip: use a YAML linter, whether an online one or a plugin in your IDE.
It is insensitive to quotes and brackets, making special characters more easily defined, especially for strings. To better explain the above concepts, you can find below some simple examples, which I found essential to know when starting to use YAML:
Variables are defined using a colon and space :
integer: 17 string: "17" float: 17.0 boolean: Yes
A list or array can be defined using an inline format that’s similar to JSON or a conventional block format:
-------- # To Do List in Block Format - Homework - Walk with the dog - Dog should eat homework -------- # To Do List in Inline Format [Homework, Walk with the dog, Dog should eat homework]
You can denote a string with a | symbol, which keeps newlines , or a > symbol, which folds them:
data: | Each of these Newlines Will be broken up data: > This text is wrapped and will be formed into a single paragraph
The YAML complete documentation can be found on its official site. It is worth going over to be familiar with it. Also, there are plenty of online tutorials; I’ve found this one to be a great source:
YAML is a data-oriented language and a superset of JSON that comes with multiple built-in advantages. It became an industry standard for configuration files. But overall, it is human-readable, which makes it easy to use and maintain. Just be gentle with these whitespaces.