Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
2.8k views
in Technique[技术] by (71.8m points)

why --- (3 dashes/hyphen) in yaml file?

So I just started using YAML file instead of application.properties as it is more readable. I see in YAML files they start with ---. I googled and found the below explanation.

YAML uses three dashes (“---”) to separate directives from document content. This also serves to signal the start of a document if no directives are present.

Also, I tried a sample without --- and understood that it is not mandatory to have them.

I think I don't have a clear understanding of directive and document. Can anyone please explain with a simple example?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

As you already found out, the three dashes --- are used to signal the start of a document, i.e.:

  1. To signal the document start after directives, i.e., %YAML or %TAG lines according to the current spec. For example:

    %YAML 1.2
    %TAG !foo! !foo-types/
    ---
    myKey: myValue
    
  2. To signal the document start when you have multiple yaml documents in the same stream, e.g., a yaml file:

    doc 1
    ---
    doc 2
    

    If doc 2 has some preceding directives, then we have to use three dots ... to indicate the end of doc 1 (and the start of potential directives preceding doc 2) to the parser. For example:

    doc 1
    ...
    %TAG !bar! !bar-types/
    ---
    doc 2
    

The spec is good for yaml parser implementers. However, I find this article easier to read from a user perspective.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...