-
|
I am using rapidyaml v0.9.0. #include <iostream>
#include <ryml.hpp>
#include <ryml_std.hpp>
int main() {
std::string yaml_content = R"(
LogicalMemoryList:
- Name: "AXI_ACB"
Type: "1W2W3W"
Param: "4"
)";
ryml::Tree tree = ryml::parse_in_arena(ryml::to_csubstr(yaml_content));
ryml::NodeRef root = tree.rootref();
ryml::NodeRef list = root["LogicalMemoryList"];
if (list.is_seq()) {
for (ryml::NodeRef item : list.children()) {
std::string name = item["Name"].val().str;
std::string type = item["Type"].val().str;
std::string param= item["Param"].val().str;
std::cout << "Name: " << name << "\nType: " << type << "\nParam = " << param << std::endl;
}
}
}I am seeing following results: Type: 1W2W3W" Param = 4" However, I am expecting following results: |
Beta Was this translation helpful? Give feedback.
Replies: 2 comments 1 reply
-
|
RTFM.
Do this instead: ryml::csubstr n = item["Name"].val();
std::string name(n.str, n.len);And likewise for the other strings. |
Beta Was this translation helpful? Give feedback.
-
|
Thank you. This worked. But does that mean extraction of every string value needs both the lines? Is there a less verbose way of extracting this value? |
Beta Was this translation helpful? Give feedback.
RTFM.
ryml::csubstris a view, so it is NOT terminated by \0. You need to consider its length. So by using only.stryou are constructing the multiplestd::strings incorrectly, and that's why they go to the end of the file, because there is where the only \0 is located.Do this instead:
And likewise for the other strings.