YAML CPP: Simplify conversion of boolean data
The read conversion of boolean data should now also be correct for less
often used scalar values of booleans such as `y`, `n`, `yes` and `no`.
For more details about possible boolean YAML values, please take a look
[here](https://yaml.org/type/bool.html).
@@ -112,11 +112,12 @@ void addMetadata (Key & key, Node const & node)
Key createLeafKey (Node const & node, string const & name)
{
Key key{ name, KEY_BINARY, KEY_END };
+
if (!node.IsNull ())
{
- auto value = node.as<string> ();
- if (value == "true" || value == "false")
- {
+ // The following code is certainly not ideal from a performance perspective, since it will throw and catch an exception
+ // in the usual case (when the current key does not store boolean data). However the statements more or less reproduces the
+ // steps the main author of yaml-cpp suggest to check for types (see also: https://stackoverflow.com/questions/19994312).
try
{
key.set<bool> (node.as<bool> ());
@@ -124,12 +125,7 @@ Key createLeafKey (Node const & node, string const & name)
}
catch (YAML::BadConversion const &)
{
- key.set<string> (value); // Save value as string, if `node` is a quoted scalar
- }
- }
- else
- {
- key.set<string> (value);
+ key.set<string> (node.as<string> ()); // Save value as string, if `node` is a quoted scalar
}
}
if (node.Tag () == "tag:yaml.org,2002:binary")
