YAML - Block Styles


Advertisements

YAML includes two block scalar styles: literal and folded. Block scalars are controlled with few indicators with a header preceding the content itself. An example of block scalar headers is given below −

%YAML 1.2
---
!!seq [
   !!str "literal\n",
   !!str "·folded\n",
   !!str "keep\n\n",
   !!str "·strip",
]

The output in JSON format with a default behavior is given below −

[
   "literal\n", 
   "\u00b7folded\n", 
   "keep\n\n", 
   "\u00b7strip"
]

Types of Block Styles

There are four types of block styles: literal, folded, keep and strip styles. These block styles are defined with the help of Block Chomping scenario. An example of block chomping scenario is given below −

%YAML 1.2
---
!!map {
   ? !!str "strip"
   : !!str "# text",
   ? !!str "clip"
   : !!str "# text\n",
   ? !!str "keep"
   : !!str "# text\n",
}

You can see the output generated with three formats in JSON as given below −

{
   "strip": "# text", 
   "clip": "# text\n", 
   "keep": "# text\n"
}

Chomping in YAML controls the final breaks and trailing empty lines which are interpreted in various forms.

Stripping

In this case, the final line break and empty lines are excluded for scalar content. It is specified by the chomping indicator “-“.

Clipping

Clipping is considered as a default behavior if no explicit chomping indicator is specified. The final break character is preserved in the scalar’s content. The best example of clipping is demonstrated in the example above. It terminates with newline “\n” character.

Keeping

Keeping refers to the addition with representation of “+” chomping indicator. Additional lines created are not subject to folding. The additional lines are not subject to folding.

Advertisements