Using yaml to write configuration files

yamlIt is a data serialization standard which is suitable for all development languages, and the biggest feature is good readability.

yamlOne of the main application directions is to write configuration files. There are many systems and frameworks configured with yaml.

yamlThere are the following basic rules:

1、Case sensitive
2、Use indentation to represent hierarchical relationships
3、No tab indentation is allowed, only space key is allowed.
4、The indentation length is not limited, as long as element alignment means that these elements belong to a hierarchy.  
5、Use notes to indicate notes.
6、Strings can be marked without quotes.

Three data structures

1. map,Hash table

Use colon: indicate key value pairs, and all key value pairs of the same indent belong to a map.

# yamlExpress

age : 12

name : beita

# Corresponding Json representation

{‘age’:12,‘name’:‘beita’}

2. list,array

Using hyphen (-) means:

# yamlExpress:

a

b

1

# Corresponding to Json means:

[‘a’,’b’,1]

It can also be written in one line:

# yamlExpress:

[a,b,c]

# Corresponding to Json means:

[ ‘a’, ‘b’, ‘c’ ]

3. scalar,Pure quantity

The smallest unit of data can no longer be segmented.

Data structure nesting

mapThe element of list can be another map or list or scalar.

1. mapNested map

 1 # YAMLExpress 2 websites:
 3  YAML: yaml.org 
 4  Ruby: ruby-lang.org 
 5  Python: python.org 
 6  Perl: use.perl.org 
 7 
 8 # Corresponding Json representation 9 { websites: 
10    { YAML: 'yaml.org',
11      Ruby: 'ruby-lang.org',
12      Python: 'python.org',
13      Perl: 'use.perl.org' } }

 

2. mapNested list

1 # YAMLExpress2 languages:
3  - Ruby
4  - Perl
5  - Python 
6  - c
7 
8 # Corresponding Json representation9 { languages: [ 'Ruby', 'Perl', 'Python', 'c' ] }

 

3. listNested list

 1 # YAMLExpress 2 -
 3   - Ruby
 4   - Perl
 5   - Python 
 6 - 
 7   - c
 8   - c++
 9   - java
10 
11 # Corresponding Json representation12 [ [ 'Ruby', 'Perl', 'Python' ], [ 'c', 'c++', 'java' ] ]
 1 # Method 2 2 - - Ruby
 3   - Perl
 4   - Python 
 5 - - c
 6   - c++
 7   - java
 8 
 9 # Method 310 - [Ruby,Perl,Python]
11 - [c,c++,java]

 

4. listNested map

 1 # YAMLExpress 2 -
 3   id: 1
 4   name: li
 5 -
 6   id: 2
 7   name: liu
 8 
 9 # Corresponding Json representation10 [ { id: 1, name: 'li' }, { id: 2, name: 'liu' } ]

 

Leave a Reply

Your email address will not be published. Required fields are marked *