Replies: 1 comment 1 reply
-
I think you mean to use the following: ondemand::parser parser;
const padded_string json = R"({ "parent": {"child1": {"name": "John"} , "child2": {"name": "Daniel"}} })"_padded;
auto doc = parser.iterate(json);
ondemand::object parent = doc["parent"];
// parent owns the focus
ondemand::object c1 = parent["child1"];
// c1 owns the focus
//
std::string_view as1 = c1["name"];
// We have that as1 == "John", as long as 'parser' and 'json' live
// c2 attempts to grab the focus from parent but fails
ondemand::object c2 = parent["child2"];
// c2 owns the focus, at this point c1 is invalid
std::string_view as2 = c2["name"];
// We have that as2 == "Daniel", as long as 'parser' and 'json' live
std::cout << as1 << " " << as2 << std::endl; // prints John Daniel
They are both safe while valid. You can hold just one child at a time, so by the time you do You need to consume If you compile your code in debug mode, which is highly recommended, you should get immediately the error...
This tells you that your code is unsafe. |
Beta Was this translation helpful? Give feedback.
Uh oh!
There was an error while loading. Please reload this page.
-
Hi,
I found the section on
Iteration Safety
in the documentation and tried the provided code. According to the documentation, c2 is unsafe, but in my tests, c2 behaves correctly while c1 unexpectedly changes its value. Could you please help me understand these results? Thank you very much.MacOS m2 arm64
The output
Beta Was this translation helpful? Give feedback.
All reactions