Lessons learned about YAML and Norway

Florian Hirsch
1 min readOct 20, 2015

--

YAML parsers usually do a good job in recognizing the correct types you are using. Parsing following YAML with for instance SnakeYAML will result in a Map containing the String “localhost” and the Integer 8080:

host: localhost
port: 8080

This is also working with mixed types in an Array:

config: [localhost, 8080]

Here we’ll get a List with a String and an Integer. While this is great for this case it may be not great for all cases. A common pitfall is to unintentionally mix types:

hexChars: [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, A, B, C, D, E, F]

Here we’ll get a List containing ten Integers and six Strings. Maybe not what we expected. While this example is quite obvious it may not always be so clear. I recently had problems with the list of Country-Codes defined in ISO-3166:

countryCodes: [AW, AF, AO, AI, AX, AL, AD, AE, AR, AM, AS, ...]

All of the entries are just two alphabetic characters so what could go wrong? It turns out that the Country-Code for Norway ‘NO’ is a boolean value for YAML.

Lessons learned: Always quote Strings in YAML. Always.

--

--