YAML subtlety with saving strings
Aug 17, 2015
Ok, so this one had me stuck for ages, and I thought I’d share what went wrong. I was trying to save strings into YAML, something like the following:
before_string: x after_string: y
I naively thought that this was fine, but if you’re familiar with YAML, you’d know that this creates problems.
After going through the YAML specs, I found the following:
Regexp: y|Y|yes|Yes|YES|n|N|no|No|NO |true|True|TRUE|false|False|FALSE |on|On|ON|off|Off|OFF
In short, any of the above values are automagically converted by YAML into a boolean type. That is, 0 or 1. Whoops.
One extremely fast and simple solution was to opt for JSON persistence instead. It got rid of the problem and seems to be more widely adopted. Who’da think it?
NB: Perhaps it was foolish of me to use YAML this way, but hey, I learned something! Maybe you did too?