Given this yaml:
{CR: {cmd: fade, color: blue, panel: 0, value: 30, fout: 0.5, fint: 5},OL: {cmd: text, value: Blu at 30% on all, color: white, time: 5, position: [540,100], size: 50}}
With this code:
bool SEMTools::decodeYaml(QString yaml)
{
try
{
YAML::Node root = YAML::Load(yaml.toStdString().c_str());
YAML::Node::iterator i;
for (i = root.begin(); i != root.end(); i )
{
qDebug() << (*i).first.as<QString>();
}
return true;
}
catch (YAML::TypedBadConversion<QString> const &e)
{
qDebug() << e.what();
}
return false;
}
I'm able to retrieve the lead keys: CR
and OL
.
For each one I also need to retrieve the whole object:
CR: {cmd: fade, color: blue, panel: 0, value: 30, fout: 0.5, fint: 5}
and
OL: {cmd: text, value: Blu at 30% on all, color: white, time: 5, position: [540,100], size: 50}
I tried with:
qDebug() << (*i).as<QString>();
but my application crashes with this error:
terminate called after throwing an instance of 'YAML::InvalidNode'
what(): invalid node; this may result from using a map iterator as a sequence iterator, or vice-versa
What is the right syntax to get the strings above?
CodePudding user response:
(*i).first
is the key, (*i).second
is its value.
Therefore, (*i)
is the whole object as you call it (key value). It's simply not a string, which is why you can't retrieve it via .as<QString>()
. Each the key and the value are a YAML::Node
just like root, and you can do .as<QString>()
only on the key because it's a string. On the value, you can do (*i).second["cmd"].as<QString>()
etc.
If you want the value to be a string instead of a nested YAML structure, you shouldn't input it as nested YAML structure.