Skip to content

delete_at_pointer #1071

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

Open
wants to merge 5 commits into
base: develop
Choose a base branch
from
Open
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
103 changes: 103 additions & 0 deletions include/boost/json/impl/pointer.ipp
Original file line number Diff line number Diff line change
Expand Up @@ -415,6 +415,109 @@ value::find_pointer(string_view ptr, std::error_code& ec) noexcept
return const_cast<value*>(self.find_pointer(ptr, ec));
}

std::pair<bool, string_view>
value::delete_at_pointer(
string_view sv,
system::error_code& ec)
{
ec.clear();


string_view previous_segment;
string_view sv_copy = sv;
string_view err_position;
string_view segment = detail::next_segment(sv, ec);
size_t shift = 0;

auto result = this;
auto previous_result = this;

while (true)
{
if (ec.failed())
return {false, err_position};

if (!result)
{
BOOST_JSON_FAIL(ec, error::not_found);
return {false, err_position};
}

if( segment.empty() )
break;

shift += segment.size();
err_position = sv_copy.substr(0, shift);

previous_segment = segment;
previous_result = result;

switch (result->kind())
{
case boost::json::kind::object: {
auto& obj = result->get_object();

detail::pointer_token const token(segment);
segment = detail::next_segment(sv, ec);

result = detail::if_contains_token(obj, token);
if( !result )
{
BOOST_JSON_FAIL(ec, error::not_found);
return {false, err_position};
}
break;
}
case boost::json::kind::array: {
auto const index = detail::parse_number_token(segment, ec);
segment = detail::next_segment(sv, ec);

auto& arr = result->get_array();
result = arr.if_contains(index);
if( !result )
{
BOOST_JSON_FAIL(ec, error::past_the_end);
return {false, err_position};
}
break;
}
default: {
BOOST_JSON_FAIL(ec, error::value_is_scalar);
return {false, err_position};
}
}
}

err_position = {};

switch (previous_result->kind())
{
case boost::json::kind::object: {
auto& obj = previous_result->get_object();
detail::pointer_token const token(previous_segment);
key_value_pair* kv = detail::find_in_object(obj, token).first;
if (kv) {
obj.erase(kv);
return {true, err_position};
}
return {false,err_position};
}
case boost::json::kind::array: {
auto const index = detail::parse_number_token(previous_segment, ec);
auto& arr = previous_result->get_array();
if (arr.if_contains(index)){
arr.erase(arr.begin() + index);
return {true, err_position};
}
return {false,err_position};
}
default: {
BOOST_JSON_FAIL(ec, error::value_is_scalar);
return {false, err_position};
}
}
}

value*
value::set_at_pointer(
string_view sv,
Expand Down
7 changes: 7 additions & 0 deletions include/boost/json/value.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -3032,6 +3032,13 @@ class value
set_pointer_options const& opts = {} );
/// @}

/** Remove an element via JSON Pointer.
*/
BOOST_JSON_DECL
std::pair<bool, string_view>
delete_at_pointer(
string_view sv,
system::error_code& ec);
//------------------------------------------------------

/** Check if two values are equal.
Expand Down
Loading