Skip to content

Commit

Permalink
select with path_node
Browse files Browse the repository at this point in the history
  • Loading branch information
danielaparker committed Nov 14, 2023
1 parent 94e8211 commit 9043e9c
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 13 deletions.
26 changes: 19 additions & 7 deletions include/jsoncons_ext/jsonpath/json_location.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -463,26 +463,38 @@ namespace jsonpath {
};

template <class Json>
Json* select(Json& root, const basic_json_location<typename Json::char_type>& path)
Json* select(Json& root, const basic_path_node<typename Json::char_type>& path)
{
using path_node_type = basic_path_node<typename Json::char_type>;

std::vector<const path_node_type*> nodes(path.size(), nullptr);
std::size_t len = nodes.size();
const path_node_type* p = std::addressof(path);
while (p != nullptr)
{
nodes[--len] = p;
p = p->parent();
}
while (p != nullptr);

Json* current = std::addressof(root);
for (const auto& basic_path_node : path)
for (auto node : nodes)
{
if (basic_path_node.node_kind() == path_node_kind::index)
if (node->node_kind() == path_node_kind::index)
{
if (current->type() != json_type::array_value || basic_path_node.index() >= current->size())
if (current->type() != json_type::array_value || node->index() >= current->size())
{
return nullptr;
}
current = std::addressof(current->at(basic_path_node.index()));
current = std::addressof(current->at(node->index()));
}
else if (basic_path_node.node_kind() == path_node_kind::name)
else if (node->node_kind() == path_node_kind::name)
{
if (current->type() != json_type::object_value)
{
return nullptr;
}
auto it = current->find(basic_path_node.name());
auto it = current->find(node->name());
if (it == current->object_range().end())
{
return nullptr;
Expand Down
8 changes: 2 additions & 6 deletions include/jsoncons_ext/jsonpath/jsonpath_selector.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -184,7 +184,6 @@ namespace detail {
using pointer = typename supertype::pointer;
using path_value_pair_type = typename supertype::path_value_pair_type;
using path_node_type = typename supertype::path_node_type;
using json_location_type = basic_json_location<char_type>;
using node_receiver_type = typename supertype::node_receiver_type;
using selector_type = typename supertype::selector_type;

Expand Down Expand Up @@ -559,7 +558,6 @@ namespace detail {
using pointer = typename supertype::pointer;
using path_value_pair_type = typename supertype::path_value_pair_type;
using path_node_type = typename supertype::path_node_type;
using json_location_type = typename supertype::path_node_type;
using path_generator_type = path_generator<Json,JsonReference>;
using node_receiver_type = typename supertype::node_receiver_type;

Expand All @@ -585,8 +583,7 @@ namespace detail {

if (ancestor != nullptr)
{
json_location_type path(*ancestor);
pointer ptr = jsoncons::jsonpath::select(root,path);
pointer ptr = jsoncons::jsonpath::select(root,*ancestor);
if (ptr != nullptr)
{
this->tail_select(resources, root, *ancestor, *ptr, receiver, options);
Expand All @@ -611,8 +608,7 @@ namespace detail {

if (ancestor != nullptr)
{
json_location_type path(*ancestor);
pointer ptr = jsoncons::jsonpath::select(root,path);
pointer ptr = jsoncons::jsonpath::select(root, *ancestor);
if (ptr != nullptr)
{
return this->evaluate_tail(resources, root, *ancestor, *ptr, options, ec);
Expand Down

0 comments on commit 9043e9c

Please sign in to comment.