What is the Yaml language format (learning Yaml)

Source: Internet
Author: User
Tags arrays data structures hash

This article is reproduced in the article, the original address: Http://www.ruanyifeng.com/blog/2016/07/yaml.html?f=tt


Programming is unavoidable to write configuration files, how to write configuration is also a learning.

YAML is a language dedicated to writing configuration files, very concise and powerful, far more convenient than JSON format.

This article describes the syntax of YAML, taking the implementation of JS-YAML as an example. You can go to the online Demo to verify the example below.

First, Introduction

The goal of the YAML language (pronounced/ˈjæməl/) is to facilitate human literacy. It is essentially a general purpose data serialization format.

It has the following basic syntax rules. Case sensitive use indentation to indicate that a hierarchical relationship is indented without using the TAB key, only spaces are allowed. The number of spaces you indent is not important, as long as the same level elements are left aligned

# indicates a comment, which is ignored by the parser, from the word selector until the end of the line.

YAML supports three types of data structures. Object: A collection of key-value pairs, also known as a map (mapping)/hash (hashes)/dictionary (Dictionary) array: a set of sequential values, also known as sequences (sequence)/list (lists) of pure quantities (scalars): Single, non-re-divided values

These three data structures are described below. second, the object

A set of key-value pairs for an object, expressed using a colon structure.

Animal:pets

Switch to JavaScript as follows.

{animal: ' Pets '}

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

Hash: {Name:steve, Foo:bar} 

Switch to JavaScript as follows.

{hash: {name: ' Steve ', foo: ' Bar '}}
three, array

A set of lines at the beginning of a conjunction line that forms an array.

-Cat
-Dog
-Goldfish

Switch to JavaScript as follows.

[' Cat ', ' Dog ', ' goldfish ']

A child member of a data structure is an array, and you can indent a space below the item.

-
 -Cat
 -Dog
 -Goldfish

Switch to JavaScript as follows.

[[' Cat ', ' Dog ', ' goldfish ']

Arrays can also be used in inline notation.

Animal: [Cat, Dog]

Switch to JavaScript as follows.

{animal: [' Cat ', ' Dog ']}
Four, 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 

Switch to JavaScript as follows.

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

The pure quantity is the most basic, non-sub-divided value. The following data types belong to the pure amount of JavaScript. String Boolean integer floating-point number Null time Date

The values are represented directly in the literal form.

number:12.30

Switch to JavaScript as follows.

{number:12.30}

Boolean values are denoted by true and false.

Isset:true

Switch to JavaScript as follows.

{Isset:true}

NULL is represented by ~.

Parent: ~ 

Switch to JavaScript as follows.

{Parent:null}

Time in ISO8601 format.

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

Switch to JavaScript as follows.

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

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

Date:1976-07-31

Switch to JavaScript as follows.

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

YAML allows you to cast a data type with two exclamation marks.

E:!! STR 123
f:!! STR true

Switch to JavaScript as follows.

{e: ' 123 ', F: ' True '}
Six, String

Strings are the most common and most complex type of data.

Strings are not used by default in quotation marks.

STR: This is a line of string

Switch to JavaScript as follows.

{str: ' This is a line string '}

If the string contains spaces or special characters, enclose them in quotation marks.

STR: ' Content: String '

Switch to JavaScript as follows.

{str: ' Content: String '}

Both single and double quotation marks can be used, and double quotes do not escape special characters.

S1: ' content \ n string '
s2: ' content \ n String '

Switch to JavaScript as follows.

{s1: ' content \\n String ', s2: ' content \ n String '}

In single quotation marks, if there is a single quotation mark, you must escape with two single quotes consecutively.

STR: ' Labor ' s Day ' 

Switch to JavaScript as follows.

{str: ' labor\ ' s Day '}

A string can be written as multiple lines, starting from the second line and having a single space indent. NewLine characters are converted to spaces.

STR: This is a
  multi-line
  string

Switch to JavaScript as follows.

{str: ' This is a multi-line string '}

Multiline strings can be used to keep line breaks, or you can use > to Fold wrap.

this: |
  Foo
  bar
that: >
  foo
  bar

The JavaScript code is as follows.

{this: ' foo\nbar\n ', that: ' Foo bar\n '}

+ means to keep line breaks at the end of a block of text--to delete line breaks at the end of a string.

S1: |
  Foo

s2: |+
  foo


s3: |-
  foo

The JavaScript code is as follows.

 {s1: ' foo\n ', S2: 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.