diff --git a/include/jsoncons_ext/jsonpath/expression.hpp b/include/jsoncons_ext/jsonpath/expression.hpp index c614860881..1f4496a81f 100644 --- a/include/jsoncons_ext/jsonpath/expression.hpp +++ b/include/jsoncons_ext/jsonpath/expression.hpp @@ -3053,7 +3053,7 @@ namespace detail { { auto callback = [&result](const path_node_type& pathp, reference) { - result.emplace_back(to_jsonpath(pathp)); + result.emplace_back(to_basic_string(pathp)); }; evaluate(resources, root, path, instance, callback, options); } diff --git a/include/jsoncons_ext/jsonpath/json_location.hpp b/include/jsoncons_ext/jsonpath/json_location.hpp index 2ad8a391ce..b20eadfed0 100644 --- a/include/jsoncons_ext/jsonpath/json_location.hpp +++ b/include/jsoncons_ext/jsonpath/json_location.hpp @@ -10,6 +10,7 @@ #include #include #include +#include namespace jsoncons { namespace jsonpath { @@ -89,7 +90,7 @@ namespace jsonpath { } }; - template > + template > class basic_json_location { public: @@ -330,11 +331,51 @@ namespace jsonpath { return found ? p_current : nullptr; } + template > + std::basic_string, Allocator> to_basic_string(const basic_json_location& location, + const Allocator& alloc = Allocator()) + { + std::basic_string, Allocator> buffer(alloc); + + buffer.push_back('$'); + for (const auto& element : location) + { + if (element.has_name()) + { + buffer.push_back('['); + buffer.push_back('\''); + jsoncons::jsonpath::escape_string(element.name().data(), element.name().size(), buffer); + buffer.push_back('\''); + buffer.push_back(']'); + } + else + { + buffer.push_back('['); + jsoncons::detail::from_integer(element.index(), buffer); + buffer.push_back(']'); + } + } + + return buffer; + } + using json_location = basic_json_location; using wjson_location = basic_json_location; using path_element = basic_path_element>; using wpath_element = basic_path_element>; + inline + std::string to_string(const json_location& location) + { + return to_basic_string(location); + } + + inline + std::wstring to_wstring(const wjson_location& location) + { + return to_basic_string(location); + } + } // namespace jsonpath } // namespace jsoncons diff --git a/include/jsoncons_ext/jsonpath/json_query.hpp b/include/jsoncons_ext/jsonpath/json_query.hpp index 029b448aa2..b4323cc8e8 100644 --- a/include/jsoncons_ext/jsonpath/json_query.hpp +++ b/include/jsoncons_ext/jsonpath/json_query.hpp @@ -130,7 +130,7 @@ namespace jsonpath { auto f = [&callback](const path_node_type& path, reference val) { - callback(to_jsonpath(path), val); + callback(to_basic_string(path), val); }; expr.evaluate_with_replacement(resources, instance, path_node_type{}, instance, f); } @@ -157,7 +157,7 @@ namespace jsonpath { auto f = [&callback](const path_node_type& path, reference val) { - callback(to_jsonpath(path), val); + callback(to_basic_string(path), val); }; expr.evaluate_with_replacement(resources, instance, path_node_type{}, instance, f); } diff --git a/include/jsoncons_ext/jsonpath/jsonpath_expr.hpp b/include/jsoncons_ext/jsonpath/jsonpath_expr.hpp index e60e968428..fa53acf99e 100644 --- a/include/jsoncons_ext/jsonpath/jsonpath_expr.hpp +++ b/include/jsoncons_ext/jsonpath/jsonpath_expr.hpp @@ -94,7 +94,7 @@ namespace jsonpath { value_type result(json_array_arg, semantic_tag::none, alloc_); auto callback = [&result](const path_node_type& p, reference) { - result.emplace_back(to_jsonpath(p)); + result.emplace_back(to_basic_string(p)); }; expr_.evaluate(resources, instance, path_node_type(), instance, callback, options); return result; @@ -133,7 +133,7 @@ namespace jsonpath { }; template - auto make_jsonpath_expr(const typename Json::string_view_type& path, + auto compile_jsonpath(const typename Json::string_view_type& path, const jsoncons::jsonpath::custom_functions::value_type>& funcs = jsoncons::jsonpath::custom_functions::value_type>()) { using jsonpath_traits_type = jsoncons::jsonpath::jsonpath_traits; @@ -150,7 +150,7 @@ namespace jsonpath { } template - auto make_jsonpath_expr(const allocator_set& alloc_set, + auto compile_jsonpath(const allocator_set& alloc_set, const typename Json::string_view_type& path, const jsoncons::jsonpath::custom_functions::value_type>& funcs, std::error_code& ec) { diff --git a/include/jsoncons_ext/jsonpath/jsonpath_expression.hpp b/include/jsoncons_ext/jsonpath/jsonpath_expression.hpp index 507f187a8c..a2424c3cc3 100644 --- a/include/jsoncons_ext/jsonpath/jsonpath_expression.hpp +++ b/include/jsoncons_ext/jsonpath/jsonpath_expression.hpp @@ -2543,7 +2543,7 @@ namespace detail { jsoncons::jsonpath::detail::dynamic_resources resources{alloc_}; auto f = [&callback](const path_node_type& path, reference val) { - callback(to_jsonpath(path), val); + callback(to_basic_string(path), val); }; expr_.evaluate(resources, instance, path_node_type{}, instance, f, options); } @@ -2557,7 +2557,7 @@ namespace detail { Json result(json_array_arg, semantic_tag::none, alloc_); auto callback = [&result](const path_node_type& path, reference) { - result.emplace_back(to_jsonpath(path)); + result.emplace_back(to_basic_string(path)); }; expr_.evaluate(resources, instance, path_node_type(), instance, callback, options); return result; diff --git a/include/jsoncons_ext/jsonpath/path_node.hpp b/include/jsoncons_ext/jsonpath/path_node.hpp index 3b3b3b216e..5622855e57 100644 --- a/include/jsoncons_ext/jsonpath/path_node.hpp +++ b/include/jsoncons_ext/jsonpath/path_node.hpp @@ -278,7 +278,7 @@ namespace jsonpath { } template > - std::basic_string,Allocator> to_jsonpath(const basic_path_node& path, const Allocator& alloc=Allocator()) + std::basic_string,Allocator> to_basic_string(const basic_path_node& path, const Allocator& alloc=Allocator()) { std::basic_string,Allocator> buffer(alloc); @@ -319,34 +319,6 @@ namespace jsonpath { return buffer; } - template > - std::basic_string, Allocator> to_jsonpath(const basic_json_location& location, - const Allocator& alloc = Allocator()) - { - std::basic_string, Allocator> buffer(alloc); - - buffer.push_back('$'); - for (const auto& element : location) - { - if (element.has_name()) - { - buffer.push_back('['); - buffer.push_back('\''); - jsoncons::jsonpath::escape_string(element.name().data(), element.name().size(), buffer); - buffer.push_back('\''); - buffer.push_back(']'); - } - else - { - buffer.push_back('['); - jsoncons::detail::from_integer(element.index(), buffer); - buffer.push_back(']'); - } - } - - return buffer; - } - template > basic_json_location to_json_location(const basic_path_node& path, const Allocator& alloc=Allocator()) { @@ -382,9 +354,22 @@ namespace jsonpath { return location; } + using path_node = basic_path_node; using wpath_node = basic_path_node; + inline + std::string to_string(const path_node& path) + { + return to_basic_string(path); + } + + inline + std::wstring to_wstring(const wpath_node& path) + { + return to_basic_string(path); + } + } // namespace jsonpath } // namespace jsoncons diff --git a/test/jsonpath/src/jsonpath_make_expression_tests.cpp b/test/jsonpath/src/jsonpath_make_expression_tests.cpp index 3b061894fc..dd6fd39dfc 100644 --- a/test/jsonpath/src/jsonpath_make_expression_tests.cpp +++ b/test/jsonpath/src/jsonpath_make_expression_tests.cpp @@ -58,7 +58,7 @@ TEST_CASE("jsonpath make_expression test") const json doc = json::parse(input); - auto expr = jsoncons::jsonpath::make_jsonpath_expr("$.books[*]"); + auto expr = jsoncons::jsonpath::compile_jsonpath("$.books[*]"); auto callback = [&](const jsonpath::path_node& /*location*/, const json& book) { @@ -80,7 +80,7 @@ TEST_CASE("jsonpath make_expression test") json doc = json::parse(input); - auto expr = jsoncons::jsonpath::make_jsonpath_expr("$.books[*]"); + auto expr = jsoncons::jsonpath::compile_jsonpath("$.books[*]"); auto callback1 = [&](const jsonpath::path_node& /*location*/, const json& book) { diff --git a/test/jsonpath/src/jsonpath_select_paths_tests.cpp b/test/jsonpath/src/jsonpath_select_paths_tests.cpp index 4a42274373..87d607f342 100644 --- a/test/jsonpath/src/jsonpath_select_paths_tests.cpp +++ b/test/jsonpath/src/jsonpath_select_paths_tests.cpp @@ -50,12 +50,12 @@ TEST_CASE("jsonpath.jsonpath select_paths test") SECTION("test 1") { - auto expr = jsonpath::make_jsonpath_expr("$..book[?(@.category == 'fiction')].title"); + auto expr = jsonpath::compile_jsonpath("$..book[?(@.category == 'fiction')].title"); auto result = expr.select_paths(doc); for (const auto& loc : result) { - std::string s = jsonpath::to_jsonpath(loc); + std::string s = jsonpath::to_string(loc); std::cout << s << std::endl; } diff --git a/test/jsonpath/src/path_node_tests.cpp b/test/jsonpath/src/path_node_tests.cpp index 323d122a48..28ec3c2ede 100644 --- a/test/jsonpath/src/path_node_tests.cpp +++ b/test/jsonpath/src/path_node_tests.cpp @@ -24,7 +24,7 @@ TEST_CASE("test json_location equals") jsonpath::path_node b4(&b3,0); CHECK((a4 == b4)); - CHECK((jsonpath::to_jsonpath(a4) == std::string("$['foo']['bar'][0]"))); + CHECK((jsonpath::to_string(a4) == std::string("$['foo']['bar'][0]"))); } TEST_CASE("test json_location with solidus to_string") @@ -34,7 +34,7 @@ TEST_CASE("test json_location with solidus to_string") jsonpath::path_node a3(&a2,"bar"); jsonpath::path_node a4(&a3,0); - CHECK(jsonpath::to_jsonpath(a4) == std::string(R"($['foo\'s']['bar'][0])")); + CHECK(jsonpath::to_string(a4) == std::string(R"($['foo\'s']['bar'][0])")); } TEST_CASE("test path_node less") @@ -135,7 +135,7 @@ TEST_CASE("test to_json_location") std::string jsonpath_string = "$['foo']['bar'][7]"; CHECK((jsonpath::to_json_location(a4) == location)); - CHECK((jsonpath::to_jsonpath(location) == jsonpath_string)); + CHECK((jsonpath::to_string(location) == jsonpath_string)); } }