-
-
Notifications
You must be signed in to change notification settings - Fork 6.7k
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
Printing attribute names #39
Comments
Yes I found this is very annoying because it FORCES the user know all the possible attributes. I think it would be convenient to just follow the convention of std::map, i.e., returning a (key, value) pair instead of the value only, when enumerating the json object. |
Dear @luxe, thanks for reporting! I shall have a look once I'm back from holidays. All the best |
That would be really useful. Even if you know the keys beforehand, being able to iterate over them will save a lot of lines. |
Yay! |
All iterators have now member functions |
That's great. Thanks! |
Sync Fork from Upstream Repo
What would be the best way to iterate over a json object and print the attribute names?
My goal would be to have something like this:
example json:
example printing code:
example results:
Not everything has an attribute name, in which case, it could return an empty string, throw an exception, or return std::optional.
I thought this might be easy to implement, but it appears that all of the attribute names are stored in the parent object, which means that when we iterate over an object, each iterator points to an object that has no way of finding out its own attribute name.
for example, if I were to edit
json.hpp
and give thebasic_json
class this public member function:and run client code like this:
it would result in the following:
I think that illustrates my point about attribute names being stored in the parents.
Anyway, do you have any thoughts on how I could accomplish this?
The text was updated successfully, but these errors were encountered: