// Copyright 2013-2023 Daniel Parker // Distributed under the Boost license, Version 1.0. // (See accompanying file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt) // See https://github.com/danielaparker/jsoncons for latest version #ifndef JSONCONS_JSONPATH_JSON_QUERY_HPP #define JSONCONS_JSONPATH_JSON_QUERY_HPP #include #include namespace jsoncons { namespace jsonpath { template Json json_query(const Json& instance, const typename Json::string_view_type& path, result_options options = result_options(), const custom_functions& functions = custom_functions()) { auto expr = make_expression(path, functions); return expr.evaluate(instance, options); } template typename std::enable_if::value,void>::type json_query(const Json& instance, const typename Json::string_view_type& path, Callback callback, result_options options = result_options(), const custom_functions& functions = custom_functions()) { auto expr = make_expression(path, functions); expr.evaluate(instance, callback, options); } template Json json_query(std::allocator_arg_t, const Alloc& alloc, const Json& instance, const typename Json::string_view_type& path, result_options options = result_options(), const custom_functions& functions = custom_functions()) { auto expr = make_expression(std::allocator_arg, alloc, path, functions); return expr.evaluate(instance, options); } template typename std::enable_if::value,void>::type json_query(std::allocator_arg_t, const Alloc& alloc, const Json& instance, const typename Json::string_view_type& path, Callback callback, result_options options = result_options(), const custom_functions& functions = custom_functions()) { auto expr = make_expression(std::allocator_arg, alloc, path, functions); expr.evaluate(instance, callback, options); } template typename std::enable_if::value,void>::type json_replace(Json& instance, const typename Json::string_view_type& path, T&& new_value, result_options options = result_options::nodups, const custom_functions& funcs = custom_functions()) { using jsonpath_traits_type = jsoncons::jsonpath::detail::jsonpath_traits; 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>(funcs); evaluator_type evaluator; path_expression_type expr = evaluator.compile(*static_resources, path); jsoncons::jsonpath::detail::dynamic_resources resources; auto callback = [&new_value](const json_location_type&, reference v) { v = std::forward(new_value); }; expr.evaluate(resources, instance, resources.root_path_node(), instance, callback, options); } template typename std::enable_if::value,void>::type json_replace(std::allocator_arg_t, const Alloc& alloc, Json& instance, const typename Json::string_view_type& path, T&& new_value, result_options options = result_options::nodups, const custom_functions& funcs = custom_functions()) { using jsonpath_traits_type = jsoncons::jsonpath::detail::jsonpath_traits; 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>(funcs, alloc); evaluator_type evaluator{alloc}; path_expression_type expr = evaluator.compile(*static_resources, path); jsoncons::jsonpath::detail::dynamic_resources resources{alloc}; auto callback = [&new_value](const json_location_type&, reference v) { v = Json(std::forward(new_value), semantic_tag::none); }; expr.evaluate(resources, instance, resources.root_path_node(), instance, callback, options); } template typename std::enable_if::value,void>::type json_replace(Json& instance, const typename Json::string_view_type& path , BinaryCallback callback, result_options options = result_options::nodups, const custom_functions& funcs = custom_functions()) { using jsonpath_traits_type = jsoncons::jsonpath::detail::jsonpath_traits; 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>(funcs); evaluator_type evaluator; path_expression_type expr = evaluator.compile(*static_resources, path); jsoncons::jsonpath::detail::dynamic_resources resources; auto f = [&callback](const json_location_type& path, reference val) { callback(path.to_string(), val); }; expr.evaluate(resources, instance, resources.root_path_node(), instance, f, options); } template typename std::enable_if::value,void>::type json_replace(std::allocator_arg_t, const Alloc& alloc, Json& instance, const typename Json::string_view_type& path , BinaryCallback callback, result_options options = result_options::nodups, const custom_functions& funcs = custom_functions()) { using jsonpath_traits_type = jsoncons::jsonpath::detail::jsonpath_traits; 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>(funcs, alloc); evaluator_type evaluator{alloc}; path_expression_type expr = evaluator.compile(*static_resources, path); jsoncons::jsonpath::detail::dynamic_resources resources{alloc}; auto f = [&callback](const json_location_type& path, reference val) { callback(path.to_string(), val); }; expr.evaluate(resources, instance, resources.root_path_node(), instance, f, options); } // Legacy replace function template typename std::enable_if::value,void>::type json_replace(Json& instance, const typename Json::string_view_type& path , UnaryCallback callback) { using jsonpath_traits_type = jsoncons::jsonpath::detail::jsonpath_traits; 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>(); evaluator_type evaluator; path_expression_type expr = evaluator.compile(*static_resources, path); jsoncons::jsonpath::detail::dynamic_resources resources; auto f = [callback](const json_location_type&, reference v) { v = callback(v); }; expr.evaluate(resources, instance, resources.root_path_node(), instance, f, result_options::nodups); } } // namespace jsonpath } // namespace jsoncons #endif