It’s a known fact that software development is a complicated process. Before you can deploy applications for the end-users, you have a lot of stages to cross, and one of the most critical ones is packaging apps in an executable form with all the binaries and libraries needed for their proper functioning.
Today, every app that is used on a hand-held device needs to process data in various forms. It could be data that you provide during your registration, or maybe game data that needs to be synced with the server. Regardless of what data an app generates, there’s always a backend where all of it is stored.
Now, whenever a user launches an app, some data is exchanged between their device and the server which hosts the app. Applications that are hosted on popular marketplaces regularly fetch and send data over the Internet. A message or object sent over the Internet must be converted into a specific format that can be viewed and stored. A process called data serialization is crucial for the efficient functioning of the app.
There are several data serialization tools used in software development, some of which are JSON, XML, and YAML. In this post, we’ll discuss YAML in detail.
What is YAML?
Until a few years ago, YAML stood for Yet Another Markup Language. Over time, however, developers redefined that acronym so that people would know about the language’s data-orientation. This is how the expanded form “YAML Ain’t Markup Language” for YAML emerged.
Unlike other languages which have their limitations to software and hardware environments, YAML can be used for applications that need to transmit and store data. The flexibility of the language can be attributed to the fact that it derives most of its features from popular programming languages used in software development, for instance:
- Associative arrays, lists, and scalars are based on Perl
- Document separator “—” is based on MIME
- Whitespace spacing is based on HTML
- Escape sequences are based on C
Features of YAML
YAML has several great features that are inherited from languages that make up different elements of it. Since it’s more of a developer tool, we’ve described the particular features of this language that are preferred by most developers:
1. Delimiter collision resistance
Unlike other programming languages like JSON, YAML relies heavily on the indentation of the structure that makes it resistant to delimiter collision. Some languages need padded quotation marks, escape sequences or characters, while others handle special characters. YAML is unresponsive to braces and quotation marks, and that makes it easy for developers to define special characters, especially in strings.
Most security threats in applications come from executable codes. YAML is a data representation language that doesn’t have any executable commands. However, its integration with other languages like Perl reduces its security shield. For example, when used with Perl parsers, YAML is required for Perl code. PyYAML is an emitter and parser for Python. This security vulnerability is specifically addressed in its documentation. The application can, however, be protected from dangerous Python objects with its built-in function yaml.safe.load.
Basic YAML syntax & Data Types
Every YAML file starts with “—”. When creating an API, this is used for data mapping as shown in the below-mentioned example:
The mapping syntax used is “key: value.” Note that the space after the colon is important. Besides text and numbers, YAML also supports data types like floating values, integers, characters, strings, and arrays made of basic data types.
To get an idea of the data types YAML supports, consider the following example:
The first data type mentioned in the list (“female”) is a BOOLEAN; it can have a “TRUE” or “FALSE” value. The value of “GPA” is a floating-point. The “Issues” field has a NULL data type. The value of the field “Name” is a string that should be enclosed in a single or double quote.
YAML also supports single line and multiple line strings as well as multiline strings for readability. Here are a few examples of both:
1. Multiple Line Strings
In the above example, the > symbol allows you to write a single string into multiple lines. The sentence is a single string though it is written in multiple lines to improve the readability of the code.
2. Multiline Strings
The multiline string in YAML uses the “|” symbol. Here is an example:
Now let’s see a few examples of another important data type in YAML – Lists. When you need to create a list of similar objects in YAML, this is the syntax you can use:
YAML also allows you to have nesting lists. Here is an example:
In the above code, Ford is nested in Cars which in turn is nested in Automobiles.
And now that we’ve seen a high-level overview of the data types supported by YAML, let’s have a quick look at some of the rules of this programming language.
Rules for writing YAML in SLS (Salt State System) files
Salt State System refers to the representation of the state in which a particular system should be set up. The setup typically contains data in a simple format, and is part of configuration management.
There are three basic rules you need to follow when writing code in YAML.
1. Rule One – Indentation
The programming language uses a fixed indentation scheme to symbolize the relationship between data layers. Salt has a specific requirement regarding indentation at each level; it should be exactly two spaces. However, the use of tabs is not allowed
2. Rule – Colons
YAML uses Python dictionaries, which are nothing more than key-value pairs. Other programming languages refer to this data type as associative arrays or hashes. In the coding, dictionary keys are represented as strings terminated by a colon. Strings can be used to represent values after the colon or separated by spaces. For example:
my key: my value
3. Rule 3 – Dashes
To represent a list of items, a single dash followed by space is used. Multiple items which are part of the list will have the same indentation. For example:
Wrapping it Up
While there is a lot more to cover as far as the introduction to YAML goes, we believe this post contains enough information to get you started.
Modern applications and programming frameworks use database storage in distributed forms. YAML is a feature-rich and flexible data serialization language that works with most platforms and makes data management easy for applications. In communication applications, it is primarily used for creating configuration files, and most developers prefer it to XML. YAML has a minimal syntax that makes it different from other languages of the same kind. It integrates the best features of the popular web programming language in an easy-to-use format.
Would you like to know more about YAML? If yes, do let us know in the comments below.
People are also reading: