YAML - Character Streams



In YAML, you come across various character streams as follows −

  • Directives
  • Document Boundary Markers
  • Documents
  • Complete Stream

In this chapter, we will discuss them in detail.

Directives

Directives are basic instructions used in YAML processor. Directives are the presentation details like comments which are not reflected in serialization tree. In YAML, there is no way to define private directives. This section discusses various types of directives with relevant examples −

Reserved Directives

Reserved directives are initialized with three hyphen characters (---) as shown in the example below. The reserved directives are converted into specific value of JSON.

%YAML 1.1
--- !!str
"foo"

YAML Directive

YAML Directives are default directives. If converted in JSON, the value fetched includes forward slash character in preceding and terminating characters.

%YAML 1.1
---
!!str "foo"

Document Boundary Markers

YAML uses these markers to allow more than one document to be contained in one stream. These markers are specially used to convey the structure of YAML document. Note that a line beginning with “---“is used to start a new document.

The following code explains about this with examples −

%YAML 1.1
---
!!str "foo"
%YAML 1.1
---
!!str "bar"
%YAML 1.1
---
!!str "baz"

Documents

YAML document is considered as a single native data structure presented as a single root node. The presentation details in YAML document such as directives, comments, indentation and styles are not considered as contents included in them.

There are two types of documents used in YAML. They are explained in this section −

Explicit Documents

It begins with the document start marker followed by the presentation of the root node. The example of YAML explicit declaration is given below −

---

some: yaml

...

It includes an explicit start and end markers which is “---“and “…” in given example. On converting the specified YAML in JSON format, we get the output as shown below −

{
   "some": "yaml"
}

Implicit Documents

These documents do not begin with a document start marker. Observe the code given below −

fruits:
   - Apple
   - Orange
   - Pineapple
   - Mango

Converting these values in JSON format we get the output as a simple JSON object as given below −

{
   "fruits": [
      "Apple",
      "Orange",
      "Pineapple",
      "Mango"
   ]
}

Complete Stream

YAML includes a sequence of bytes called as character stream. The stream begins with a prefix containing a byte order denoting a character encoding. The complete stream begins with a prefix containing a character encoding, followed by comments.

An example of complete stream (character stream) is shown below −

%YAML 1.1
---
!!str "Text content\n"
Advertisements