Skip to content

Commit

Permalink
basic_path_node
Browse files Browse the repository at this point in the history
  • Loading branch information
danielaparker committed Nov 10, 2023
1 parent e3123cb commit 68ae13b
Show file tree
Hide file tree
Showing 5 changed files with 34 additions and 46 deletions.
28 changes: 14 additions & 14 deletions include/jsoncons_ext/jsonpath/expression.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -2070,7 +2070,7 @@ namespace detail {
using reference = JsonReference;
using value_pointer = typename std::conditional<std::is_const<typename std::remove_reference<JsonReference>::type>::value,typename Json::const_pointer,typename Json::pointer>::type;
using path_node_type = basic_path_node<typename Json::char_type>;
using json_location_type = basic_json_location<char_type,typename Json::allocator_type>;
using json_location_type = basic_json_location<char_type>;
using path_pointer = const path_node_type*;

json_location_type path_;
Expand Down Expand Up @@ -2131,7 +2131,7 @@ namespace detail {
using reference = JsonReference;
using value_pointer = typename std::conditional<std::is_const<typename std::remove_reference<JsonReference>::type>::value,typename Json::const_pointer,typename Json::pointer>::type;
using path_node_type = basic_path_node<typename Json::char_type>;
using json_location_type = basic_json_location<char_type,typename Json::allocator_type>;
using json_location_type = basic_json_location<char_type>;
using path_pointer = const path_node_type*;
private:
const path_node_type* last_ptr_;
Expand Down Expand Up @@ -2177,7 +2177,7 @@ namespace detail {
using char_type = typename Json::char_type;
using string_type = typename Json::string_type;
using path_node_type = basic_path_node<typename Json::char_type>;
using json_location_type = basic_json_location<char_type,allocator_type>;
using json_location_type = basic_json_location<char_type>;
using path_value_pair_type = path_value_pair<Json,JsonReference>;

allocator_type alloc_;
Expand All @@ -2191,7 +2191,7 @@ namespace detail {
void add(const path_node_type& tail_nodes,
reference value) override
{
nodes.emplace_back(json_location_type(tail_nodes, alloc_), std::addressof(value));
nodes.emplace_back(json_location_type(tail_nodes), std::addressof(value));
}
};

Expand Down Expand Up @@ -2320,7 +2320,7 @@ namespace detail {
using pointer = typename std::conditional<std::is_const<typename std::remove_reference<JsonReference>::type>::value,typename Json::const_pointer,typename Json::pointer>::type;
using path_value_pair_type = path_value_pair<Json,JsonReference>;
using path_node_type = basic_path_node<typename Json::char_type>;
using json_location_type = basic_json_location<char_type,typename Json::allocator_type>;
using json_location_type = basic_json_location<char_type>;
using node_receiver_type = node_receiver<Json,JsonReference>;
using selector_type = jsonpath_selector<Json,JsonReference>;

Expand Down Expand Up @@ -3005,7 +3005,7 @@ namespace detail {
using char_type = typename Json::char_type;
using string_type = typename Json::string_type;
using path_node_type = basic_path_node<typename Json::char_type>;
using json_location_type = basic_json_location<char_type,typename Json::allocator_type>;
using json_location_type = basic_json_location<char_type>;
private:
allocator_type alloc_;
Callback& callback_;
Expand All @@ -3019,7 +3019,7 @@ namespace detail {
void add(const path_node_type& tail_nodes,
reference value) override
{
callback_(json_location_type(tail_nodes, alloc_), value);
callback_(tail_nodes, value);
}
};

Expand All @@ -3041,7 +3041,7 @@ namespace detail {
using reference_arg_type = typename std::conditional<std::is_const<typename std::remove_reference<JsonReference>::type>::value,
const_reference_arg_t,reference_arg_t>::type;
using path_node_type = basic_path_node<typename Json::char_type>;
using json_location_type = basic_json_location<char_type,allocator_type>;
using json_location_type = basic_json_location<char_type>;
using selector_type = jsonpath_selector<Json,JsonReference>;
private:
allocator_type alloc_;
Expand Down Expand Up @@ -3096,7 +3096,7 @@ namespace detail {
}

template <class Callback>
typename std::enable_if<extension_traits::is_binary_function_object<Callback,const json_location_type&,reference>::value,void>::type
typename std::enable_if<extension_traits::is_binary_function_object<Callback,const path_node_type&,reference>::value,void>::type
evaluate(dynamic_resources<Json,JsonReference>& resources,
reference root,
const path_node_type& path,
Expand Down Expand Up @@ -3128,7 +3128,7 @@ namespace detail {
receiver.nodes.erase(last,receiver.nodes.end());
for (auto& node : receiver.nodes)
{
callback(node.path(), node.value());
callback(node.path().base_node(), node.value());
}
}
else
Expand All @@ -3151,15 +3151,15 @@ namespace detail {
}
for (auto& node : temp2)
{
callback(node.path(), node.value());
callback(node.path().base_node(), node.value());
}
}
}
else
{
for (auto& node : receiver.nodes)
{
callback(node.path(), node.value());
callback(node.path().base_node(), node.value());
}
}
}
Expand All @@ -3171,7 +3171,7 @@ namespace detail {
}

template <class Callback>
typename std::enable_if<extension_traits::is_binary_function_object<Callback,const json_location_type&,reference>::value,void>::type
typename std::enable_if<extension_traits::is_binary_function_object<Callback,const path_node_type&,reference>::value,void>::type
evaluate_with_replacement(dynamic_resources<Json,JsonReference>& resources,
reference root,
const path_node_type& path,
Expand All @@ -3196,7 +3196,7 @@ namespace detail {
for (std::size_t i = receiver.nodes.size(); i-- > 0;)
{
auto& node = receiver.nodes[i];
callback(node.path(), node.value());
callback(node.path().base_node(), node.value());
}
}
}
Expand Down
2 changes: 1 addition & 1 deletion include/jsoncons_ext/jsonpath/json_location.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -387,7 +387,7 @@ namespace jsonpath {
};

template <class Json>
Json* select(Json& root, const basic_json_location<typename Json::char_type,typename Json::allocator_type>& path)
Json* select(Json& root, const basic_json_location<typename Json::char_type>& path)
{
Json* current = std::addressof(root);
for (const auto& basic_path_node : path)
Expand Down
38 changes: 13 additions & 25 deletions include/jsoncons_ext/jsonpath/jsonpath_expression.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -111,7 +111,7 @@ namespace detail {
using token_type = token<Json,JsonReference>;
using path_expression_type = path_expression<Json,JsonReference>;
using expression_type = expression<Json,JsonReference>;
using json_location_type = basic_json_location<char_type,allocator_type>;
using json_location_type = basic_json_location<char_type>;
using path_node_type = basic_path_node<typename Json::char_type>;
using selector_type = jsonpath_selector<Json,JsonReference>;

Expand Down Expand Up @@ -2497,7 +2497,7 @@ namespace detail {
using allocator_type = typename value_type::allocator_type;
using evaluator_type = typename jsoncons::jsonpath::detail::jsonpath_evaluator<value_type, reference>;
using path_node_type = basic_path_node<typename Json::char_type>;
using json_location_type = basic_json_location<typename Json::char_type,typename Json::allocator_type>;
using json_location_type = basic_json_location<typename Json::char_type>;
using path_expression_type = jsoncons::jsonpath::detail::path_expression<value_type,reference>;
using path_pointer = const path_node_type*;
};
Expand All @@ -2516,7 +2516,7 @@ namespace detail {
using allocator_type = typename value_type::allocator_type;
using evaluator_type = typename jsoncons::jsonpath::detail::jsonpath_evaluator<value_type, reference>;
using path_node_type = basic_path_node<typename Json::char_type>;
using json_location_type = basic_json_location<typename Json::char_type,typename Json::allocator_type>;
using json_location_type = basic_json_location<typename Json::char_type>;
using path_expression_type = jsoncons::jsonpath::detail::path_expression<value_type,reference>;
using path_pointer = const path_node_type*;
};
Expand All @@ -2537,6 +2537,7 @@ namespace detail {
using const_reference = typename jsonpath_traits_type::const_reference;
using path_expression_type = typename jsonpath_traits_type::path_expression_type;
using json_location_type = typename jsonpath_traits_type::json_location_type;
using path_node_type = typename jsonpath_traits_type::path_node_type;
private:
allocator_type alloc_;
std::unique_ptr<jsoncons::jsonpath::detail::static_resources<value_type,reference>> static_resources_;
Expand All @@ -2562,39 +2563,25 @@ namespace detail {
evaluate(reference instance, BinaryCallback callback, result_options options = result_options()) const
{
jsoncons::jsonpath::detail::dynamic_resources<Json,reference> resources{alloc_};
auto f = [&callback](const json_location_type& path, reference val)
auto f = [&callback](const path_node_type& path, reference val)
{
callback(path.to_string(), val);
basic_json_location<char_type> loc(path);
callback(loc.to_string(), val);
};
expr_.evaluate(resources, instance, resources.root_path_node(), instance, f, options);
}

template <class BinaryCallback>
typename std::enable_if<extension_traits::is_binary_function_object<BinaryCallback,const json_location_type&,const_reference>::value,void>::type
select_nodes(reference instance, BinaryCallback callback, result_options options = result_options()) const
{
jsoncons::jsonpath::detail::dynamic_resources<Json,reference> resources{alloc_};
expr_.evaluate(resources, instance, resources.root_path_node(), instance, callback, options);
}

template <class BinaryCallback>
typename std::enable_if<extension_traits::is_binary_function_object<BinaryCallback,const json_location_type&,Json&>::value,void>::type
update_nodes(reference instance, BinaryCallback callback) const
{
jsoncons::jsonpath::detail::dynamic_resources<Json,reference> resources{alloc_};
expr_.evaluate_with_replacement(resources, instance, resources.root_path_node(), instance, callback);
}

Json evaluate(reference instance, result_options options = result_options()) const
{
if ((options & result_options::path) == result_options::path)
{
jsoncons::jsonpath::detail::dynamic_resources<Json,reference> resources{alloc_};

Json result(json_array_arg, semantic_tag::none, alloc_);
auto callback = [&result](const json_location_type& p, reference)
auto callback = [&result](const path_node_type& path, reference)
{
result.emplace_back(p.to_string());
basic_json_location<char_type> loc(path);
result.emplace_back(loc.to_string());
};
expr_.evaluate(resources, instance, resources.root_path_node(), instance, callback, options);
return result;
Expand Down Expand Up @@ -2623,6 +2610,7 @@ namespace detail {
using const_reference = typename jsonpath_traits_type::const_reference;
using path_expression_type = typename jsonpath_traits_type::path_expression_type;
using json_location_type = typename jsonpath_traits_type::json_location_type;
using path_node_type = typename jsonpath_traits_type::path_node_type;
private:
allocator_type alloc_;
std::unique_ptr<jsoncons::jsonpath::detail::static_resources<value_type,reference>> static_resources_;
Expand All @@ -2644,7 +2632,7 @@ namespace detail {
jsonpath_expr& operator=(jsonpath_expr&&) = default;

template <class BinaryCallback>
typename std::enable_if<extension_traits::is_binary_function_object<BinaryCallback,const json_location_type&,const_reference>::value,void>::type
typename std::enable_if<extension_traits::is_binary_function_object<BinaryCallback,const path_node_type&,const_reference>::value,void>::type
select_nodes(reference instance, BinaryCallback callback, result_options options = result_options()) const
{
jsoncons::jsonpath::detail::dynamic_resources<value_type,reference> resources{alloc_};
Expand Down Expand Up @@ -2673,7 +2661,7 @@ namespace detail {
}

template <class BinaryCallback>
typename std::enable_if<extension_traits::is_binary_function_object<BinaryCallback,const json_location_type&,value_type&>::value,void>::type
typename std::enable_if<extension_traits::is_binary_function_object<BinaryCallback,const path_node_type&,value_type&>::value,void>::type
update_nodes(reference instance, BinaryCallback callback) const
{
jsoncons::jsonpath::detail::dynamic_resources<value_type,reference> resources{alloc_};
Expand Down
6 changes: 3 additions & 3 deletions include/jsoncons_ext/jsonpath/jsonpath_selector.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -583,7 +583,7 @@ namespace detail {

if (ancestor != nullptr)
{
json_location_type path(*ancestor, resources.get_allocator());
json_location_type path(*ancestor);
pointer ptr = jsoncons::jsonpath::select(root,path);
if (ptr != nullptr)
{
Expand All @@ -609,11 +609,11 @@ namespace detail {

if (ancestor != nullptr)
{
json_location_type path(*ancestor, resources.get_allocator());
json_location_type path(*ancestor);
pointer ptr = jsoncons::jsonpath::select(root,path);
if (ptr != nullptr)
{
return this->evaluate_tail(resources, root, /*path.last()*/ *ancestor, *ptr, options, ec);
return this->evaluate_tail(resources, root, *ancestor, *ptr, options, ec);
}
else
{
Expand Down
6 changes: 3 additions & 3 deletions test/jsonpath/src/jsonpath_make_expression_tests.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ TEST_CASE("jsonpath make_expression test")

auto expr = jsoncons::jsonpath::make_jsonpath_expr<const json>("$.books[*]");

auto callback = [&](const jsonpath::json_location& /*location*/, const json& book)
auto callback = [&](const jsonpath::path_node& /*location*/, const json& book)
{
if (book.at("category") == "memoir" && !book.contains("price"))
{
Expand All @@ -82,15 +82,15 @@ TEST_CASE("jsonpath make_expression test")

auto expr = jsoncons::jsonpath::make_jsonpath_expr<json>("$.books[*]");

auto callback1 = [&](const jsonpath::json_location& /*location*/, const json& book)
auto callback1 = [&](const jsonpath::path_node& /*location*/, const json& book)
{
if (book.at("category") == "memoir" && !book.contains("price"))
{
++count;
}
};

auto callback2 = [](const jsonpath::json_location& /*location*/, json& book)
auto callback2 = [](const jsonpath::path_node& /*location*/, json& book)
{
if (book.at("category") == "memoir" && !book.contains("price"))
{
Expand Down

0 comments on commit 68ae13b

Please sign in to comment.