Skip to content
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

Iteration weirdness #842

Closed
Exaeta opened this issue Nov 23, 2017 · 10 comments
Closed

Iteration weirdness #842

Exaeta opened this issue Nov 23, 2017 · 10 comments
Labels
solution: proposed fix a fix for the issue has been proposed and waits for confirmation

Comments

@Exaeta
Copy link

Exaeta commented Nov 23, 2017

Feature Request

It seems that right now iterating over a json object iterates over it as though it was an array, which is counter intuitive from the C++ perspective, which you would expect to be iterated like a std::map.

I suggest json::as_map() and json::as_array() so that one can do:

void foobar (nlohmann::json js1, nlohmann::json js2)
{
  for (auto & x : js2.as_map())
  {
    foo(x.first, x.second);
  }

  for (auto & x : js2.as_array())
  {
    bar(x);
  }
}

Both would return proxy objects (objects that contain a reference to the original, and different begin() and end() functions.)

@nlohmann
Copy link
Owner

Have you seen the iteration_wrapper? Here is an example: https://github.com/nlohmann/json/blob/develop/doc/examples/iterator_wrapper.cpp

@pingsoli
Copy link

@nlohmann
Sorry to disturb you. How to merge two json arrays into one ? such as
[ { "name": "Ally", "age": "11" }, ... ] and [ {"name": "Bob", "age": "10"}, ... ] merge into
[ {"name": "Ally", "age": "11" }, ... , {"name": "Bob", "age": "10"}, .... ].

Using iterator can solve it , but, do you have another way? or a faster way?

I'm really sorry to trouble you, I'll appreciate your reply. Thanks.

@nlohmann
Copy link
Owner

@pingsoli see #428.

@nlohmann nlohmann added the solution: proposed fix a fix for the issue has been proposed and waits for confirmation label Nov 25, 2017
@Exaeta
Copy link
Author

Exaeta commented Nov 27, 2017

@nlohmann .key() and .value() are kinda weird from the C++ world. I think imitating std::map would be better personally.

@nlohmann
Copy link
Owner

I did not find a better way. PRs welcome!

@nlohmann
Copy link
Owner

nlohmann commented Dec 6, 2017

@Exaeta Do you have an idea how to implement a better interface?

@nlohmann
Copy link
Owner

Any news on this?

@Exaeta
Copy link
Author

Exaeta commented Dec 12, 2017

I'm not super familiar with the implementation, but thinking about it, you'd probably lose the array vs object distinction if you did that, so maybe an obvious solution is not available.

@nlohmann
Copy link
Owner

Related: #874

@nlohmann
Copy link
Owner

I'm closing this. Hopefully, #874 solves this issue.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
solution: proposed fix a fix for the issue has been proposed and waits for confirmation
Projects
None yet
Development

No branches or pull requests

3 participants