Skip to content

Commit

Permalink
path_node has size
Browse files Browse the repository at this point in the history
  • Loading branch information
danielaparker committed Nov 14, 2023
1 parent 65c19e7 commit aa55c28
Show file tree
Hide file tree
Showing 5 changed files with 20 additions and 23 deletions.
2 changes: 1 addition & 1 deletion include/jsoncons_ext/jsonpath/expression.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
Expand Down
4 changes: 2 additions & 2 deletions include/jsoncons_ext/jsonpath/json_query.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
Expand All @@ -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);
}
Expand Down
2 changes: 1 addition & 1 deletion include/jsoncons_ext/jsonpath/jsonpath_expression.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
31 changes: 14 additions & 17 deletions include/jsoncons_ext/jsonpath/path_node.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
Expand All @@ -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
Expand Down Expand Up @@ -506,7 +503,7 @@ namespace jsonpath {
}

template <class CharT, class Allocator=std::allocator<CharT>>
std::basic_string<CharT,std::char_traits<CharT>,Allocator> to_basic_string(const basic_path_node<CharT>& path, const Allocator& alloc=Allocator())
std::basic_string<CharT,std::char_traits<CharT>,Allocator> to_jsonpath(const basic_path_node<CharT>& path, const Allocator& alloc=Allocator())
{
std::basic_string<CharT,std::char_traits<CharT>,Allocator> buffer(alloc);

Expand Down
4 changes: 2 additions & 2 deletions test/jsonpath/src/path_node_tests.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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")
Expand All @@ -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")
Expand Down

0 comments on commit aa55c28

Please sign in to comment.