-
Notifications
You must be signed in to change notification settings - Fork 109
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Adding dynamic keys (from short-living local strings) #500
Comments
You need to keep all keys such that they outlive the tree, by serializing it to either the tree or any other place of your choice (that also outlives the tree). To serialize to the tree, you can use This is indeed in the quickstart. For example: //------------------------------------------------------------------
// Adding new nodes:
// adding a keyval node to a map:
CHECK(root.num_children() == 5);
wroot["newkeyval"] = "shiny and new"; // using these strings
wroot.append_child() << ryml::key("newkeyval (serialized)") << "shiny and new (serialized)"; // serializes and assigns the serialization
CHECK(root.num_children() == 7);
CHECK(root["newkeyval"].key() == "newkeyval");
CHECK(root["newkeyval"].val() == "shiny and new");
CHECK(root["newkeyval (serialized)"].key() == "newkeyval (serialized)");
CHECK(root["newkeyval (serialized)"].val() == "shiny and new (serialized)"); |
Not really, I tried it already. This: {
std::string aaa("aaa");
treeroot << ryml::key(ryml::to_csubstr(aaa.c_str()));
} gives
|
The compile error is telling you cannot pass a temporary to ryml::Tree t;
ryml::csubstr s = ryml::to_csubstr(std::string("key"));
t["test"] << ryml::key(s) << std::string("val");
CHECK(ryml::emitrs_yaml<std::string>(t) == "key: val\n"); I agree that the ergonomics of the key tag function could be improved. |
Hi,
first time using the library, and I have an issue with making dynamic keys (the string is generated and has short life time).
I would like to have following structure:
I start with some initial
content
:and now I want to add the key:
Can you help me with this? The
quickstart.cpp
doesn't cover this I think. Perhaps you can include this?The text was updated successfully, but these errors were encountered: