Skip to content

Commit

Permalink
added items() function #874
Browse files Browse the repository at this point in the history
  • Loading branch information
nlohmann committed Jan 5, 2018
1 parent 96b40b2 commit 78f8f83
Show file tree
Hide file tree
Showing 5 changed files with 790 additions and 0 deletions.
23 changes: 23 additions & 0 deletions doc/examples/items.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
#include <iostream>
#include "json.hpp"

using json = nlohmann::json;

int main()
{
// create JSON values
json j_object = {{"one", 1}, {"two", 2}};
json j_array = {1, 2, 4, 8, 16};

// example for an object
for (auto& x : j_object.items())
{
std::cout << "key: " << x.key() << ", value: " << x.value() << '\n';
}

// example for an array
for (auto& x : j_array.items())
{
std::cout << "key: " << x.key() << ", value: " << x.value() << '\n';
}
}
1 change: 1 addition & 0 deletions doc/examples/items.link
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
<a target="_blank" href="https://wandbox.org/permlink/E7h0HOuw778Ski8S"><b>online</b></a>
7 changes: 7 additions & 0 deletions doc/examples/items.output
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
key: one, value: 1
key: two, value: 2
key: 0, value: 1
key: 1, value: 2
key: 2, value: 4
key: 3, value: 8
key: 4, value: 16
62 changes: 62 additions & 0 deletions src/json.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -11375,6 +11375,68 @@ class basic_json
return iteration_proxy<const_iterator>(ref);
}

/*!
@brief helper to access iterator member functions in range-based for

This function allows to access @ref iterator::key() and @ref
iterator::value() during range-based for loops. In these loops, a
reference to the JSON values is returned, so there is no access to the
underlying iterator.

For loop without `items()` function:

@code{cpp}
for (auto it = j_object.begin(); it != j_object.end(); ++it)
{
std::cout << "key: " << it.key() << ", value:" << it.value() << '\n';
}
@endcode

Range-based for loop without `items()` function:

@code{cpp}
for (auto it : j_object)
{
// "it" is of type json::reference and has no key() member
std::cout << "value: " << it << '\n';
}
@endcode

Range-based for loop with `items()` function:

@code{cpp}
for (auto it : j_object.items())
{
std::cout << "key: " << it.key() << ", value:" << it.value() << '\n';
}
@endcode

@note When iterating over an array, `key()` will return the index of the
element as string (see example).

@return iteration proxy object wrapping @a ref with an interface to use in
range-based for loops

@liveexample{The following code shows how the function is used.,items}

@exceptionsafety Strong guarantee: if an exception is thrown, there are no
changes in the JSON value.

@complexity Constant.
*/
iteration_proxy<iterator> items()
{
return iteration_proxy<iterator>(*this);
}

/*!
@copydoc items()
*/
iteration_proxy<const_iterator> items() const
{
return iteration_proxy<const_iterator>(*this);
}

/// @}


Expand Down
Loading

0 comments on commit 78f8f83

Please sign in to comment.