yaml profile

Programming inevitably needs to write configuration files. How to write configuration is also a knowledge. YAML language (pronunciation)/ ˈ jæm ə l /) is designed to facilitate human reading and writing. It is essentially a universal data serialization format.

YAML is a language specially used to write configuration files. It is very concise and powerful, which is far more convenient than JSON format.

This paper introduces the syntax of YAML to JS-YAML As an example. You can go Online Demo Verify the following example.

 

Its basic syntax rules are as follows.

  • Case sensitive
  • Use indents to indicate hierarchical relationships
  • The Tab key is not allowed when indenting, only spaces are allowed.
  • The number of indented spaces is not important, as long as the elements of the same level are aligned to the left

 

#Represents a comment. From this character to the end of the line, it will be ignored by the parser.

YAML supports three data structures.

  • Object: a collection of key value pairs, also known as mapping / hashes / dictionary
  • Array: a set of values arranged in order, also known as sequence / list
  • scalars: single, non separable value

These three data structures are described below.

2, Object

A set of key value pairs of an object, represented by a colon structure.

animal: pets

Convert to JavaScript as follows.

{ animal: 'pets' }

Yaml also allows another way to write all key value pairs as an inline object.

hash: { name: Steve, foo: bar } 

Convert to JavaScript as follows.

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

3, Array

A set of lines beginning with a conjunction line to form an array.

- Cat
- Dog
- Goldfish

Convert to JavaScript as follows.

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

If the child member of the data structure is an array, you can indent a space under the item.

-
 - Cat
 - Dog
 - Goldfish

Convert to JavaScript as follows.

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

Arrays can also use inline representation.

animal: [Cat, Dog]

Convert to JavaScript as follows.

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

4, Composite structure

Objects and arrays can be used together to form a composite structure.

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

Convert to JavaScript as follows.

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

5, Pure quantity

Scalar is the most basic and inseparable value. The following data types are scalars of JavaScript.

  • character string
  • Boolean value
  • integer
  • Floating point number
  • Null
  • time
  • date

Values are expressed directly in literal form.

number: 12.30

Convert to JavaScript as follows.

{ number: 12.30 }

Boolean values are represented by true and false.

isSet: true

Convert to JavaScript as follows.

{ isSet: true }

null is represented by ~.

parent: ~ 

Convert to JavaScript as follows.

{ parent: null }

The time is in ISO8601 format.

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

Convert to JavaScript as follows.

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

The date is represented by year, month and day in compound iso8601 format.

date: 1976-07-31

Convert to JavaScript as follows.

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

YAML allows the use of two exclamation marks to cast data types.

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

Convert to JavaScript as follows.

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

6, String

String is the most common and complex data type.

Strings are not quoted by default.

str: This is a line of string

Convert to JavaScript as follows.

{ str: 'This is a line of string' }

If the string contains spaces or special characters, you need to put them in quotation marks.

str: 'Content: String'

Convert to JavaScript as follows.

{ str: 'content: character string' }

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

s1: 'content\n character string'
s2: "content\n character string"

Convert to JavaScript as follows.

{ s1: 'content\\n character string', s2: 'content\n character string' }

If there are single quotation marks in single quotation marks, two single quotation marks must be used continuously to escape.

str: 'labor''s day' 

Convert to JavaScript as follows.

{ str: 'labor\'s day' }

The string can be written in multiple lines. Starting from the second line, there must be a single space indentation. Line breaks are converted to spaces.

str: This is a paragraph
  Multiline
  character string

Convert to JavaScript as follows.

{ str: 'This is a multiline string' }

For multiline strings, you can use | to preserve line breaks or > to collapse line breaks.

this: |
  Foo
  Bar
that: >
  Foo
  Bar

The code to JavaScript is as follows.

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

+Means to keep the line break at the end of the text block, - means to delete the line break at the end of the string.

s1: |
  Foo

s2: |+
  Foo


s3: |-
  Foo

The code to JavaScript 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">
    paragraph
  </p>

Convert to JavaScript as follows.

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

7, Quote

Anchor & and alias *, which can be used to reference.

defaults: &defaults
  adapter:  postgres
  host:     localhost

development:
  database: myapp_development
  <<: *defaults

test:
  database: myapp_test
  <<: *defaults

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

&It is used to establish anchors, < < indicates merging into the current data, * is used to reference anchors.

Here is another example.

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

The code to JavaScript is as follows.

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

8, Conversion of functions and regular expressions

This is JS-YAML Library specific functions can convert functions and regular expressions into strings.

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

The code for parsing the above yml file 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);
}

The code to restore the JavaScript object to the 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);
}

9, Reference link

(end)

 

Posted by johnnyblotter on Wed, 11 May 2022 17:24:54 +0300