Skip to content

Implement a JSON::erase_if method for arrays #1835

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

Merged
merged 1 commit into from
Jul 10, 2025
Merged
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
17 changes: 17 additions & 0 deletions src/core/json/include/sourcemeta/core/json_value.h
Original file line number Diff line number Diff line change
Expand Up @@ -1443,6 +1443,23 @@ class SOURCEMETA_CORE_JSON_EXPORT JSON {
auto erase(typename Array::const_iterator first,
typename Array::const_iterator last) -> typename Array::iterator;

/// This method deletes a set of array elements given a predicate. For
/// example:
///
/// ```cpp
/// #include <sourcemeta/core/json.h>
/// #include <cassert>
///
/// sourcemeta::core::JSON array =
/// sourcemeta::core::parse_json("[ 1, 2, 3 ]");
/// array.erase_if(array,
/// [](const auto &item) { return item.to_integer() % 2 == 0; });
/// assert(array.size(), 2);
/// assert(array.at(0), 1);
/// assert(array.at(1), 3);
/// ```
auto erase_if(const std::function<bool(const JSON &)> &predicate) -> void;

/// This method deletes all members of an object or all elements of an array,
/// leaving them empty. For example:
///
Expand Down
6 changes: 6 additions & 0 deletions src/core/json/json_value.cc
Original file line number Diff line number Diff line change
Expand Up @@ -852,6 +852,12 @@ auto JSON::erase(typename JSON::Array::const_iterator first,
return this->data_array.data.erase(first, last);
}

auto JSON::erase_if(const std::function<bool(const JSON &)> &predicate)
-> void {
assert(this->is_array());
std::erase_if(this->data_array.data, predicate);
}

auto JSON::clear() -> void {
if (this->is_object()) {
this->data_object.data.clear();
Expand Down
42 changes: 42 additions & 0 deletions test/json/json_array_test.cc
Original file line number Diff line number Diff line change
Expand Up @@ -534,3 +534,45 @@ TEST(JSON_array, sort_object_items) {

EXPECT_EQ(document, expected);
}

TEST(JSON_array, erase_if_some) {
auto document = sourcemeta::core::parse_json(R"JSON([
1, 2, 3, 4, 5
])JSON");

document.erase_if([](const auto &item) {
return item.is_integer() && item.to_integer() % 2 == 0;
});

const auto expected = sourcemeta::core::parse_json(R"JSON([
1, 3, 5
])JSON");

EXPECT_EQ(document, expected);
}

TEST(JSON_array, erase_if_none) {
auto document = sourcemeta::core::parse_json(R"JSON([
1, 2, 3, 4, 5
])JSON");

document.erase_if([](const auto &item) { return item.is_boolean(); });

const auto expected = sourcemeta::core::parse_json(R"JSON([
1, 2, 3, 4, 5
])JSON");

EXPECT_EQ(document, expected);
}

TEST(JSON_array, erase_if_all) {
auto document = sourcemeta::core::parse_json(R"JSON([
1, 2, 3, 4, 5
])JSON");

document.erase_if([](const auto &item) { return item.is_integer(); });

const auto expected = sourcemeta::core::parse_json(R"JSON([])JSON");

EXPECT_EQ(document, expected);
}