How to define Directives in YAML?

Category: YAMLHow to define Directives in YAML?
Editor">Editor Staff asked 2 years ago

How to define Directives in YAML?

1 Answers
Editor">Editor Staff answered 2 years ago
Directives

Directives can be defined as instructions given to YAML processor.

  • Each directive is denoted b the character % followed by directive name and its parameters.

There are two different types of directives such as

  • Reserved directive
  • YAML directive
  • TAG directive

Reserved directive: YAML should reject unknown directives with an appropriate warning. Those directives are called reserved directives
Example:
%  # This is ignored     # with warning
YAML directive specifies the version of document that YAML follow to.

  • Let us consider an YAML 1.2 version processor which accepts documents with %YAML 1.2 directive and also accept the documents which doesn’t have an directives.
  • If the directive is higher minor version (eg: %YAML 1.3 directive) then the document is processed with an appropriate warning.
  • If the directive is higher major version (eg: %YAML 2.0 directive) then the document is rejected with an appropriate error.
  • If the directive is less than the processor version(eg: %YAML 1.1 directive) , then it should accept the document as 1.2 version is superset of 1.1.

NOTE: It gives an error when you specify more than one directive in same document.

TAG Directive

Node tags are specified through a short-hand notation through TAG directive.

  • Tag directive is associated with handle along with a prefix.

Example:
%TAG !yaml! tag:yaml.org,2002:—!yaml!str “A”

  • You cannot specify more than one TAG directive for one handle.