YAML - Block Scalar Header



In this chapter, we will focus on various scalar types which are used for representing the content. In YAML, comments may either precede or follow scalar content. It is important to note that comments should not be included within scalar content.

Note that all flow scalar styles can include multiple lines, except with usage in multiple keys.

The representation of scalars is given below −

%YAML 1.1
---
!!map {
   ? !!str "simple key"
   : !!map {
      ? !!str "also simple"
      : !!str "value",
      ? !!str "not a simple key"
      : !!str "any value"
   }
}

The generated output of block scalar headers is shown below −

{
   "simple key": {
      "not a simple key": "any value", 
      "also simple": "value"
   }
}

Document Marker Scalar Content

All characters in this example are considered as content, including the inner space characters.

%YAML 1.1
---
!!map {
   ? !!str "---"
   : !!str "foo",
   ? !!str "...",
   : !!str "bar"
}

%YAML 1.1
---
!!seq [
   !!str "---",
   !!str "...",
   !!map {
      ? !!str "---"
      : !!str "..."
   }
]

The plain line breaks are represented with the example given below −

%YAML 1.1
---
!!str "as space \
trimmed\n\
specific\L\n\
none"

The corresponding JSON output for the same is mentioned below −

"as space trimmed\nspecific\u2028\nnone"
Advertisements