Skip to content
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
33 changes: 32 additions & 1 deletion include/boost/json/detail/value_to.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,23 @@
#ifndef BOOST_JSON_DETAIL_VALUE_TO_HPP
#define BOOST_JSON_DETAIL_VALUE_TO_HPP

#ifndef BOOST_JSON_INTRUSIVE_INDEX_INC
#define BOOST_JSON_INTRUSIVE_INDEX_INC ((void)0);
#endif

#ifndef BOOST_JSON_INTRUSIVE_PATH_PUSH
#define BOOST_JSON_INTRUSIVE_PATH_PUSH(x) ((void)0);
#endif

#ifndef BOOST_JSON_INTRUSIVE_PATH_POP
#define BOOST_JSON_INTRUSIVE_PATH_POP ((void)0);
#endif

#ifndef BOOST_JSON_INTRUSIVE_MESSAGE
#define BOOST_JSON_INTRUSIVE_MESSAGE(x) ((void)0);
#endif


#include <boost/json/value.hpp>
#include <boost/json/conversion.hpp>
#include <boost/json/result_for.hpp>
Expand Down Expand Up @@ -270,13 +287,21 @@ value_to_impl(
}

auto ins = detail::inserter(result, inserter_implementation<T>());

BOOST_JSON_INTRUSIVE_PATH_PUSH(-1)

for( value const& val: *arr )
{
BOOST_JSON_INTRUSIVE_INDEX_INC

auto elem_res = try_value_to<value_type<T>>( val, ctx );
if( elem_res.has_error() )
return {boost::system::in_place_error, elem_res.error()};
*ins++ = std::move(*elem_res);
}

BOOST_JSON_INTRUSIVE_PATH_POP

return result;
}

Expand Down Expand Up @@ -377,10 +402,14 @@ struct to_described_member
system::error_code ec;
BOOST_JSON_FAIL(ec, error::size_mismatch);
res = {boost::system::in_place_error, ec};

BOOST_JSON_INTRUSIVE_MESSAGE(std::format("the key >> {} << is non optional and missing in path {}", D::name, BOOST_JSON_INTRUSIVE::composePath()));
}
return;
}

BOOST_JSON_INTRUSIVE_PATH_PUSH(D::name)

#if defined(__GNUC__) && BOOST_GCC_VERSION >= 80000 && BOOST_GCC_VERSION < 11000
# pragma GCC diagnostic push
# pragma GCC diagnostic ignored "-Wunused"
Expand All @@ -390,8 +419,10 @@ struct to_described_member
#if defined(__GNUC__) && BOOST_GCC_VERSION >= 80000 && BOOST_GCC_VERSION < 11000
# pragma GCC diagnostic pop
#endif
if( member_res )
if( member_res ){
(*res).* D::pointer = std::move(*member_res);
BOOST_JSON_INTRUSIVE_PATH_POP
}
else
res = {boost::system::in_place_error, member_res.error()};
}
Expand Down
99 changes: 99 additions & 0 deletions include/boost/json/impl/pointer.ipp
Original file line number Diff line number Diff line change
Expand Up @@ -415,6 +415,105 @@ value::find_pointer(string_view ptr, std::error_code& ec) noexcept
return const_cast<value*>(self.find_pointer(ptr, ec));
}

bool
value::erase_at_pointer(
string_view sv,
system::error_code& ec)
{
ec.clear();
if(sv.empty()){
BOOST_JSON_FAIL(ec, error::syntax);
return false;
}

string_view previous_segment;

string_view segment = detail::next_segment(sv, ec);

auto result = this;
auto previous_result = this;

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

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

if( segment.empty() )
break;

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;
}
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;
}
break;
}
default: {
BOOST_JSON_FAIL(ec, error::value_is_scalar);
return false;
}
}
}

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;
}
return false;
}
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;
}
return false;
}
default: {
BOOST_JSON_FAIL(ec, error::value_is_scalar);
return false;
}
}
}

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

/** Remove an element via JSON Pointer.
*/
BOOST_JSON_DECL
bool erase_at_pointer(
string_view sv,
system::error_code& ec);
//------------------------------------------------------

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