Skip to content

Commit

Permalink
jsonpath jsonpath_expression const_reference
Browse files Browse the repository at this point in the history
  • Loading branch information
danielaparker committed Oct 23, 2023
1 parent 29192ba commit cfed965
Showing 1 changed file with 32 additions and 1 deletion.
33 changes: 32 additions & 1 deletion include/jsoncons_ext/jsonpath/jsonpath_expression.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -2538,7 +2538,7 @@ namespace detail {
jsonpath_expression& operator=(jsonpath_expression&&) = default;

template <class BinaryCallback>
typename std::enable_if<extension_traits::is_binary_function_object<BinaryCallback,const string_type&,reference>::value,void>::type
typename std::enable_if<extension_traits::is_binary_function_object<BinaryCallback,const string_type&,const_reference>::value,void>::type
evaluate(const_reference instance, BinaryCallback callback, result_options options = result_options()) const
{
jsoncons::jsonpath::detail::dynamic_resources<Json,reference> resources{alloc_};
Expand All @@ -2549,6 +2549,18 @@ namespace detail {
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 string_type&,Json&>::value,void>::type
evaluate_and_update(reference instance, BinaryCallback callback) const
{
jsoncons::jsonpath::detail::dynamic_resources<Json,reference> resources{alloc_};
auto f = [&callback](const json_location_type& path, reference val)
{
callback(path.to_string(), val);
};
expr_.evaluate_with_replacement(resources, instance, resources.root_path_node(), instance, f);
}

Json evaluate(const_reference instance, result_options options = result_options()) const
{
if ((options & result_options::path) == result_options::path)
Expand Down Expand Up @@ -2641,6 +2653,25 @@ namespace detail {
return jsonpath_expression<Json>(alloc_set, std::move(resources), std::move(expr));
}

template <class Json>
auto make_expression_for_update(const typename Json::string_view_type& path,
const jsoncons::jsonpath::custom_functions<Json>& funcs = jsoncons::jsonpath::custom_functions<Json>())
{
using jsonpath_traits_type = jsoncons::jsonpath::detail::jsonpath_traits<Json, Json&>;

using value_type = typename jsonpath_traits_type::value_type;
using reference = typename jsonpath_traits_type::reference;
using evaluator_type = typename jsonpath_traits_type::evaluator_type;
using path_expression_type = typename jsonpath_traits_type::path_expression_type;
using json_location_type = typename jsonpath_traits_type::json_location_type;

auto static_resources = jsoncons::make_unique<jsoncons::jsonpath::detail::static_resources<value_type,reference>>(funcs);
evaluator_type evaluator;
auto expr = evaluator.compile(*static_resources, path);

return jsoncons::jsonpath::jsonpath_expression<Json,Json&>(jsoncons::combine_allocators(), std::move(static_resources), std::move(expr));
}

} // namespace jsonpath
} // namespace jsoncons

Expand Down

0 comments on commit cfed965

Please sign in to comment.