Nathan Meyer
Nathan Meyer

Reputation: 415

How does YAML Interpret Special Characters

So, I'm attempting to write a plugin configuration file in YAML, but my parser is throwing errors even though everything seems to be formatted correctly. The YAML website is less than helpful, and the only place I've found a tutorial of any kind is the spotty one at docs.ansible.com/ansible/YAMLSyntax.html. Since I am probably missing something silly in the syntax from a general lack of YAML syntax knowledge, what do each of the YAML special characters do?

For reference, here is my misbehaving YAML file:

EnforcedWorlds:|
  - World
  - Nether
  - DIM-1
  - End
  - DIM1
Ignore List:|
  - CuckooClock5000
  - Venomous Potato
  - Subtle Snail
Radius: 10
DisallowInRadius:|
  - Sandstone => "Darude Sandstone!"
  - Quarry (1040) => "This block is too L33T 4U 2 Use here :)"
  - Minium Stone (20102 with datavalues 1 to 1520) => "Minimum Minium area!"
Command Output groups:
  Mod:|
    - Perm Node: gpu.mod
    - (Light Green) $(Banner)
    - (Teal) Command A
    - (Pink) Command B
  Admin:|
    - Perm Node: gpu.admin
    - (Light Red) $(Banner)
    - Mod lines 2-3
    - (Blue) Command C
    - (Bold)
  Owner:|
    - Mod lines 1-3
    - Admin line 2

Upvotes: 7

Views: 52258

Answers (2)

TheZeke
TheZeke

Reputation: 816

YAML doesn't require quoting most strings but you'll want to quote special characters if they fall within a certain list of characters. Use quotes if your value includes any of the following special characters: {, }, [, ], &, *, #, ?, |, -, <, >, =, !, %, @, : also ` and ,

Upvotes: 23

flyx
flyx

Reputation: 39638

Your YAML syntax is invalid not because of the characters, but because of missing spaces:

EnforcedWorlds:|

This whole line is a string. You want it to be a mapping key (EnforcedWorlds), followed by a header for a block scalar (|). For this to work, you must add a space after the colon:

EnforcedWorlds: |

But I am not sure if that is actually what you want. It will parse all following more indented lines as a literal string (preserving newlines). But the content looks like a YAML sequence, so perhaps you want to write it like this:

EnforcedWorlds:
  - World
  - Nether
  - DIM-1
  - End
  - DIM1

Here, the value will be parsed as a sequence of scalar items.

To see how your input is parsed, you can use some web services:

There's also ysh, an interactive YAML shell for testing, using Perl.

Upvotes: 7

Related Questions