YAML - Datatypes

YAML - Datatypes

As we already understand, key-value pairs are the fundamental components of YAML documents. This prompts the question: what types of values can we assign to a key? The key will always be a string, while its corresponding value can encompass a wide range of data types, including strings, numeric values, boolean values, or null. Now, let's explore the available data types in the language:

String types

This is how string types look like:

In programming, strings are typically enclosed within single or double quotes, but here they can also be written without quotes. Although strings can be written without quotes, double quotes are necessary when incorporating escape sequences.

To span string values across multiple lines, you can use the '>' (greater than symbol). This approach aids in formatting text for readability and presentation purposes.

But if all the formatting and indentation of this multi-line text will be lost, then we can't write such formatted strings in YAML. Of course, we can, using the pipe ( | ) character like this:

So here, this text will be interpreted as written preserving all new lines and styling.

Numeric types

This is how we can write various types of numeric types:

The comments on lines 34 and 35 explain that similar to Python, YAML can automatically detect the data type of a variable without needing explicit specification. However, if we choose to specify the data type, we can do so using the format: !!<datatype>

YAML can represent various types of numbers, including binary, octal, and hexadecimal. Additionally, it can represent special values such as Not-a-Number (NAN) and infinity.

pos_infinity: .inf                 #to represent positive infinity
negative_infinity: -.inf           # to represent negative infinity

Null values

There are various formats in which we can write null values:

A tilde (~) can represent a null value, including when a key itself is null. Line 67 demonstrates how we can represent a null key in such cases.

One thing I want to mention here about null keys is YAML doesn't allow duplicate keys. So, if there are multiple null keys, we can't represent them all with the '~' symbol. Instead, we need to use placeholders in such cases.

# we can't have something like this in one yaml document
~: this is a null key
~: this is another null key

# we can do something like this
~: this is a null key
null_another: another null key
null2: one more example of null key

Here "null_another", and "null2" are appropriate placeholders that we are using to show that these keys are null.

Booleans

There are various ways to represent true and false in YAML:

Above mentioned are some fundamental datatypes often used in YAML documents. In the next blog of the YAML series, I'll be talking about some advanced datatypes frequently used while writing YAML files.

I'm studying YAML from:

https://www.cloudbees.com/blog/yaml-tutorial-everything-you-need-get-started

Complete YAML course By Kunal Kushwaha

As I am new to this language, there is a possibility that I may have made mistakes in writing or understanding certain concepts. If this is the case, I welcome readers to provide corrections and feedback by leaving a comment. Your input is greatly appreciated and will help improve the quality and accuracy of the content.

~ Future Forward :)