From 1d055261b4144dbf86b2658437015b15d4dd9bff Mon Sep 17 00:00:00 2001 From: Richard Date: Sun, 4 Sep 2022 00:32:56 +0100 Subject: initial --- include/jsoncons_ext/jsonpath/json_query.hpp | 115 +++++++++++++++++++++++++++ 1 file changed, 115 insertions(+) create mode 100644 include/jsoncons_ext/jsonpath/json_query.hpp (limited to 'include/jsoncons_ext/jsonpath/json_query.hpp') diff --git a/include/jsoncons_ext/jsonpath/json_query.hpp b/include/jsoncons_ext/jsonpath/json_query.hpp new file mode 100644 index 0000000..8facfa9 --- /dev/null +++ b/include/jsoncons_ext/jsonpath/json_query.hpp @@ -0,0 +1,115 @@ +// 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 -- cgit v1.2.3