preface
If you’ve ever worked with programming data structures, you’ll be quick to pick this up. You’ve written json… Focus on indentation and understand the concept. Here only list very high frequency and high versatility of the use of posture, individual coquettish posture to understand their own proposals!
Writing and annotation
It is worth mentioning that:
- File first line
%YAML version
, you can specify the version of YAML to use - Colons in key-value pairs must be followed by Spaces is standard writing, otherwise it will be a string!
- Case sensitivity
- The statement
key
Must be a string - Indent with space (TAB not allowed)
#
The comment area is from the beginning to the end of the line- The types allowed by the value are
str
String:int | float
: Integer and floating pointlist(array)
: can be understood as an arraymap(dict)
: Different languages have different names, but dictionaries are more generalnull(~)
: a null valuedate
: IsO-8601 date format
%YAML 1.2
---
# This is a comment, just like bash, starting with a # sign
# The most common string declaration, with single quotes by default (escaped)
text: I'm the test text
text1: 'I'm test text 1'
text2: 'I'll be escaped \n, because single quotes will escape what needs to be escaped'
text3: "Double quotes ignore,\n\t"
text4: "This is multi-line text that needs space indentation, you can try that."
# | behind may add + (retain a newline, default), - remove the newline character at the end of the string in a row
text5: | the multiline text readability is stronger A similar array, lined up # also supports + -, also is to control a newlinetext6: > that's the way you fold a line break and you keep the line break at the end of the last lineisEmpty: ~
isEmpty2: null
date: 202007 -- 15
A list of one-dimensional arrays
# write a
language: ['c','c++']
# write two
hight_language:
- 'python'
- 'java'
# two-dimensional array
# write a
testNestedArr1:
- [1, 2, 3]
- (4 and 6)
# 2, relying on indentation
testNestedArr2:
-
- Cat
- Dog
- Goldfish
# map # map # map # map # map
# write a
asiaHuman: {eye: 'black',skin: 'yellow'}
# write two
europeHuman:
- eye: 'blue'
- skin: 'white'
# array object notation
arrayMap:
- key: 1
- {test: 'fsadfas'}
This can be used for other reuse, somewhat like inheritance
Use & to declare anchor points
human: &base
foot: true
hand: true
head: true
body: true
# anchor point reuse, with << to declare the import (merged into the current),*+ anchor point to determine the scope of reference
asiaMan: &asiaM
skin: 'yellow'
< < : *base
otherDesc:
- &inline {strong: True}
skill:
< < : *asiaM
< < : *inline
it: very good
Type cast
#!!!!! Int # Integer type
#!!!!! Float # Floating point type
#!!!!! Bool # Boolean type
#!!!!! STR # is a string
#!!!!! Binary # is also a string
#!!!!! Timestamp # Date time type
#!!!!! Null # a null value
#!!!!! Set # collection
#!!!!! omap,!! Pairs # List of keys or objects
#!!!!! Seq # sequence, also a list
#!!!!! Map # key table
needStr: !!!!! str '0.01'
needBoolean: !!!!! bool 'false'
needBoolean2: !!!!! bool 'True'
needSeq: !!!!! seq ,2,2,3 [1]
needFloat: !!!!! float '666.6'
# associative array keys, I saw this in k8s
It can combine multiple keys into more complex keys
? - key1
- key2
: - value1
- {value2: '3'}
.
---
# online compiler is data stream compilation, can not multiple documents online, this part can not be put in (can be independently tested)
# Slicing yamL documents. This gesture is common in K8S configuration files
# Begin with --(three lines)
# to... End up as a YAML document
testNestedArr2:
-
- Cat
- Dog
- Goldfish
.
Copy the code
Compile the effect
{ text: 'I'm test text'.text1: 'I'm test text 1'.text2: 'I will be escaped \\n because single quotes will escape what needs to be escaped'.text3: 'Double quotes ignore,\n\t' to be escaped.text4: 'This is multi-line text that needs space indentation, you can try that.'.text5: \n\n# supports +-, also controls the newline character \n'..text6: 'This is the position of folding the newline and keeping the newline character at the end of the last line. Try it out. \n'.isEmpty: null.isEmpty2: null.date: Wed Jul 15 2020 08:00:00 GMT+0800(China Standard Time)language: [ 'c'.'c++'].hight_language: [ 'python'.'java'].testNestedArr1: [[1.2.3 ], [ 4.5.6]],testNestedArr2: [['Cat'.'Dog'.'Goldfish']],asiaHuman: { eye: 'black'.skin: 'yellow' },
europeHuman: [{eye: 'blue' }, { skin: 'white'}].arrayMap: [{key: 1 }, { test: 'fsadfas'}].human: { foot: true.hand: true.head: true.body: true },
asiaMan: { skin: 'yellow'.foot: true.hand: true.head: true.body: true },
otherDesc: [{strong: true}].skill:
{ skin: 'yellow'.foot: true.hand: true.head: true.body: true.strong: true.it: 'very good' },
needStr: '0.01'.needBoolean: false.needBoolean2: true.needSeq: [ 1.2.2.3].needFloat: 666.6.'key1,key2': [ 'value1', { value2: '3'}}]Copy the code
Online test address
Based on JS-YARML: nodeca.github. IO /js-yaml/
The official proposal
Yaml is constantly being updated to include more complex postures depending on the level of support for different language implementations! For those interested, click on the portal below: yaml.org/
conclusion
Why will masturbation this, the main collation after convenient review.. If there is something wrong, please leave a message and correct it in time. Thank you for reading!