Hey everyone, In this blog I am going to take you through what is yaml and actually write your first file with yaml.
What is Serialization and Deserialization:
Let's first understand what is Serialization and Deserialization. Let's say you have a object which you want to store in files. You can't just copy paste the object into the file so for that reason you need to "Serialize" your data. By serializing it means it converts your object into stream of bytes that can then be saved in files, databases or memory as per your choice, and this is done through a "serializer" (refer fig 1). Now this serialized data is easily transimmatable and shared. And exactly the reverse of this would be "Deserialization".
What is YAML:
Okay so YAML is not a programming language nor a markup language. It's a data serialization language - meaning how you store the data in the code/file format. YAML is similar to XML and JSON if you know about it. Also the interesting fact is you can only save data into YAML files and not commands. So storing the data in files is known as "Data Serialization".
Why YAML?
YAML is a very essential and easy language to write the configuration files for your project or object creation. YAML can also be used for logs, caches. I will list down some benefits of using YAML
- Human readable language
- Strict syntax
- Easily convertable to JSON or XML
- Most of the languages use YAML
- Helpful while representing complex data
- You can use various tools like Parsers etc.
- Reading the data is easy from YAML files
Enough talk, let's try out:
What's benefit in just learning about it when you can't try it out right? So let's go ahead and write and learn some basic yaml
#key value pair
"apple": "I build iPhones"
1: "This is Kaiwalya"
# or
{apple: "I build iPhones", 1: "This is Kaiwalya"}
#Lists in yaml
- Apple
- Microsoft
- Google
- Facebook
#Block style in yaml
countries:
- India
- Bhutan
- Bangladesh
#or
countries: [India, Bhutan, Bangladesh]
You can tell YMAL that above three are different documents by seperate it via following sysntax
#Start and seperation of document
---
#Ending the document
...
Now lets see the datatypes in YAML
#valid String Variables
me: Kaiwalya
lname: "Koparkar"
job: 'Developer'
But how can we use it in real world scenario? so for that we are going to see it using a real world example of school.
Note that if there is going to be multiple name
then there is -
added infront of it. (Like list of school name
s)
---
School:
- name: "DPS"
principal: "Random"
student:
- rollno: 1
name: "Kaiwalya"
marks: 92
- rollno: 2
name: "Ram"
marks: 80
- name: "ABC"
principal: "Someone"
student:
- rollno: 1
name: "Stud1"
marks: 79
- rollno: 2
name: "Stud2"
marks: 80
...
And done, honestly that's it. That's all you need to know about YAML. Just some headsup, It is extremely crusial to test if you YAML file is valid or not for that reason you can use tool like YAMLlint to validate your YAML code.
Resources:
Complete YAML Course - Beginner to Advanced for DevOps and more!
Thank you so much for reading 💖
Like | Follow | Subscribe to the newsletter.
Catch me on my socials here: https://bio.link/kaiwalya
Top comments (0)