Replies: 6 comments 2 replies
-
@sndth Note that the new function won't work for sequences since the library throws an exception on calling the key function with sequences just as the current implementation: // This is an example using the current implementation
for (auto itr = seq.begin(); itr != seq.end(); ++itr) {
std::cout << itr.key() /*exception thrown here*/ << std::endl;
}
// will work as suggested with range-based for
for (auto& el : seq.items()) {
std::cout << el << std::endl;
}
// but the above is the same as
for (auto& el : seq) {
std::cout << el << std::endl;
}
// (if structured bindings get supported...)
// exception thrown at the `for...` line in the same way as the first one, even with `auto& [val]`,
// but is it necessary or even possible with dynamically sized array...?
for (auto& [_, val] : seq.items()) {
std::cout << val << std::endl;
} Given the above limitation, would it be better to name the function as something like |
Beta Was this translation helpful? Give feedback.
-
Will it be possible to detect the type of the element (variable) inside the Can you also provide the YAML file to get the exceptions you got? Because I cannot reproduce these. Thank you! |
Beta Was this translation helpful? Give feedback.
-
That is possible and that's how the current implementation (like the
That is why we cannot get keys using range-based for loops now. To fix it, I guess we have two options.
Frankly, I can't yet decide which one is better.
Try this YAML. (GitHub doesn't accept YAML files, but just three lines. Please copy and paste them...) - foo
- bar
- baz And my cpp file is: #include <iostream>
#include <fstream>
#include <fkYAML/node.hpp>
int main() {
std::ifstream ifs("./sample.yml");
fkyaml::node n = fkyaml::node::deserialize(ifs);
try {
for (auto itr = n.begin(); itr != n.end(); ++itr) {
std::cout << itr.key() << ": " << itr.value() << std::endl;
}
} catch (const fkyaml::exception& e) {
std::cerr << e.what() << std::endl;
}
return 0;
} You should see the following error log because the
|
Beta Was this translation helpful? Give feedback.
-
Oh, I guess I saw. When you try to get key from array using nlohmann json, you will get item index:
and right - we can't do this currently. |
Beta Was this translation helpful? Give feedback.
-
@sndth Let's wrap up the discussion. If I miss or misunderstand anything, please let me know. Also, I can implement the feature if you're not planning to do so. |
Beta Was this translation helpful? Give feedback.
-
Hello! I tried to implement map_items function but this caused me some problems, mainly due to the iterator class, which I can't understand (too much code haha). I tried to use many methods, e.g. vector: std::vector<iterator> map_items() {
return {begin(), end()};
} but this crashing (cannot dereference end map/set iterator) and showing only: abc:
a: b
b: c
c: d
e: f for (auto itr : n["abc"].map_items()) {
std::cout << itr.key() << ' ' << itr.value() << std::endl;
} |
Beta Was this translation helpful? Give feedback.
-
Hi! From what I saw, there is only one way to get unknown key with value:
for example, in the json library we have items function and we can use:
or:
It would be good idea to implement
items
function to stop using old C++ loop!Beta Was this translation helpful? Give feedback.
All reactions