Simple YAML language tutorial

Original link: http://www.ruanyifeng.com/blog/2016/07/yaml.html?f=tt

Programming will inevitably write the configuration file, how to write the configuration is also a science.

YAML is designed to write the language configuration file, it is very simple and powerful and convenient than the JSON format.

This article describes the YAML syntax to  JS-YAML  implementation example. You can go online Demo  verify the following example.

I. Introduction

YAML language (pronunciation / jæməl /) design goal is to facilitate human reader. It is essentially a common serial data format.

Its basic syntax rules are as follows.

  • Case Sensitive
  • Use indention hierarchy
  • Not allowed to use the Tab key to indent only allows the use of space.
  • The number of spaces to indent does not matter, as long as the left elements are aligned to the same level

# Indicates a comment from this character until the end of the line, the parser will be ignored.

YAML data structure supported by three.

  • Object: a set of keys, also known as mapping (mapping) / hashes (hashes) / Dictionary (dictionary)
  • Array: a set of values ​​are arranged in order, also known sequence (sequence) / list (list)
  • Scalar (scalars): individual, can not be divided value

The following sections describe these three data structures.

Second, the object

A set of key-value object of using the structure represented by colons.


animal: pets

JavaScript turned follows.


{ animal: 'pets' }

Yaml also allows another way, all the key-value pairs within the object written as a row.


hash: { name: Steve, foo: bar } 

JavaScript turned follows.


{ hash: { name: 'Steve', foo: 'bar' } }

Third, the array

A set of row lines beginning conjunction form an array.


- Cat
- Dog
- Goldfish

JavaScript turned follows.


[ 'Cat', 'Dog', 'Goldfish' ]

Child members of the data structure is an array, it can be indented one space in the below.


-
 - Cat
 - Dog
 - Goldfish

JavaScript turned follows.


[ [ 'Cat', 'Dog', 'Goldfish' ] ]

Array notation may be employed within the row.


animal: [Cat, Dog]

JavaScript turned follows.


{ animal: [ 'Cat', 'Dog' ] }

Fourth, the composite structure

And using the array of objects may be combined to form a composite structure.


languages:
 - Ruby
 - Perl
 - Python 
websites:
 YAML: yaml.org 
 Ruby: ruby-lang.org 
 Python: python.org 
 Perl: use.perl.org 

JavaScript turned follows.


{ languages: [ 'Ruby', 'Perl', 'Python' ],
  websites: 
   { YAML: 'yaml.org',
     Ruby: 'ruby-lang.org',
     Python: 'python.org',
     Perl: 'use.perl.org' } }

V. scalar

Scalar is the most basic, the value can not be divided. The following data types are all pure amount of JavaScript.

  • String
  • Boolean value
  • Integer
  • Float
  • Null
  • time
  • date

Values ​​are expressed directly as literals.


number: 12.30

JavaScript turned follows.


{ number: 12.30 }

Boolean value trueand falserepresent.


isSet: true

JavaScript turned follows.


{ isSet: true }

nullWith ~representation.


parent: ~ 

JavaScript turned follows.


{ parent: null }

Time in ISO8601 format.


iso8601: 2001-12-14t21:59:43.10-05:00 

JavaScript turned follows.


{ iso8601: new Date('2001-12-14t21:59:43.10-05:00') }

Date composite iso8601 format year, month, day.


date: 1976-07-31

JavaScript turned follows.


{ date: new Date('1976-07-31') }

YAML allows two exclamation points, cast data types.


e: !!str 123
f: !!str true

JavaScript turned follows.


{ e: '123', f: 'true' }

Six String

String is the most common, and most complex data type.

String Default does not use quotation marks.


str: 这是一行字符串

JavaScript turned follows.


{ str: '这是一行字符串' }

If among the string contains spaces or special characters, you need to be placed in quotation marks.


str: '内容: 字符串'

JavaScript turned follows.


{ str: '内容: 字符串' }

Single and double quotes can be used, double quotes will not escape special characters.


s1: '内容\n字符串'
s2: "内容\n字符串"

JavaScript turned follows.


{ s1: '内容\\n字符串', s2: '内容\n字符串' }

Among the single quotation marks if there is a single quote, you must use two single quotes to escape.


str: 'labor''s day' 

JavaScript turned follows.


{ str: 'labor\'s day' }

String can be written in multiple lines start from the second row, there must be a single space indent. Line breaks will be converted to spaces.


str: 这是一段
  多行
  字符串

JavaScript turned follows.


{ str: '这是一段 多行 字符串' }

Multiline strings may be used |to retain line breaks, it can also be used >folded wrap.


this: |
  Foo
  Bar
that: >
  Foo
  Bar

Into JavaScript code is as follows.


{ this: 'Foo\nBar\n', that: 'Foo Bar\n' }

+Represents a block of text at the end of the reserved line breaks, -it represents the end of the string deleted wrap.


s1: |
  Foo

s2: |+
  Foo


s3: |-
  Foo

Into JavaScript code is as follows.


{ s1: 'Foo\n', s2: 'Foo\n\n\n', s3: 'Foo' }

HTML tags can be inserted into the string.


message: |

  <p style="color: red">
    段落
  </p>

JavaScript turned follows.


{ message: '\n<p style="color: red">\n  段落\n</p>\n' }

Seven references

Anchors &and aliases *can be used to reference.


defaults: &defaults
  adapter:  postgres
  host:     localhost

development:
  database: myapp_development
  <<: *defaults

test:
  database: myapp_test
  <<: *defaults

It is equivalent to the following code.


defaults:
  adapter:  postgres
  host:     localhost

development:
  database: myapp_development
  adapter:  postgres
  host:     localhost

test:
  database: myapp_test
  adapter:  postgres
  host:     localhost

&Used to create an anchor point ( defaults), <<represents the merged into the current data, *is used to reference anchor.

Here is another example.


- &showell Steve 
- Clark 
- Brian 
- Oren 
- *showell 

Into JavaScript code is as follows.


[ 'Steve', 'Clark', 'Brian', 'Oren', 'Steve' ]

Eight, the regular expression functions and conversion

This is the  JS-YAML  Kut some features, functions, and can be a regular expression to a string.


# example.yml
fn: function () { return 1 }
reg: /test/

Yml code analysis above document is as follows.


var yaml = require('js-yaml');
var fs   = require('fs');

try {
  var doc = yaml.load(
    fs.readFileSync('./example.yml', 'utf8')
  );
  console.log(doc);
} catch (e) {
  console.log(e);
}

JavaScript objects restored from the code yaml file is as follows.


var yaml = require('js-yaml');
var fs   = require('fs');

var obj = {
  fn: function () { return 1 },
  reg: /test/
};

try {
  fs.writeFileSync(
    './example.yml',
    yaml.dump(obj),
    'utf8'
  );
} catch (e) {
  console.log(e);
}

Nine, reference links

Guess you like

Origin blog.csdn.net/suo082407128/article/details/99633452