// Copyright 2021 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&,const Json&>::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 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 evaluator_t = typename jsoncons::jsonpath::detail::jsonpath_evaluator; //using string_type = typename evaluator_t::string_type; using value_type = typename evaluator_t::value_type; using reference = typename evaluator_t::reference; using json_selector_t = typename evaluator_t::path_expression_type; using json_location_type = typename evaluator_t::json_location_type; jsoncons::jsonpath::detail::static_resources static_resources(funcs); evaluator_t e; json_selector_t expr = e.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(Json& instance, const typename Json::string_view_type& path , UnaryCallback callback) { using evaluator_t = typename jsoncons::jsonpath::detail::jsonpath_evaluator; //using string_type = typename evaluator_t::string_type; using value_type = typename evaluator_t::value_type; using reference = typename evaluator_t::reference; using json_selector_t = typename evaluator_t::path_expression_type; using json_location_type = typename evaluator_t::json_location_type; jsoncons::jsonpath::detail::static_resources static_resources; evaluator_t e; json_selector_t expr = e.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); } template typename std::enable_if&,Json&>::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 evaluator_t = typename jsoncons::jsonpath::detail::jsonpath_evaluator; //using string_type = typename evaluator_t::string_type; using value_type = typename evaluator_t::value_type; using reference = typename evaluator_t::reference; using json_selector_t = typename evaluator_t::path_expression_type; using json_location_type = typename evaluator_t::json_location_type; jsoncons::jsonpath::detail::static_resources static_resources(funcs); evaluator_t e; json_selector_t expr = e.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); } } // namespace jsonpath } // namespace jsoncons #endif