This is the 23rd day of my participation in the August More Text Challenge.More challenges in August
YAML is a user-friendly data serialization language that can be used with most current programming languages.
YAML’s syntax is concise and intuitive, characterized by the use of indentation to express hierarchies
YAML is used to write configuration files, which usually end with. Yml or. YAML
Write rules
- Case sensitivity
# The next two are different
name: klaus
Name: klaus
Copy the code
- Use indentation to indicate hierarchy
- The Tab key is not allowed for indentation. Only Spaces are allowed.
- The number of Spaces indented does not matter, as long as elements of the same rank are aligned to the left
# YAML
info:
name: klaus
firend:
name: Alex
age: 18
Copy the code
- use
#
Represents a comment,-
This character is ignored by the parser all the way to the end of the line
-
That is, YAML only supports single-line comments
-
# I'm a comment
Copy the code
- Support for flow-style syntax (wrapped in curly braces, separated by commas and Spaces, similar to JSON)
- That is
yaml
The file is native and supports JSON format
# YAMl can parse JSON content properly
{
"name": "Klaus"."age": 23
}
Copy the code
We can use this test platform to see yamL compiled into JS objects or use this test platform to see yamL compiled into JSON
object
# object - the colon is followed by a space
# object key-value pairs use the colon structure for key: value,
# key: value
name: Klaus # => { name: 'Klaus' }
# -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -
Object nesting
Writing # 1
info :
name: Klaus
# => { info: { name: 'Klaus' } }
# write 2
Support streaming style
info : { name: Klaus } # => { info: { name: 'Klaus' } }
info: {
name: Klaus.age: 23
}
# => { info: { name: 'Klaus', age: 23 } }
# 4 - a question mark with a space represents a complex key
?
- complexkey1
- complexkey2
:
- complexvalue1
- complexvalue2
# => 'complexkey1,complexkey2': [ 'complexvalue1', 'complexvalue2' ] }
Copy the code
An array of
# array
An array of data beginning with a Block Format (dash + space)
- Cat
- Dog
- Fish
# => [ 'Cat', 'Dog', 'Fish' ]
-
- Cat
- Dog
- Fish
# => [ [ 'Cat', 'Dog', 'Fish' ] ]
# Support Inline Format (enclosed in square brackets, separated by commas and Spaces, similar to JSON)
[ Cat.Dog.Fish ] # => [ 'Cat', 'Dog', 'Fish' ]
There is a default value of null when you do not write an array value
Yaml does not have undefined, so it uses null by default
- # => [ null ]
Copy the code
scalar
A scalar (literal) is the most basic, nonseparable value
boolean:
- true # true, true, true
- false # false, false, false
string:
# Special characters can be wrapped in double or single quotation marks - no quotation marks are recommended
- Klaus # => Klaus, 'Klaus ', "Klaus"
# If there are special symbols, use quotation marks around them
- 'name: Klaus' # => 'name: Klaus'
- Klaus Wang # => 'Klaus Wang'
The # string can be broken into multiple lines, and each line is converted to a space
- Klaus
Wang # => 'Klaus Wang'
number:
#, like number in JS, can be either an integer or a floating point number
- 10 # => 10, 0.1, 12.5
# support binary, octal, and hexadecimal numbers.
- 0b11000 # => 0o30, 0x18
- 0B11000 # => '0B11000' The base identifier is recognized as a string if written in uppercase
# The hexadecimal characters are case insensitive except for the base identifier
- 0x1A The conversion of # 0x1a and 0x1A is the same
- 6.8523015 e+5 Scientific notation can be used
Yaml does not have the undefined data type
# use ~ to represent null
# null, null, null, and ~ are null, and default to null if no value is specified
null: ~
The default value is null when the object does not write a value
name: # => { name: null }
The date must be in ISO 8601 format, yyyY-MM-DD
date:
- 2018-02-17
The time is in ISO 8601 format, with T concatenation between time and date, and + for time zone at the end
datetime:
- 2018-02-17T15:02:31+08:00
Copy the code
YAML allows you to cast data types using two exclamation points (double exclamation points + target type)
e: !!!!! str 123
f: !!!!! str true
# => { e: '123', f: 'true' }
Copy the code
The anchor
defaults: &defaults # Set the anchor point
adapter: postgres
host: localhost
adapter: *defaults # Use anchor points
Copy the code
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
Copy the code
- &showell Steven
- Clark
- Brian
- Oren
- *showell
# => [ 'Steven', 'Clark', 'Brian', 'Oren', 'Steven' ]
Copy the code
js-yaml
Js-yaml is a library that parses YML files and converts them into JavaScript objects
The installation
npm install js-yaml
Copy the code
use
const yaml = require('js-yaml');
const fs = require('fs');
const doc = yaml.load(fs.readFileSync('./foo.yml'.'utf8'));
console.log(doc);
Copy the code