From b7563cb58ee9b877768506a4f7345b2a0ca886ae Mon Sep 17 00:00:00 2001 From: Daniel Parker Date: Tue, 24 Oct 2023 17:19:27 -0400 Subject: [PATCH] json_location -> value_location --- include/jsoncons_ext/jsonpath/expression.hpp | 106 ++++++------- include/jsoncons_ext/jsonpath/json_query.hpp | 20 +-- .../jsonpath/jsonpath_expression.hpp | 18 +-- .../jsonpath/jsonpath_selector.hpp | 106 ++++++------- .../{json_location.hpp => value_location.hpp} | 142 +++++++++--------- test/CMakeLists.txt | 1 + test/jsonpath/src/json_location_tests.cpp | 56 ------- test/jsonpath/src/value_location_tests.cpp | 56 +++++++ 8 files changed, 253 insertions(+), 252 deletions(-) rename include/jsoncons_ext/jsonpath/{json_location.hpp => value_location.hpp} (63%) delete mode 100644 test/jsonpath/src/json_location_tests.cpp create mode 100644 test/jsonpath/src/value_location_tests.cpp diff --git a/include/jsoncons_ext/jsonpath/expression.hpp b/include/jsoncons_ext/jsonpath/expression.hpp index aa6d082820..32c5f645f5 100644 --- a/include/jsoncons_ext/jsonpath/expression.hpp +++ b/include/jsoncons_ext/jsonpath/expression.hpp @@ -18,7 +18,7 @@ #include #endif #include -#include +#include #include namespace jsoncons { @@ -2069,19 +2069,19 @@ namespace detail { using value_type = Json; using reference = JsonReference; using value_pointer = typename std::conditional::type>::value,typename Json::const_pointer,typename Json::pointer>::type; - using json_location_node_type = json_location_node; - using json_location_type = json_location; - using path_pointer = const json_location_node_type*; + using jsonpath_node_type = jsonpath_node; + using value_location_type = value_location; + using path_pointer = const jsonpath_node_type*; - json_location_type path_; + value_location_type path_; value_pointer value_ptr_; - path_value_pair(const json_location_type& path, reference value) noexcept + path_value_pair(const value_location_type& path, reference value) noexcept : path_(path), value_ptr_(std::addressof(value)) { } - path_value_pair(json_location_type&& path, value_pointer valp) noexcept + path_value_pair(value_location_type&& path, value_pointer valp) noexcept : path_(std::move(path)), value_ptr_(valp) { } @@ -2091,7 +2091,7 @@ namespace detail { path_value_pair& operator=(const path_value_pair&) = default; path_value_pair& operator=(path_value_pair&& other) = default; - json_location_type path() const + value_location_type path() const { return path_; } @@ -2130,19 +2130,19 @@ namespace detail { using value_type = Json; using reference = JsonReference; using value_pointer = typename std::conditional::type>::value,typename Json::const_pointer,typename Json::pointer>::type; - using json_location_node_type = json_location_node; - using json_location_type = json_location; - using path_pointer = const json_location_node_type*; + using jsonpath_node_type = jsonpath_node; + using value_location_type = value_location; + using path_pointer = const jsonpath_node_type*; private: - const json_location_node_type* last_ptr_; + const jsonpath_node_type* last_ptr_; value_pointer value_ptr_; public: - path_component_value_pair(const json_location_node_type& last, reference value) noexcept + path_component_value_pair(const jsonpath_node_type& last, reference value) noexcept : last_ptr_(std::addressof(last)), value_ptr_(std::addressof(value)) { } - const json_location_node_type& last() const + const jsonpath_node_type& last() const { return *last_ptr_; } @@ -2160,11 +2160,11 @@ namespace detail { using char_type = typename Json::char_type; using string_type = typename Json::string_type; using reference = JsonReference; - using json_location_node_type = json_location_node; + using jsonpath_node_type = jsonpath_node; virtual ~node_receiver() noexcept = default; - virtual void add(const json_location_node_type& path_tail, + virtual void add(const jsonpath_node_type& tail_nodes, reference value) = 0; }; @@ -2176,8 +2176,8 @@ namespace detail { using reference = JsonReference; using char_type = typename Json::char_type; using string_type = typename Json::string_type; - using json_location_node_type = json_location_node; - using json_location_type = json_location; + using jsonpath_node_type = jsonpath_node; + using value_location_type = value_location; using path_value_pair_type = path_value_pair; allocator_type alloc_; @@ -2188,10 +2188,10 @@ namespace detail { { } - void add(const json_location_node_type& path_tail, + void add(const jsonpath_node_type& tail_nodes, reference value) override { - nodes.emplace_back(json_location_type(path_tail, alloc_), std::addressof(value)); + nodes.emplace_back(value_location_type(tail_nodes, alloc_), std::addressof(value)); } }; @@ -2202,15 +2202,15 @@ namespace detail { using reference = JsonReference; using char_type = typename Json::char_type; using string_type = typename Json::string_type; - using json_location_node_type = json_location_node; + using jsonpath_node_type = jsonpath_node; using path_stem_value_pair_type = path_component_value_pair; std::vector nodes; - void add(const json_location_node_type& path_tail, + void add(const jsonpath_node_type& tail_nodes, reference value) override { - nodes.emplace_back(path_tail, value); + nodes.emplace_back(tail_nodes, value); } }; @@ -2222,12 +2222,12 @@ namespace detail { using string_type = typename Json::string_type; using reference = JsonReference; using pointer = typename std::conditional::type>::value,typename Json::const_pointer,typename Json::pointer>::type; - using json_location_node_type = json_location_node; + using jsonpath_node_type = jsonpath_node; using path_stem_value_pair_type = path_component_value_pair; allocator_type alloc_; std::vector> temp_json_values_; - std::vector> temp_path_node_values_; + std::vector> temp_node_values_; std::unordered_map cache_; string_type length_label_; public: @@ -2269,15 +2269,15 @@ namespace detail { return ptr; } - const json_location_node_type& root_path_node() const + const jsonpath_node_type& root_path_node() const { - static json_location_node_type root(string_type{JSONCONS_CSTRING_CONSTANT(char_type, "$"), alloc_}); + static jsonpath_node_type root(string_type{JSONCONS_CSTRING_CONSTANT(char_type, "$"), alloc_}); return root; } - const json_location_node_type& current_path_node() const + const jsonpath_node_type& current_path_node() const { - static json_location_node_type root(string_type{JSONCONS_CSTRING_CONSTANT(char_type, "@"), alloc_}); + static jsonpath_node_type root(string_type{JSONCONS_CSTRING_CONSTANT(char_type, "@"), alloc_}); return root; } @@ -2287,11 +2287,11 @@ namespace detail { } template - const json_location_node_type* create_path_node(Args&& ... args) + const jsonpath_node_type* create_path_node(Args&& ... args) { - auto temp = jsoncons::make_unique(std::forward(args)...); - json_location_node_type* ptr = temp.get(); - temp_path_node_values_.emplace_back(std::move(temp)); + auto temp = jsoncons::make_unique(std::forward(args)...); + jsonpath_node_type* ptr = temp.get(); + temp_node_values_.emplace_back(std::move(temp)); return ptr; } }; @@ -2319,8 +2319,8 @@ namespace detail { using reference = JsonReference; using pointer = typename std::conditional::type>::value,typename Json::const_pointer,typename Json::pointer>::type; using path_value_pair_type = path_value_pair; - using json_location_node_type = json_location_node; - using json_location_type = json_location; + using jsonpath_node_type = jsonpath_node; + using value_location_type = value_location; using node_receiver_type = node_receiver; using selector_type = jsonpath_selector; @@ -2350,14 +2350,14 @@ namespace detail { virtual void select(dynamic_resources& resources, reference root, - const json_location_node_type& path_tail, + const jsonpath_node_type& tail_nodes, reference val, node_receiver_type& receiver, result_options options) const = 0; virtual reference evaluate(dynamic_resources& resources, reference root, - const json_location_node_type& path_tail, + const jsonpath_node_type& tail_nodes, reference current, result_options options, std::error_code& ec) const = 0; @@ -2623,13 +2623,13 @@ namespace detail { using reference = JsonReference; using pointer = typename std::conditional::type>::value,typename Json::const_pointer,typename Json::pointer>::type; using path_value_pair_type = path_value_pair; - using json_location_node_type = json_location_node; + using jsonpath_node_type = jsonpath_node; virtual ~expression_base() noexcept = default; virtual value_type evaluate(dynamic_resources& resources, reference root, - //const json_location_node_type& path, + //const jsonpath_node_type& path, reference val, result_options options, std::error_code& ec) const = 0; @@ -3004,8 +3004,8 @@ namespace detail { using reference = JsonReference; using char_type = typename Json::char_type; using string_type = typename Json::string_type; - using json_location_node_type = json_location_node; - using json_location_type = json_location; + using jsonpath_node_type = jsonpath_node; + using value_location_type = value_location; private: allocator_type alloc_; Callback& callback_; @@ -3016,10 +3016,10 @@ namespace detail { { } - void add(const json_location_node_type& path_tail, + void add(const jsonpath_node_type& tail_nodes, reference value) override { - callback_(json_location_type(path_tail, alloc_), value); + callback_(value_location_type(tail_nodes, alloc_), value); } }; @@ -3040,8 +3040,8 @@ namespace detail { using token_type = token; using reference_arg_type = typename std::conditional::type>::value, const_reference_arg_t,reference_arg_t>::type; - using json_location_node_type = json_location_node; - using json_location_type = json_location; + using jsonpath_node_type = jsonpath_node; + using value_location_type = value_location; using selector_type = jsonpath_selector; private: allocator_type alloc_; @@ -3069,7 +3069,7 @@ namespace detail { Json evaluate(dynamic_resources& resources, reference root, - const json_location_node_type& path, + const jsonpath_node_type& path, reference instance, result_options options) const { @@ -3077,7 +3077,7 @@ namespace detail { if ((options & result_options::path) == result_options::path) { - auto callback = [&result](const json_location_type& pathp, reference) + auto callback = [&result](const value_location_type& pathp, reference) { result.emplace_back(pathp.to_string()); }; @@ -3085,7 +3085,7 @@ namespace detail { } else { - auto callback = [&result](const json_location_type&, reference val) + auto callback = [&result](const value_location_type&, reference val) { result.push_back(val); }; @@ -3096,10 +3096,10 @@ namespace detail { } template - typename std::enable_if::value,void>::type + typename std::enable_if::value,void>::type evaluate(dynamic_resources& resources, reference root, - const json_location_node_type& path, + const jsonpath_node_type& path, reference current, Callback callback, result_options options) const @@ -3171,10 +3171,10 @@ namespace detail { } template - typename std::enable_if::value,void>::type + typename std::enable_if::value,void>::type evaluate_with_replacement(dynamic_resources& resources, reference root, - const json_location_node_type& path, + const jsonpath_node_type& path, reference current, Callback callback) const { @@ -3238,7 +3238,7 @@ namespace detail { using token_type = token; using reference_arg_type = typename std::conditional::type>::value, const_reference_arg_t,reference_arg_t>::type; - using json_location_node_type = json_location_node; + using jsonpath_node_type = jsonpath_node; using stack_item_type = value_or_pointer; private: std::vector token_list_; diff --git a/include/jsoncons_ext/jsonpath/json_query.hpp b/include/jsoncons_ext/jsonpath/json_query.hpp index 701af94f5a..a24472e06e 100644 --- a/include/jsoncons_ext/jsonpath/json_query.hpp +++ b/include/jsoncons_ext/jsonpath/json_query.hpp @@ -68,14 +68,14 @@ namespace jsonpath { 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; + using value_location_type = typename jsonpath_traits_type::value_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) + auto callback = [&new_value](const value_location_type&, reference v) { v = std::forward(new_value); }; @@ -94,14 +94,14 @@ namespace jsonpath { 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; + using value_location_type = typename jsonpath_traits_type::value_location_type; auto static_resources = jsoncons::make_unique>(funcs, alloc_set.get_allocator()); evaluator_type evaluator{alloc_set.get_allocator()}; path_expression_type expr = evaluator.compile(*static_resources, path); jsoncons::jsonpath::detail::dynamic_resources resources{alloc_set.get_allocator()}; - auto callback = [&new_value](const json_location_type&, reference v) + auto callback = [&new_value](const value_location_type&, reference v) { v = Json(std::forward(new_value), semantic_tag::none); }; @@ -119,7 +119,7 @@ namespace jsonpath { 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; + using value_location_type = typename jsonpath_traits_type::value_location_type; auto static_resources = jsoncons::make_unique>(funcs); evaluator_type evaluator; @@ -127,7 +127,7 @@ namespace jsonpath { jsoncons::jsonpath::detail::dynamic_resources resources; - auto f = [&callback](const json_location_type& pathp, reference val) + auto f = [&callback](const value_location_type& pathp, reference val) { callback(pathp.to_string(), val); }; @@ -146,7 +146,7 @@ namespace jsonpath { 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; + using value_location_type = typename jsonpath_traits_type::value_location_type; auto static_resources = jsoncons::make_unique>(funcs, alloc_set.get_allocator()); evaluator_type evaluator{alloc_set.get_allocator()}; @@ -154,7 +154,7 @@ namespace jsonpath { jsoncons::jsonpath::detail::dynamic_resources resources{alloc_set.get_allocator()}; - auto f = [&callback](const json_location_type& pathp, reference val) + auto f = [&callback](const value_location_type& pathp, reference val) { callback(pathp.to_string(), val); }; @@ -172,14 +172,14 @@ namespace jsonpath { 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; + using value_location_type = typename jsonpath_traits_type::value_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) + auto f = [callback](const value_location_type&, reference v) { v = callback(v); }; diff --git a/include/jsoncons_ext/jsonpath/jsonpath_expression.hpp b/include/jsoncons_ext/jsonpath/jsonpath_expression.hpp index eef51e6dba..c5ddaec205 100644 --- a/include/jsoncons_ext/jsonpath/jsonpath_expression.hpp +++ b/include/jsoncons_ext/jsonpath/jsonpath_expression.hpp @@ -111,8 +111,8 @@ namespace detail { using token_type = token; using path_expression_type = path_expression; using expression_type = expression; - using json_location_type = json_location; - using json_location_node_type = json_location_node; + using value_location_type = value_location; + using jsonpath_node_type = jsonpath_node; using selector_type = jsonpath_selector; private: @@ -2493,10 +2493,10 @@ namespace detail { using pointer = typename std::conditional::type>::value, typename Json::const_pointer, typename Json::pointer>::type; using allocator_type = typename value_type::allocator_type; using evaluator_type = typename jsoncons::jsonpath::detail::jsonpath_evaluator; - using json_location_node_type = json_location_node; - using json_location_type = json_location; + using jsonpath_node_type = jsonpath_node; + using value_location_type = value_location; using path_expression_type = path_expression; - using path_pointer = const json_location_node_type*; + using path_pointer = const jsonpath_node_type*; }; } // namespace detail @@ -2516,7 +2516,7 @@ namespace detail { using reference = typename jsonpath_traits_type::reference; using const_reference = typename jsonpath_traits_type::const_reference; using path_expression_type = typename jsonpath_traits_type::path_expression_type; - using json_location_type = typename jsonpath_traits_type::json_location_type; + using value_location_type = typename jsonpath_traits_type::value_location_type; private: allocator_type alloc_; std::unique_ptr> static_resources_; @@ -2542,7 +2542,7 @@ namespace detail { evaluate(reference instance, BinaryCallback callback, result_options options = result_options()) const { jsoncons::jsonpath::detail::dynamic_resources resources{alloc_}; - auto f = [&callback](const json_location_type& path, reference val) + auto f = [&callback](const value_location_type& path, reference val) { callback(path.to_string(), val); }; @@ -2554,7 +2554,7 @@ namespace detail { evaluate_and_update(reference instance, BinaryCallback callback) const { jsoncons::jsonpath::detail::dynamic_resources resources{alloc_}; - auto f = [&callback](const json_location_type& path, reference val) + auto f = [&callback](const value_location_type& path, reference val) { callback(path.to_string(), val); }; @@ -2568,7 +2568,7 @@ namespace detail { jsoncons::jsonpath::detail::dynamic_resources resources{alloc_}; Json result(json_array_arg, semantic_tag::none, alloc_); - auto callback = [&result](const json_location_type& p, reference) + auto callback = [&result](const value_location_type& p, reference) { result.emplace_back(p.to_string()); }; diff --git a/include/jsoncons_ext/jsonpath/jsonpath_selector.hpp b/include/jsoncons_ext/jsonpath/jsonpath_selector.hpp index 7214464805..bc38318fbf 100644 --- a/include/jsoncons_ext/jsonpath/jsonpath_selector.hpp +++ b/include/jsoncons_ext/jsonpath/jsonpath_selector.hpp @@ -114,7 +114,7 @@ namespace detail { using reference = JsonReference; using char_type = typename Json::char_type; using string_type = typename Json::string_type; - using json_location_node_type = json_location_node; + using jsonpath_node_type = jsonpath_node; Json* val; @@ -123,7 +123,7 @@ namespace detail { { } - void add(const json_location_node_type&, reference value) override + void add(const jsonpath_node_type&, reference value) override { val->emplace_back(value); } @@ -135,10 +135,10 @@ namespace detail { using char_type = typename Json::char_type; using string_view_type = typename Json::string_view_type; using string_type = typename Json::string_type; - using json_location_node_type = json_location_node; + using jsonpath_node_type = jsonpath_node; - static const json_location_node_type& generate(dynamic_resources& resources, - const json_location_node_type& last, + static const jsonpath_node_type& generate(dynamic_resources& resources, + const jsonpath_node_type& last, std::size_t index, result_options options) { @@ -153,8 +153,8 @@ namespace detail { } } - static const json_location_node_type& generate(dynamic_resources& resources, - const json_location_node_type& last, + static const jsonpath_node_type& generate(dynamic_resources& resources, + const jsonpath_node_type& last, const string_type& identifier, result_options options) { @@ -182,8 +182,8 @@ namespace detail { using reference = typename supertype::reference; using pointer = typename supertype::pointer; using path_value_pair_type = typename supertype::path_value_pair_type; - using json_location_node_type = typename supertype::json_location_node_type; - using json_location_type = typename supertype::json_location_type; + using jsonpath_node_type = typename supertype::jsonpath_node_type; + using value_location_type = typename supertype::value_location_type; using node_receiver_type = typename supertype::node_receiver_type; using selector_type = typename supertype::selector_type; @@ -211,7 +211,7 @@ namespace detail { void tail_select(dynamic_resources& resources, reference root, - const json_location_node_type& last, + const jsonpath_node_type& last, reference current, node_receiver_type& receiver, result_options options) const @@ -228,7 +228,7 @@ namespace detail { reference evaluate_tail(dynamic_resources& resources, reference root, - const json_location_node_type& last, + const jsonpath_node_type& last, reference current, result_options options, std::error_code& ec) const @@ -272,7 +272,7 @@ namespace detail { using reference = typename supertype::reference; using pointer = typename supertype::pointer; using path_value_pair_type = typename supertype::path_value_pair_type; - using json_location_node_type = typename supertype::json_location_node_type; + using jsonpath_node_type = typename supertype::jsonpath_node_type; using node_receiver_type = typename supertype::node_receiver_type; private: string_type identifier_; @@ -285,7 +285,7 @@ namespace detail { void select(dynamic_resources& resources, reference root, - const json_location_node_type& last, + const jsonpath_node_type& last, reference current, node_receiver_type& receiver, result_options options) const override @@ -337,7 +337,7 @@ namespace detail { reference evaluate(dynamic_resources& resources, reference root, - const json_location_node_type& last, + const jsonpath_node_type& last, reference current, result_options options, std::error_code& ec) const override @@ -431,7 +431,7 @@ namespace detail { using reference = typename supertype::reference; using pointer = typename supertype::pointer; using path_value_pair_type = typename supertype::path_value_pair_type; - using json_location_node_type = typename supertype::json_location_node_type; + using jsonpath_node_type = typename supertype::jsonpath_node_type; using node_receiver_type = typename supertype::node_receiver_type; root_selector(std::size_t id) @@ -441,7 +441,7 @@ namespace detail { void select(dynamic_resources& resources, reference root, - const json_location_node_type& last, + const jsonpath_node_type& last, reference, node_receiver_type& receiver, result_options options) const override @@ -451,7 +451,7 @@ namespace detail { reference evaluate(dynamic_resources& resources, reference root, - const json_location_node_type& last, + const jsonpath_node_type& last, reference, result_options options, std::error_code& ec) const override @@ -497,7 +497,7 @@ namespace detail { using reference = typename supertype::reference; using pointer = typename supertype::pointer; using path_value_pair_type = typename supertype::path_value_pair_type; - using json_location_node_type = typename supertype::json_location_node_type; + using jsonpath_node_type = typename supertype::jsonpath_node_type; using path_generator_type = path_generator; using node_receiver_type = typename supertype::node_receiver_type; @@ -507,7 +507,7 @@ namespace detail { void select(dynamic_resources& resources, reference root, - const json_location_node_type& last, + const jsonpath_node_type& last, reference current, node_receiver_type& receiver, result_options options) const override @@ -518,7 +518,7 @@ namespace detail { reference evaluate(dynamic_resources& resources, reference root, - const json_location_node_type& last, + const jsonpath_node_type& last, reference current, result_options options, std::error_code& ec) const override @@ -556,8 +556,8 @@ namespace detail { using reference = typename supertype::reference; using pointer = typename supertype::pointer; using path_value_pair_type = typename supertype::path_value_pair_type; - using json_location_node_type = typename supertype::json_location_node_type; - using json_location_type = typename supertype::json_location_type; + using jsonpath_node_type = typename supertype::jsonpath_node_type; + using value_location_type = typename supertype::value_location_type; using path_generator_type = path_generator; using node_receiver_type = typename supertype::node_receiver_type; @@ -568,12 +568,12 @@ namespace detail { void select(dynamic_resources& resources, reference root, - const json_location_node_type& last, + const jsonpath_node_type& last, reference, node_receiver_type& receiver, result_options options) const override { - const json_location_node_type* ancestor = std::addressof(last); + const jsonpath_node_type* ancestor = std::addressof(last); int index = 0; while (ancestor != nullptr && index < ancestor_depth_) { @@ -583,7 +583,7 @@ namespace detail { if (ancestor != nullptr) { - json_location_type path(*ancestor, resources.get_allocator()); + value_location_type path(*ancestor, resources.get_allocator()); pointer ptr = jsoncons::jsonpath::select(root,path); if (ptr != nullptr) { @@ -594,12 +594,12 @@ namespace detail { reference evaluate(dynamic_resources& resources, reference root, - const json_location_node_type& last, + const jsonpath_node_type& last, reference, result_options options, std::error_code& ec) const override { - const json_location_node_type* ancestor = std::addressof(last); + const jsonpath_node_type* ancestor = std::addressof(last); int index = 0; while (ancestor != nullptr && index < ancestor_depth_) { @@ -609,7 +609,7 @@ namespace detail { if (ancestor != nullptr) { - json_location_type path(*ancestor, resources.get_allocator()); + value_location_type path(*ancestor, resources.get_allocator()); pointer ptr = jsoncons::jsonpath::select(root,path); if (ptr != nullptr) { @@ -652,7 +652,7 @@ namespace detail { using reference = typename supertype::reference; using pointer = typename supertype::pointer; using path_value_pair_type = typename supertype::path_value_pair_type; - using json_location_node_type = typename supertype::json_location_node_type; + using jsonpath_node_type = typename supertype::jsonpath_node_type; using path_generator_type = path_generator; using node_receiver_type = typename supertype::node_receiver_type; @@ -663,7 +663,7 @@ namespace detail { void select(dynamic_resources& resources, reference root, - const json_location_node_type& last, + const jsonpath_node_type& last, reference current, node_receiver_type& receiver, result_options options) const override @@ -694,7 +694,7 @@ namespace detail { reference evaluate(dynamic_resources& resources, reference root, - const json_location_node_type& last, + const jsonpath_node_type& last, reference current, result_options options, std::error_code& ec) const override @@ -742,7 +742,7 @@ namespace detail { using reference = typename supertype::reference; using pointer = typename supertype::pointer; using path_value_pair_type = typename supertype::path_value_pair_type; - using json_location_node_type = typename supertype::json_location_node_type; + using jsonpath_node_type = typename supertype::jsonpath_node_type; using path_generator_type = path_generator; using node_receiver_type = typename supertype::node_receiver_type; @@ -753,7 +753,7 @@ namespace detail { void select(dynamic_resources& resources, reference root, - const json_location_node_type& last, + const jsonpath_node_type& last, reference current, node_receiver_type& receiver, result_options options) const override @@ -781,7 +781,7 @@ namespace detail { reference evaluate(dynamic_resources& resources, reference root, - const json_location_node_type& last, + const jsonpath_node_type& last, reference current, result_options options, std::error_code&) const override @@ -817,7 +817,7 @@ namespace detail { using reference = typename supertype::reference; using pointer = typename supertype::pointer; using path_value_pair_type = typename supertype::path_value_pair_type; - using json_location_node_type = typename supertype::json_location_node_type; + using jsonpath_node_type = typename supertype::jsonpath_node_type; using path_generator_type = path_generator; using node_receiver_type = typename supertype::node_receiver_type; @@ -828,7 +828,7 @@ namespace detail { void select(dynamic_resources& resources, reference root, - const json_location_node_type& last, + const jsonpath_node_type& last, reference current, node_receiver_type& receiver, result_options options) const override @@ -856,7 +856,7 @@ namespace detail { reference evaluate(dynamic_resources& resources, reference root, - const json_location_node_type& last, + const jsonpath_node_type& last, reference current, result_options options, std::error_code&) const override @@ -891,8 +891,8 @@ namespace detail { using reference = typename supertype::reference; using pointer = typename supertype::pointer; using path_value_pair_type = typename supertype::path_value_pair_type; - using json_location_node_type = typename supertype::json_location_node_type; - using json_location_type = typename supertype::json_location_type; + using jsonpath_node_type = typename supertype::jsonpath_node_type; + using value_location_type = typename supertype::value_location_type; using path_expression_type = path_expression; using path_generator_type = path_generator; using node_receiver_type = typename supertype::node_receiver_type; @@ -924,7 +924,7 @@ namespace detail { void select(dynamic_resources& resources, reference root, - const json_location_node_type& last, + const jsonpath_node_type& last, reference current, node_receiver_type& receiver, result_options options) const override @@ -937,7 +937,7 @@ namespace detail { reference evaluate(dynamic_resources& resources, reference root, - const json_location_node_type& last, + const jsonpath_node_type& last, reference current, result_options options, std::error_code&) const override @@ -979,7 +979,7 @@ namespace detail { using reference = typename supertype::reference; using pointer = typename supertype::pointer; using path_value_pair_type = typename supertype::path_value_pair_type; - using json_location_node_type = typename supertype::json_location_node_type; + using jsonpath_node_type = typename supertype::jsonpath_node_type; using path_generator_type = path_generator; using node_receiver_type = typename supertype::node_receiver_type; @@ -990,7 +990,7 @@ namespace detail { void select(dynamic_resources& resources, reference root, - const json_location_node_type& last, + const jsonpath_node_type& last, reference current, node_receiver_type& receiver, result_options options) const override @@ -1029,7 +1029,7 @@ namespace detail { reference evaluate(dynamic_resources& resources, reference root, - const json_location_node_type& last, + const jsonpath_node_type& last, reference current, result_options options, std::error_code&) const override @@ -1069,7 +1069,7 @@ namespace detail { using reference = typename supertype::reference; using pointer = typename supertype::pointer; using path_value_pair_type = typename supertype::path_value_pair_type; - using json_location_node_type = typename supertype::json_location_node_type; + using jsonpath_node_type = typename supertype::jsonpath_node_type; using path_generator_type = path_generator; using node_receiver_type = typename supertype::node_receiver_type; @@ -1080,7 +1080,7 @@ namespace detail { void select(dynamic_resources& resources, reference root, - const json_location_node_type& last, + const jsonpath_node_type& last, reference current, node_receiver_type& receiver, result_options options) const override @@ -1109,7 +1109,7 @@ namespace detail { reference evaluate(dynamic_resources& resources, reference root, - const json_location_node_type& last, + const jsonpath_node_type& last, reference current, result_options options, std::error_code& ec) const override @@ -1168,7 +1168,7 @@ namespace detail { using reference = typename supertype::reference; using pointer = typename supertype::pointer; using path_value_pair_type = typename supertype::path_value_pair_type; - using json_location_node_type = typename supertype::json_location_node_type; + using jsonpath_node_type = typename supertype::jsonpath_node_type; using node_receiver_type = typename supertype::node_receiver_type; slice_selector(const slice& slic) @@ -1178,7 +1178,7 @@ namespace detail { void select(dynamic_resources& resources, reference root, - const json_location_node_type& last, + const jsonpath_node_type& last, reference current, node_receiver_type& receiver, result_options options) const override @@ -1232,7 +1232,7 @@ namespace detail { reference evaluate(dynamic_resources& resources, reference root, - const json_location_node_type& last, + const jsonpath_node_type& last, reference current, result_options options, std::error_code&) const override @@ -1256,7 +1256,7 @@ namespace detail { using reference = typename supertype::reference; using pointer = typename supertype::pointer; using path_value_pair_type = typename supertype::path_value_pair_type; - using json_location_node_type = typename supertype::json_location_node_type; + using jsonpath_node_type = typename supertype::jsonpath_node_type; using path_generator_type = path_generator; using node_receiver_type = typename supertype::node_receiver_type; @@ -1267,7 +1267,7 @@ namespace detail { void select(dynamic_resources& resources, reference root, - const json_location_node_type& last, + const jsonpath_node_type& last, reference current, node_receiver_type& receiver, result_options options) const override @@ -1282,7 +1282,7 @@ namespace detail { reference evaluate(dynamic_resources& resources, reference root, - const json_location_node_type& last, + const jsonpath_node_type& last, reference current, result_options options, std::error_code& ec) const override diff --git a/include/jsoncons_ext/jsonpath/json_location.hpp b/include/jsoncons_ext/jsonpath/value_location.hpp similarity index 63% rename from include/jsoncons_ext/jsonpath/json_location.hpp rename to include/jsoncons_ext/jsonpath/value_location.hpp index 38985396c5..b1d7d8a844 100644 --- a/include/jsoncons_ext/jsonpath/json_location.hpp +++ b/include/jsoncons_ext/jsonpath/value_location.hpp @@ -21,44 +21,44 @@ namespace jsoncons { namespace jsonpath { template - class json_location; + class value_location; - enum class json_location_node_kind { root, index, name }; + enum class jsonpath_node_kind { root, index, name }; template - class json_location_node + class jsonpath_node { - friend class json_location; + friend class value_location; public: using string_type = StringT; using char_type = typename StringT::value_type; private: - const json_location_node* parent_; - json_location_node_kind node_kind_; + const jsonpath_node* parent_; + jsonpath_node_kind node_kind_; jsoncons::optional name_; std::size_t index_; public: - json_location_node(string_type&& name) + jsonpath_node(string_type&& name) : parent_(nullptr), - node_kind_(json_location_node_kind::root), + node_kind_(jsonpath_node_kind::root), name_(std::move(name)), index_(0) { } - json_location_node(const json_location_node* parent, const string_type& name) - : parent_(parent), node_kind_(json_location_node_kind::name), name_(name), index_(0) + jsonpath_node(const jsonpath_node* parent, const string_type& name) + : parent_(parent), node_kind_(jsonpath_node_kind::name), name_(name), index_(0) { } - json_location_node(const json_location_node* parent, std::size_t index) - : parent_(parent), node_kind_(json_location_node_kind::index), index_(index) + jsonpath_node(const jsonpath_node* parent, std::size_t index) + : parent_(parent), node_kind_(jsonpath_node_kind::index), index_(index) { } - const json_location_node* parent() const { return parent_;} + const jsonpath_node* parent() const { return parent_;} - json_location_node_kind node_kind() const + jsonpath_node_kind node_kind() const { return node_kind_; } @@ -73,7 +73,7 @@ namespace jsonpath { return index_; } - void swap(json_location_node& node) + void swap(jsonpath_node& node) { std::swap(parent_, node.parent_); std::swap(node_kind_, node.node_kind_); @@ -85,12 +85,12 @@ namespace jsonpath { std::size_t node_hash() const { - std::size_t h = node_kind_ == json_location_node_kind::index ? std::hash{}(index_) : std::hash{}(*name_); + std::size_t h = node_kind_ == jsonpath_node_kind::index ? std::hash{}(index_) : std::hash{}(*name_); return h; } - int compare_node(const json_location_node& other) const + int compare_node(const jsonpath_node& other) const { int diff = 0; if (node_kind_ != other.node_kind_) @@ -101,11 +101,11 @@ namespace jsonpath { { switch (node_kind_) { - case json_location_node_kind::root: - case json_location_node_kind::name: + case jsonpath_node_kind::root: + case jsonpath_node_kind::name: diff = (*name_).compare(*(other.name_)); break; - case json_location_node_kind::index: + case jsonpath_node_kind::index: diff = index_ < other.index_ ? -1 : index_ > other.index_ ? 1 : 0; break; default: @@ -119,7 +119,7 @@ namespace jsonpath { namespace detail { template - class json_location_iterator + class value_location_iterator { Iterator it_; @@ -131,22 +131,22 @@ namespace jsonpath { using pointer = const value_type*; using reference = const value_type&; - json_location_iterator() : it_() + value_location_iterator() : it_() { } - explicit json_location_iterator(Iterator ptr) : it_(ptr) + explicit value_location_iterator(Iterator ptr) : it_(ptr) { } - json_location_iterator(const json_location_iterator&) = default; - json_location_iterator(json_location_iterator&&) = default; - json_location_iterator& operator=(const json_location_iterator&) = default; - json_location_iterator& operator=(json_location_iterator&&) = default; + value_location_iterator(const value_location_iterator&) = default; + value_location_iterator(value_location_iterator&&) = default; + value_location_iterator& operator=(const value_location_iterator&) = default; + value_location_iterator& operator=(value_location_iterator&&) = default; template ::value && std::is_convertible::value>::type> - json_location_iterator(const json_location_iterator& other) + value_location_iterator(const value_location_iterator& other) : it_(other.it_) { } @@ -166,56 +166,56 @@ namespace jsonpath { return (*it_); } - json_location_iterator& operator++() + value_location_iterator& operator++() { ++it_; return *this; } - json_location_iterator operator++(int) + value_location_iterator operator++(int) { - json_location_iterator temp = *this; + value_location_iterator temp = *this; ++*this; return temp; } - json_location_iterator& operator--() + value_location_iterator& operator--() { --it_; return *this; } - json_location_iterator operator--(int) + value_location_iterator operator--(int) { - json_location_iterator temp = *this; + value_location_iterator temp = *this; --*this; return temp; } - json_location_iterator& operator+=(const difference_type offset) + value_location_iterator& operator+=(const difference_type offset) { it_ += offset; return *this; } - json_location_iterator operator+(const difference_type offset) const + value_location_iterator operator+(const difference_type offset) const { - json_location_iterator temp = *this; + value_location_iterator temp = *this; return temp += offset; } - json_location_iterator& operator-=(const difference_type offset) + value_location_iterator& operator-=(const difference_type offset) { return *this += -offset; } - json_location_iterator operator-(const difference_type offset) const + value_location_iterator operator-(const difference_type offset) const { - json_location_iterator temp = *this; + value_location_iterator temp = *this; return temp -= offset; } - difference_type operator-(const json_location_iterator& rhs) const noexcept + difference_type operator-(const value_location_iterator& rhs) const noexcept { return it_ - rhs.it_; } @@ -225,39 +225,39 @@ namespace jsonpath { return *(*(*this + offset)); } - bool operator==(const json_location_iterator& rhs) const noexcept + bool operator==(const value_location_iterator& rhs) const noexcept { return it_ == rhs.it_; } - bool operator!=(const json_location_iterator& rhs) const noexcept + bool operator!=(const value_location_iterator& rhs) const noexcept { return !(*this == rhs); } - bool operator<(const json_location_iterator& rhs) const noexcept + bool operator<(const value_location_iterator& rhs) const noexcept { return it_ < rhs.it_; } - bool operator>(const json_location_iterator& rhs) const noexcept + bool operator>(const value_location_iterator& rhs) const noexcept { return rhs < *this; } - bool operator<=(const json_location_iterator& rhs) const noexcept + bool operator<=(const value_location_iterator& rhs) const noexcept { return !(rhs < *this); } - bool operator>=(const json_location_iterator& rhs) const noexcept + bool operator>=(const value_location_iterator& rhs) const noexcept { return !(*this < rhs); } inline - friend json_location_iterator operator+( - difference_type offset, json_location_iterator next) + friend value_location_iterator operator+( + difference_type offset, value_location_iterator next) { return next += offset; } @@ -266,23 +266,23 @@ namespace jsonpath { } // namespace detail template - class json_location + class value_location { public: using allocator_type = typename StringT::allocator_type; using string_type = StringT; - using json_location_node_type = json_location_node; + using jsonpath_node_type = jsonpath_node; private: allocator_type alloc_; - std::vector nodes_; + std::vector nodes_; public: - using iterator = typename detail::json_location_iterator::iterator>; - using const_iterator = typename detail::json_location_iterator::const_iterator>; + using iterator = typename detail::value_location_iterator::iterator>; + using const_iterator = typename detail::value_location_iterator::const_iterator>; - json_location(const json_location_node_type& node, const allocator_type& alloc = allocator_type()) + value_location(const jsonpath_node_type& node, const allocator_type& alloc = allocator_type()) : alloc_(alloc) { - const json_location_node_type* p = std::addressof(node); + const jsonpath_node_type* p = std::addressof(node); do { nodes_.push_back(p); @@ -313,7 +313,7 @@ namespace jsonpath { return const_iterator(nodes_.end()); } - const json_location_node_type& last() const + const jsonpath_node_type& last() const { return *nodes_.back(); } @@ -326,17 +326,17 @@ namespace jsonpath { { switch (node->node_kind()) { - case json_location_node_kind::root: + case jsonpath_node_kind::root: buffer.append(node->name()); break; - case json_location_node_kind::name: + case jsonpath_node_kind::name: buffer.push_back('['); buffer.push_back('\''); jsoncons::jsonpath::escape_string(node->name().data(), node->name().size(), buffer); buffer.push_back('\''); buffer.push_back(']'); break; - case json_location_node_kind::index: + case jsonpath_node_kind::index: buffer.push_back('['); jsoncons::detail::from_integer(node->index(), buffer); buffer.push_back(']'); @@ -347,7 +347,7 @@ namespace jsonpath { return buffer; } - int compare(const json_location& other) const + int compare(const value_location& other) const { if (this == &other) { @@ -385,43 +385,43 @@ namespace jsonpath { return hash; } - friend bool operator==(const json_location& lhs, const json_location& rhs) + friend bool operator==(const value_location& lhs, const value_location& rhs) { return lhs.compare(rhs) == 0; } - friend bool operator!=(const json_location& lhs, const json_location& rhs) + friend bool operator!=(const value_location& lhs, const value_location& rhs) { return !(lhs == rhs); } - friend bool operator<(const json_location& lhs, const json_location& rhs) + friend bool operator<(const value_location& lhs, const value_location& rhs) { return lhs.compare(rhs) < 0; } }; template - Json* select(Json& root, const json_location& path) + Json* select(Json& root, const value_location& path) { Json* current = std::addressof(root); - for (const auto& json_location_node : path) + for (const auto& jsonpath_node : path) { - if (json_location_node.node_kind() == json_location_node_kind::index) + if (jsonpath_node.node_kind() == jsonpath_node_kind::index) { - if (current->type() != json_type::array_value || json_location_node.index() >= current->size()) + if (current->type() != json_type::array_value || jsonpath_node.index() >= current->size()) { return nullptr; } - current = std::addressof(current->at(json_location_node.index())); + current = std::addressof(current->at(jsonpath_node.index())); } - else if (json_location_node.node_kind() == json_location_node_kind::name) + else if (jsonpath_node.node_kind() == jsonpath_node_kind::name) { if (current->type() != json_type::object_value) { return nullptr; } - auto it = current->find(json_location_node.name()); + auto it = current->find(jsonpath_node.name()); if (it == current->object_range().end()) { return nullptr; diff --git a/test/CMakeLists.txt b/test/CMakeLists.txt index a47599bae5..d512dfd85c 100644 --- a/test/CMakeLists.txt +++ b/test/CMakeLists.txt @@ -87,6 +87,7 @@ add_executable(unit_tests jsonpatch/src/jsonpatch_test_suite.cpp jsonpatch/src/jsonpatch_tests.cpp jsonpath/src/jsonpath_flatten_tests.cpp + jsonpath/src/value_location_tests.cpp jsonpath/src/jsonpath_custom_function_tests.cpp jsonpath/src/jsonpath_json_query_tests.cpp jsonpath/src/jsonpath_make_expression_tests.cpp diff --git a/test/jsonpath/src/json_location_tests.cpp b/test/jsonpath/src/json_location_tests.cpp deleted file mode 100644 index a8a86300ca..0000000000 --- a/test/jsonpath/src/json_location_tests.cpp +++ /dev/null @@ -1,56 +0,0 @@ -// Copyright 2013-2023 Daniel Parker -// Distributed under Boost license - -#if defined(_MSC_VER) -#include "windows.h" // test no inadvertant macro expansions -#endif -#include -#include -#include - -using json_location_node = jsoncons::jsonpath::json_location_node; -using json_location = jsoncons::jsonpath::json_location; - -TEST_CASE("test json_location equals") -{ - json_location_node component1("$"); - json_location_node component2(&component1,"foo"); - json_location_node component3(&component2,"bar"); - json_location_node component4(&component3,0); - - json_location_node component11("$"); - json_location_node component12(&component11,"foo"); - json_location_node component13(&component12,"bar"); - json_location_node component14(&component13,0); - - json_location path1(component4); - json_location path2(component14); - - CHECK(path1 == path2); -} - -TEST_CASE("test json_location to_string") -{ - json_location_node component1("$"); - json_location_node component2(&component1,"foo"); - json_location_node component3(&component2,"bar"); - json_location_node component4(&component3,0); - - json_location path1(component4); - - CHECK(path1.to_string() == std::string("$['foo']['bar'][0]")); -} - -TEST_CASE("test json_location with solidus to_string") -{ - json_location_node component1("$"); - json_location_node component2(&component1,"foo's"); - json_location_node component3(&component2,"bar"); - json_location_node component4(&component3,0); - - json_location path1(component4); - - CHECK(path1.to_string() == std::string(R"($['foo\'s']['bar'][0])")); -} - - diff --git a/test/jsonpath/src/value_location_tests.cpp b/test/jsonpath/src/value_location_tests.cpp new file mode 100644 index 0000000000..37bd8096ab --- /dev/null +++ b/test/jsonpath/src/value_location_tests.cpp @@ -0,0 +1,56 @@ +// Copyright 2013-2023 Daniel Parker +// Distributed under Boost license + +#if defined(_MSC_VER) +#include "windows.h" // test no inadvertant macro expansions +#endif +#include +#include +#include + +using jsonpath_node = jsoncons::jsonpath::jsonpath_node; +using value_location = jsoncons::jsonpath::value_location; + +TEST_CASE("test value_location equals") +{ + jsonpath_node component1("$"); + jsonpath_node component2(&component1,"foo"); + jsonpath_node component3(&component2,"bar"); + jsonpath_node component4(&component3,0); + + jsonpath_node component11("$"); + jsonpath_node component12(&component11,"foo"); + jsonpath_node component13(&component12,"bar"); + jsonpath_node component14(&component13,0); + + value_location path1(component4); + value_location path2(component14); + + CHECK(path1 == path2); +} + +TEST_CASE("test value_location to_string") +{ + jsonpath_node component1("$"); + jsonpath_node component2(&component1,"foo"); + jsonpath_node component3(&component2,"bar"); + jsonpath_node component4(&component3,0); + + value_location path1(component4); + + CHECK(path1.to_string() == std::string("$['foo']['bar'][0]")); +} + +TEST_CASE("test value_location with solidus to_string") +{ + jsonpath_node component1("$"); + jsonpath_node component2(&component1,"foo's"); + jsonpath_node component3(&component2,"bar"); + jsonpath_node component4(&component3,0); + + value_location path1(component4); + + CHECK(path1.to_string() == std::string(R"($['foo\'s']['bar'][0])")); +} + +