YAML CPP: Add partial support for writing arrays
@@ -43,6 +43,20 @@ NameIterator relativeKeyIterator (Key const & key, Key const & parent)
return keyIterator;
}
+/**
+ * @brief This function checks if a key name specifies an array key.
+ *
+ * @param nameIterator This iterator specifies the name of the key.
+ *
+ * @retval true if `name` specifies an array key
+ * @retval false otherwise
+ */
+bool isArrayKey (NameIterator const & nameIterator)
+{
+ string const & current = *nameIterator;
+ return current.size () != 0 && current.front () == '#';
+}
+
/**
* @brief This function adds a key to a YAML node.
*
@@ -53,15 +67,22 @@ NameIterator relativeKeyIterator (Key const & key, Key const & parent)
void addKey (YAML::Node & data, NameIterator & keyIterator, Key const & key)
{
if (keyIterator == --key.end ())
+ {
+ if (isArrayKey (keyIterator))
+ {
+ data.push_back (YAML::Node (key.getString ()));
+ }
+ else
{
data[*keyIterator] = YAML::Node (key.getString ());
- return;
}
- YAML::Node dictionary = (data[*keyIterator] && data[*keyIterator].IsMap ()) ? data[*keyIterator] : YAML::Node (YAML::NodeType::Map);
- data[*keyIterator] = dictionary;
+ return;
+ }
- addKey (dictionary, ++keyIterator, key);
+ YAML::Node node = (data[*keyIterator] && !data[*keyIterator].IsScalar ()) ? data[*keyIterator] : YAML::Node ();
+ data[*keyIterator] = node;
+ addKey (node, ++keyIterator, key);
}
/**
