What is YAML?
- YAML means another markup language. It is a data serialization language that works well with the modern programming language, and it is human-friendly.
- It is used to define the data structures that are very easy to understand. These data structures are very easy to manage and maintain by the users.
- YAML uses space to define something. If we use a single space or double space, it has different meanings in YAML. Spaces change the meaning of data structure.
- YAML is case sensitive.
- YAML is language independent. If we define the YAML once, the same YAML file can be invoked in Python file, Ruby, etc.
- In the starting, YAML was very useful and friendly to people who work with data. YAML uses Unicode printable characters. In Unicode, some provide the information of structure, and others contain the data itself. Using the minimization of structural characters, a unique cleanness can be achieved by YAML. YAML is used to show the data itself in a meaningful and natural way. For example, bullet lists are created by dashes, key: value pairs are separated by a colon, and structure may be used by indentation.
- To work well with the common use cases, YAML language was specialty designed. Common-use cases are cross-language data sharing, log files, debugging of complex data structure, interprocess messaging, object persistence, and configuration files. The programming becomes an easy task when data is easy to understand and view.
How To Write YAML
The basic structure of a YAML file is a map. You might call this a dictionary, hash or object, depending on your programming language or mood.
Very generally, it’s keys and values all the way down:
key: value
YAML Example: Scalar Types
You can use all sorts of scalar types as values: numbers, booleans, and strings (quoted or not). The first line of a config.yml
, for example, is usually:
version: 2
Words in keys can be separated by underscores, dashes, or spaces. At CircleCI, we use underscores.
If the value of a key is a multi-line string, you can use either the ‘literal block’ style using the ‘|’ character. This is particularly helpful when defining shell commands:
command:
if [ "${CIRCLE_BRANCH}" == "master" ];
then ansible-playbook site.yml -i production;
fi
Note that the leading indentation for the multi-line string will be stripped.
YAML Example: Collection Types
All you have to do to create collections is Use Indentation:
environment:
TEST_REPORTS: /tmp/test-reports
If you have a list of things (like images), you can denote that sequence using dashes:
docker:
- image: ubuntu:14.04
- image: mongo:2.6.8
command: [mongod, --smallfiles]
- image: postgres:9.4.1
Note that the second item in the sequence has two keys: image
and command
. The command
key uses a JSON-style sequence because (remember!) YAML is a superset of JSON.
Finally, YAML doesn’t allow tab characters, so if you’re using those to indent, have your text editor convert those to spaces. Syntax errors in your YAML can sometimes cause CircleCI builds to hang, but they’re also easily preventable by running your circle.yml/config.yml
through an online validator.
What is the file extension for YAML
yaml
files are created with extension .yaml
or .yml
. You can use any IDE or text editor to open this file.
All the popular IDE supports the yaml
file extension and also provides plugins to validate the yaml
file content.
Advantages of YAML
- Files are easy to work in a text editor.
- Files are portable between programming languages.
- Files are expressive and also extensible.
- Files support the Unicode character set.
- Support for major programming languages.
Disadvantages of YAML
- It is a new format introduced recently, a learning curve exists.
- Not much popular other than XML and JSON.
- It is very complex to represent configuration in the hierarchy of data.
Conclusion
In this guide, we covered the steps of preparing the development environment in both Windows as well as Linux to get started with YAML. We nearly discussed all the concepts of YAML’s basic data types, YAML editor, and YAML Parser.
We have also highlighted the benefits of using YAML vis-a-vis other markup languages and provided code examples with the help of a supporting sample project. We hope that now the learners can use YAML to abstract data from application logic to write efficient and maintainable code.
Happy Learning!!
- Best AI tools for Software Engineers - November 4, 2024
- Installing Jupyter: Get up and running on your computer - November 2, 2024
- An Introduction of SymOps by SymOps.com - October 30, 2024