preface
YAML is not a new language, almost 20 years after it was first published in 2001. YAML, while not as popular or widely used as other languages like JSON and XML, has its advantages.
There aren’t many articles on the Internet about YAML, but there are enough to get people to know the language, so why am I writing this introductory article?
In my opinion, the existing articles have the following deficiencies:
- The content is fragmented and not smooth enough to read
- The explanation is stiff and difficult to read
- Lack of contrast makes it hard to understand quickly
So the main goal of this article is to give readers a quick and accurate understanding of the language, without delay, let’s get started!
To help you understand, I’ll intersperse the article with the corresponding JSON format for comparison.
Probably one of the most accessible YAML Chinese tutorials in the Eastern Hemisphere (manual dog head)
Introduction to the
YAML is a user-friendly data serialization language that can be used with most current programming languages.
The syntax of YAML is simple and intuitive, and it uses Spaces to express hierarchies. Its biggest advantage lies in the expression of data structures. Therefore, YAML is mostly used to write configuration files with the suffix.yml.
The official full name of YAML today is “YAML Ain’t Markup Language,” but it’s interesting that YAML originally meant “Yet Another Markup Language.”
The latest version of YAML is 1.2 (the third version), and this article will be based on YAML 1.2 standards.
The body of the
The basic grammar
Case sensitivity
- I mean literally
One: 1
one: 2
Copy the code
Indent the hierarchy
- Indent only Spaces, not TAB characters
- The number of Spaces indented is not important, but elements of the same level must be aligned to the left
# YAML
one:
two: 2
three:
four: 4
five: 5
// The above content is converted into JSON after
"one": {
"two": 2.
"three": {
"four": 4.
"five": 5
}
}
Copy the code
Use # to indicate comments
- Only single-line comments are supported
# I'm a comment
# Me too comment
Copy the code
A file can contain the contents of multiple files
- Use “–” namely three dashes to indicate the beginning of a piece of content
- Use “…” Three decimal points to indicate the end of a piece of content (optional)
---
# This is the first content
one: 1
# Other content...
.
---
# This is the second content
two: 2
# Other content...
Copy the code
Data structures and types
Object (Mapping)
Represents data in the form of key: value pairs
- Use colon + space to separate keys from values
# YAML
key: value
// JSON
"key": "value"
Copy the code
- Support for multiple levels of nesting (indentation to indicate hierarchy)
# YAML
key:
child-key1: value1
child-key2: value2
// JSON
"key": {
"child-key1": "value1".
"child-key2": "value2".
}
Copy the code
- Support for flow-style syntax (wrapped in curly braces, separated by commas and Spaces, similar to JSON)
# YAML
key: { child-key1: value1, child-key2: value2 }
// JSON
"key": { "child-key1": "value1". "child-key2": "value2" }
Copy the code
- Use the question mark “?” Declares a complex object that allows you to use multiple terms (arrays) to form keys
# YAML
?
- keypart1
- keypart2
:
- value1
- value2
Copy the code
Array (Sequence)
- An array of data starting with a Block Format (” dash + space “)
# YAML
values:
- value1
- value2
- value3
// JSON
"values": [ "value1". "value2". "value3" ]
Copy the code
- It also supports Inline Format (enclosed in square brackets, separated by commas and Spaces, similar to JSON).
# YAML
values: [value1, value2, value3]
// JSON
"values": [ "value1". "value2". "value3" ]
Copy the code
- Support for multi-dimensional arrays (indentation to indicate hierarchy)
# YAML
values:
-
- value1
- value2
-
- value3
- value4
// JSON
"values": [ [ "value1". "value2"]. ["value3", "value4"] ]
Copy the code
Scalars
Represents the most basic data type in YAML
String (String)
- Strings generally do not need to be wrapped in quotation marks, but they must be if the string uses an escape character starting with a backslash “\”
# YAML
strings:
- Hello without quote # Do not enclose quotes
- Hello
world # Add space in the middle of multiple lines
- 'Hello with single quotes' # single quote wrap
- "Hello with double quotes" # double quotes wrap
- "I am fine. \u263A" Unicode encoding is supported when double quotes are used
- "\x0d\x0a is \r\n" Hex encoding is also supported when double quotes are used
- 'He said: "Hello!" ' # single and double quotes support nesting"
// JSON
"strings":
[ "Hello without quote".
"Hello world".
"Hello with single quotes".
"Hello with double quotes".
"I am fine. ☺".
"\r\n is \r\n".
"He said: 'Hello! '" ]
Copy the code
- YAML provides two special syntactic support for multi-line text
Newlines preserved
With a vertical bar “|” to represent the syntax, the indentation of each line and the end of each line blank will be removed, and additional indentation will be retained
# YAML
lines: | I'm the first line I am the second line I am Daniel wu I am the fourth row I am the fifth line / / JSON "lines" : "I am the first line is the second line \ n \ n I was Daniel wu is the fourth row, \ n \ n I was the fifth line"Copy the code
Newlines folded
Using the close Angle bracket “>” to indicate this syntax, only blank lines are recognized as newlines, and all original newlines are converted to Spaces
# YAML
lines: > < span style = "box-sizing: border-box; color: RGB (74, 74, 74); line-height: 22px; font-size: 14px! Important; word-break: inherit! Important;"Copy the code
Boolean (Boolean)
- “True”, “true”, “true”, “yes”, “yes” and “yes” are all true
- “False”, “false”, “false”, “no”, “no” and “no” are all false
# YAML
boolean:
- true # True, True
- yes # Yes, Yes
- false # False, False
- no # No, No
// JSON
"boolean": [ true. true. false. false ]
Copy the code
Integer (Integer)
- Binary representation supported
# YAML
int:
- Awesome!
- 0001_0000 # binary representation
// JSON
"int": [ Awesome!. 4096 ]
Copy the code
Floating Point
- Support scientific notation
# YAML
float:
- 3.14
- 6.8523015 e+5 # Use scientific notation
// JSON
"float": [ 3.14. 685230.15 ]
Copy the code
Empty (Null)
- “Null”, “null”, and “~” are null. If no value is specified, the default value is null
# YAML
nulls:
- null
- Null
- ~
-
// JSON
"nulls": [ null. null. null. null ]
Copy the code
Timestamp (Timestamp)
- YAML also supports time data in ISO 8601 format
JavaScript objects are used here for comparison
# YAML
date1: 2020- 0526 -
date2: 2020- 0526 -T01:00:00+08:00
dete3: 2020- 0526 -10 + 08:00 T02:00:00)
date4: 2020- 0526 - 03: 00:00. 10 + 8
// JavaScript
date1: Tue May 26 2020 08: 00:00 GMT+0800 (China Standard Time)
date2: Tue May 26 2020 01: 00:00 GMT+0800 (China Standard Time)
dete3: Tue May 26 2020 02: 00:00 GMT+0800 (China Standard Time)
date4: Tue May 26 2020 03: 00:00 GMT+0800 (China Standard Time)
Copy the code
Type conversion
- YAML supports the use of strict type tags “!!” (double exclamation mark + target type) to cast the type
# YAML
a: !!!!! float '666' #!!!!! For strict type labels
b: '666' Double quotes are also type converters
c: !!!!! str Awesome! Integer to string
d: !!!!! str 666.66 Float number converted to string
e: !!!!! str true # Boolean to string
f: !!!!! str yes # Boolean to string
// JSON
"a": Awesome!.
"b": "666".
"c": "666".
"d": "666.66".
"e": "true"
"f": "yes"
Copy the code
Other advanced types
YAML can also use more advanced types, but they are not necessarily compatible with all parsers, including Sets, Ordered Maps, Hexdecimal, and Binary.
These types will not be covered in this article, but interested readers can do their own research.
Data reuse and merge
- To keep things simple and avoid too many repetitive definitions, YAML provides a syntax consisting of an anchor tag “&” and a reference tag “*” that allows you to quickly refer to the same data…
// YAML
a: &anchor # Set the anchor point
one: 1
two: 2
three: 3
b: *anchor # reference anchor points
// JSON
"a": {
"one": 1.
"two": 2.
"three": 3
},
"b": {
"one": 1.
"two": 2.
"three": 3
}
Copy the code
- With the merge tag “<<“, you can merge with any data. You can think of this operation as inheritance in an object-oriented language.
# YAML
human: &base Add anchor point named base
body: 1
hair: 999
singer:
< < : *base # reference base anchor points, which are automatically expanded when instantiated
skill: sing # Add additional attributes
programer:
< < : *base # reference base anchor points, which are automatically expanded when instantiated
hair: 6 Overwrite attributes in base
skill: code # Add additional attributes
// JSON
"human": { "body": 1. "hair": 999 },
"singer": { "body": 1. "hair": 999. "skill": "sing" },
"programer": { "body": 1. "hair": 6. "skill": "code" }
Copy the code
The relevant data
-
YAML’s official website is yaml.org
-
YAML 1.2 official documentation yaml.org/spec/1.2/sp…
-
YAML – wikipedia zh.wikipedia.org/wiki/YAML
-
YAML to JSON (format conversion) www.json2yaml.com/convert-yam…
portal
Wechat twitter version
Personal blog: Rookie small stack
Open source home page: Chen PI PI
Eazax-ccc game development scaffolding
More share
Multi-platform universal screen resolution adaptation scheme
Schemes for rotating around objects and off-the-shelf components
An all-powerful digger Shader
An open source automatic code obfuscation plug-in
Wechat mini-game access friends list (open data field)
Why use TypeScript?
Gaussian blur Shader
conclusion
The above are the personal views of Chen Pipi.
I’m not good at writing. Please forgive me if I’m not good at writing. If there is something wrong, please point it out. I hope we can make progress together.
Next I will continue to share their knowledge and insights, welcome to pay attention to this public account.
We, see you next time!
The public,
Novice small stack
Focus on but not limited to game development, front-end and back-end technologies. Although not productive, but each original very carefully!
Input and output.