Skip to content
Permalink
Branch: master
Find file Copy path
Find file Copy path
Fetching contributors…
Cannot retrieve contributors at this time
107 lines (97 sloc) 3.21 KB

YAML Essentials

Introduction

What is YAML

  • YAML Ain't Markup Language
    • Data serialization
    • That's really it: It's just structured data
  • YAML is familiar
    • Data types you know:
      • Lists
        • AKA: Arrays
        • Rundown:
          • Dash-and-space
          • Can be nested
          • Cannot nest with other lists
          • Cannot be empty
          • Can contain a list of associative arrays/mappings
           ---
           - ship: the enterprise
             crew:
               - Jean-Luc Picard
               - William Riker
               - Georgi La Forge
               
          
      • Scalars
        • Can contain strings, numbers, whitespace, Booleans
        • Convert to to string with quotes
        • 'Single quotes' are literal
        • "Double quotes" allow for escape characters (\n)
      • Multiline Scalars:
        • Vertical bar (|) to preserve linebreak
        • Greater than (>) to convert linebreaks to single spaces
        ---
        
        engineering_downtime: |
          42073.1: Navigation system upgrade
          42073.2: Holodeck maintenance
        
        captains_log: >
          Captain's Log, Stardate 42193.6.
          We're on a long reach toward
          the Morgana Quadrant, a section
          of the galaxy which has yet to be
          visited by a manned Federation vessel.
        
      • Associative arrays
        • AKA:
          • Key-value pairs
          • Mappings
        • Run Down:
          • Colon-and-a-space
          • Can be nested
          • Can contain lists
          • "Keys" cannot be duplicated*
          • Values can be empty
          ---
          
          the-enterprise:
            captain: Jean-Luc Picard
            medical:
              - Beverly Crusher
              - Katherine Pulaski
          
          voyager:
            captain: Katherine Janeway
            medical:
              - The Doctor
              - Kes
          

Advancing Your YAML

  • Tags:
    • Allow you to expose tags to certain APIs
    • Let your apps parse your YAML the way you want
  • Anchors:
    • Replicate portions of your data by assigning a reference
  • Structure you know:
    • Dashes
    • Indentation (two spaces)
    • Colons
  • Additional structure:
    • Signal the end of file (in one file)
    • Signal separate documents (in one file)
    ---
    crew:
      - !CAP Jean-Luc Picard
    ...
    ---
    captain: &JANE Katherine Janeway
    crew:
      - *JANE
    ---
    captain: James T. Kirk 
    
You can’t perform that action at this time.