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

simple .each/.forEach extension #848

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -279,3 +279,4 @@ script:
brew install nlohmann_json --HEAD ;
brew test nlohmann_json ;
fi

53 changes: 53 additions & 0 deletions src/json.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -13658,6 +13658,59 @@ class basic_json
return ptr.get_checked(this);
}

/*!
@brief execute a provided function once for each item in a container

Iterates over a list of elements, yielding each in turn to an iteratee function. The iteratee is bound to the context object, if one is passed. Each invocation of iteratee is called with three arguments: (element, index, list). If list is a JavaScript object, iteratee's arguments will be (value, key, list). Returns the list for chaining.

@param iteratee Callable object accepting 3 arguments
@param iteratee.value The value of the current item
@param iteratee.key The key/index of the current item
@param iteratee.object The JSON container being iterated

@throw type_error.302 if not is_object() or not is_array()

@complexity Linear in the size the JSON container.

@example j["list"].each([](auto key, auto value, auto& object){ object[key] = std::to_string(value); });

@since never
*/
template <typename Function>
void each(Function function)
{
basic_json& value = *this;
switch (value.m_type)
{
case detail::value_t::array:
{
// iterate array and use index as reference string
for (std::size_t i = 0; i < value.m_value.array->size(); ++i)
{
function(value.m_value.array->operator[](i), i, value);
}
}
break;

case detail::value_t::object:
{
// iterate object and use keys as reference string
for (const auto& element : *value.m_value.object)
{
function(element.second, element.first, value);
}
break;
}

default:
{
// maybe throw an error
JSON_THROW(type_error::create(302, "type must be array or object, but is " + std::string(value.type_name())));
break;
}
}
}

/*!
@brief return flattened JSON value

Expand Down