From aa55c288f2e21aed6ae3038eb52230b4fd8ab78c Mon Sep 17 00:00:00 2001 From: Daniel Parker Date: Mon, 13 Nov 2023 21:55:20 -0500 Subject: [PATCH] path_node has size --- include/jsoncons_ext/jsonpath/expression.hpp | 2 +- include/jsoncons_ext/jsonpath/json_query.hpp | 4 +-- .../jsonpath/jsonpath_expression.hpp | 2 +- include/jsoncons_ext/jsonpath/path_node.hpp | 31 +++++++++---------- test/jsonpath/src/path_node_tests.cpp | 4 +-- 5 files changed, 20 insertions(+), 23 deletions(-) diff --git a/include/jsoncons_ext/jsonpath/expression.hpp b/include/jsoncons_ext/jsonpath/expression.hpp index a8da7e5f99..e758f6198b 100644 --- a/include/jsoncons_ext/jsonpath/expression.hpp +++ b/include/jsoncons_ext/jsonpath/expression.hpp @@ -3065,7 +3065,7 @@ namespace detail { { auto callback = [&result](const path_node_type& pathp, reference) { - result.emplace_back(to_basic_string(pathp)); + result.emplace_back(to_jsonpath(pathp)); }; evaluate(resources, root, path, instance, callback, options); } diff --git a/include/jsoncons_ext/jsonpath/json_query.hpp b/include/jsoncons_ext/jsonpath/json_query.hpp index 48993a6ed9..fd2dc486e9 100644 --- a/include/jsoncons_ext/jsonpath/json_query.hpp +++ b/include/jsoncons_ext/jsonpath/json_query.hpp @@ -129,7 +129,7 @@ namespace jsonpath { auto f = [&callback](const path_node_type& path, reference val) { - callback(to_basic_string(path), val); + callback(to_jsonpath(path), val); }; expr.evaluate_with_replacement(resources, instance, resources.root_path_node(), instance, f); } @@ -156,7 +156,7 @@ namespace jsonpath { auto f = [&callback](const path_node_type& path, reference val) { - callback(to_basic_string(path), val); + callback(to_jsonpath(path), val); }; expr.evaluate_with_replacement(resources, instance, resources.root_path_node(), instance, f); } diff --git a/include/jsoncons_ext/jsonpath/jsonpath_expression.hpp b/include/jsoncons_ext/jsonpath/jsonpath_expression.hpp index 39e6991f9e..72389d4da6 100644 --- a/include/jsoncons_ext/jsonpath/jsonpath_expression.hpp +++ b/include/jsoncons_ext/jsonpath/jsonpath_expression.hpp @@ -2643,7 +2643,7 @@ namespace detail { value_type result(json_array_arg, semantic_tag::none, alloc_); auto callback = [&result](const path_node_type& p, reference) { - result.emplace_back(to_basic_string(p)); + result.emplace_back(to_jsonpath(p)); }; expr_.evaluate(resources, instance, resources.root_path_node(), instance, callback, options); return result; diff --git a/include/jsoncons_ext/jsonpath/path_node.hpp b/include/jsoncons_ext/jsonpath/path_node.hpp index 8b8f5a2e92..ba5472fb19 100644 --- a/include/jsoncons_ext/jsonpath/path_node.hpp +++ b/include/jsoncons_ext/jsonpath/path_node.hpp @@ -34,45 +34,48 @@ namespace jsonpath { using char_type = CharT; private: - char_type root_; const basic_path_node* parent_; + std::size_t size_; path_node_kind node_kind_; + char_type root_; string_view_type name_; std::size_t index_; public: basic_path_node(char_type root) - : root_{root}, parent_(nullptr), + : parent_(nullptr), size_(1), node_kind_(path_node_kind::root), - name_(&root_,1), index_(0) + root_{root}, name_(&root_,1), index_(0) { } basic_path_node(const basic_path_node* parent, const string_view_type& name) - : root_(0), parent_(parent), node_kind_(path_node_kind::name), name_(name), index_(0) + : parent_(parent), size_(parent == nullptr ? 1 : parent->size()+1), + node_kind_(path_node_kind::name), root_(0), name_(name), index_(0) { } basic_path_node(const basic_path_node* parent, std::size_t index) - : root_(0), parent_(parent), node_kind_(path_node_kind::index), index_(index) + : parent_(parent), size_(parent == nullptr ? 1 : parent->size()+1), + node_kind_(path_node_kind::index), root_(0), index_(index) { } basic_path_node(const basic_path_node& other) - : root_(other.root_), - parent_(other.parent_), + : parent_(other.parent_), size_(other.size()), node_kind_(other.node_kind_), - name_(other.node_kind_ == path_node_kind::root ? string_view_type(&root_, 1) : other.name_), + root_(other.root_), name_(other.node_kind_ == path_node_kind::root ? string_view_type(&root_, 1) : other.name_), index_(other.index_) { } basic_path_node& operator=(const basic_path_node& other) { - root_ = other.root_; parent_ = other.parent_; + size_ = other.size(); node_kind_ = other.node_kind_; index_ = other.index_; + root_ = other.root_; name_ = other.node_kind_ == path_node_kind::root ? string_view_type(&root_, 1) : other.name_; return *this; } @@ -91,13 +94,7 @@ namespace jsonpath { std::size_t size() const { - std::size_t len = 1; - - for (auto p = parent_; p != nullptr; p = p->parent_) - { - ++len; - } - return len; + return size_; } std::size_t index() const @@ -506,7 +503,7 @@ namespace jsonpath { } template > - std::basic_string,Allocator> to_basic_string(const basic_path_node& path, const Allocator& alloc=Allocator()) + std::basic_string,Allocator> to_jsonpath(const basic_path_node& path, const Allocator& alloc=Allocator()) { std::basic_string,Allocator> buffer(alloc); diff --git a/test/jsonpath/src/path_node_tests.cpp b/test/jsonpath/src/path_node_tests.cpp index 401ac1f1de..7107056fb2 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_basic_string(a4) == std::string("$['foo']['bar'][0]"))); + CHECK((jsonpath::to_jsonpath(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_basic_string(a4) == std::string(R"($['foo\'s']['bar'][0])")); + CHECK(jsonpath::to_jsonpath(a4) == std::string(R"($['foo\'s']['bar'][0])")); } TEST_CASE("test path_node less")