English 中文(简体)
YAML – Character Streams
  • 时间:2024-11-03

YAML - Character Streams


Previous Page Next Page  

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 pke comments which are not reflected in seriapzation 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 initiapzed 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 pne 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 −

Exppcit Documents

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

---

some: yaml

...

It includes an exppcit 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"
}

Imppcit 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
"
Advertisements