1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327
|
// Copyright 2013-2025 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_EXT_JSONPATH_PATH_NODE_HPP
#define JSONCONS_EXT_JSONPATH_PATH_NODE_HPP
#include <algorithm> // std::reverse
#include <cstddef>
#include <functional>
#include <memory>
#include <string>
#include <vector>
#include <jsoncons/config/jsoncons_config.hpp>
#include <jsoncons/detail/write_number.hpp>
#include <jsoncons/json_type.hpp>
#include <jsoncons_ext/jsonpath/jsonpath_utilities.hpp>
namespace jsoncons {
namespace jsonpath {
enum class path_node_kind { root, name, index };
template <typename CharT>
class basic_path_node
{
public:
using string_view_type = jsoncons::basic_string_view<CharT>;
using char_type = CharT;
private:
const basic_path_node* parent_{nullptr};
std::size_t size_{1};
path_node_kind node_kind_;
string_view_type name_;
std::size_t index_{0};
public:
basic_path_node() noexcept
: node_kind_(path_node_kind::root)
{
}
basic_path_node(const basic_path_node* parent, string_view_type name)
: parent_(parent), size_(parent == nullptr ? 1 : parent->size()+1),
node_kind_(path_node_kind::name), name_(name)
{
}
basic_path_node(const basic_path_node* parent, std::size_t index)
: parent_(parent), size_(parent == nullptr ? 1 : parent->size()+1),
node_kind_(path_node_kind::index), index_(index)
{
}
basic_path_node(const basic_path_node& other) = default;
basic_path_node(basic_path_node&& other) = default;
~basic_path_node() = default;
basic_path_node& operator=(const basic_path_node& other) = default;
basic_path_node& operator=(basic_path_node&& other) = default;
const basic_path_node* parent() const { return parent_;}
path_node_kind node_kind() const
{
return node_kind_;
}
string_view_type name() const
{
return name_;
}
std::size_t size() const
{
return size_;
}
std::size_t index() const
{
return index_;
}
void swap(basic_path_node& node) noexcept
{
std::swap(parent_, node.parent_);
std::swap(node_kind_, node.node_kind_);
std::swap(name_, node.name_);
std::swap(index_, node.index_);
}
private:
std::size_t node_hash() const
{
std::size_t h = node_kind_ == path_node_kind::index ? std::hash<std::size_t>{}(index_) : std::hash<string_view_type>{}(name_);
return h;
}
int compare_node(const basic_path_node& other) const
{
int diff = 0;
if (node_kind_ != other.node_kind_)
{
diff = static_cast<int>(node_kind_) - static_cast<int>(other.node_kind_);
}
else
{
switch (node_kind_)
{
case path_node_kind::root:
case path_node_kind::name:
diff = name_.compare(other.name_);
break;
case path_node_kind::index:
diff = index_ < other.index_ ? -1 : index_ > other.index_ ? 1 : 0;
break;
default:
break;
}
}
return diff;
}
friend bool operator<(const basic_path_node& lhs, const basic_path_node& rhs)
{
std::size_t len = (std::min)(lhs.size(),rhs.size());
const basic_path_node* p_lhs = std::addressof(lhs);
const basic_path_node* p_rhs = std::addressof(rhs);
bool is_less = false;
while (p_lhs->size() > len)
{
p_lhs = p_lhs->parent_;
is_less = false;
}
while (p_rhs->size() > len)
{
p_rhs = p_rhs->parent_;
is_less = true;
}
while (p_lhs != nullptr)
{
int diff = 0;
if (p_lhs->node_kind_ != p_rhs->node_kind_)
{
diff = static_cast<int>(p_lhs->node_kind_) - static_cast<int>(p_rhs->node_kind_);
}
else
{
switch (p_lhs->node_kind_)
{
case path_node_kind::root:
case path_node_kind::name:
diff = p_lhs->name_.compare(p_rhs->name_);
break;
case path_node_kind::index:
diff = static_cast<int>(p_lhs->index_) - static_cast<int>(p_rhs->index_);
break;
default:
break;
}
}
if (diff < 0)
{
is_less = true;
}
else if (diff > 0)
{
is_less = false;
}
p_lhs = p_lhs->parent_;
p_rhs = p_rhs->parent_;
}
return is_less;
}
friend bool operator==(const basic_path_node& lhs, const basic_path_node& rhs)
{
if (lhs.size() != rhs.size())
{
return false;
}
const basic_path_node* p_lhs = std::addressof(lhs);
const basic_path_node* p_rhs = std::addressof(rhs);
bool is_equal = true;
while (p_lhs != nullptr && is_equal)
{
if (p_lhs->node_kind_ != p_rhs->node_kind_)
{
is_equal = false;
}
else
{
switch (p_lhs->node_kind_)
{
case path_node_kind::root:
case path_node_kind::name:
is_equal = p_lhs->name_ == p_rhs->name_;
break;
case path_node_kind::index:
is_equal = p_lhs->index_ == p_rhs->index_;
break;
default:
break;
}
}
p_lhs = p_lhs->parent_;
p_rhs = p_rhs->parent_;
}
return is_equal;
}
};
template <typename Json>
Json* select(Json& root, const basic_path_node<typename Json::char_type>& path)
{
using path_node_type = basic_path_node<typename Json::char_type>;
std::vector<const path_node_type*> nodes(path.size(), nullptr);
std::size_t len = nodes.size();
const path_node_type* p = std::addressof(path);
while (p != nullptr)
{
nodes[--len] = p;
p = p->parent();
}
Json* current = std::addressof(root);
for (auto node : nodes)
{
if (node->node_kind() == path_node_kind::index)
{
if (current->type() != json_type::array_value || node->index() >= current->size())
{
return nullptr;
}
current = std::addressof(current->at(node->index()));
}
else if (node->node_kind() == path_node_kind::name)
{
if (current->type() != json_type::object_value)
{
return nullptr;
}
auto it = current->find(node->name());
if (it == current->object_range().end())
{
return nullptr;
}
current = std::addressof((*it).value());
}
}
return current;
}
template <typename CharT,typename 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> buffer(alloc);
using path_node_type = basic_path_node<CharT>;
std::vector<const path_node_type*> nodes(path.size(), nullptr);
std::size_t len = nodes.size();
const path_node_type* p = std::addressof(path);
while (p != nullptr)
{
nodes[--len] = p;
p = p->parent();
}
for (auto node : nodes)
{
switch (node->node_kind())
{
case path_node_kind::root:
buffer.push_back('$');
break;
case path_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 path_node_kind::index:
buffer.push_back('[');
jsoncons::detail::from_integer(node->index(), buffer);
buffer.push_back(']');
break;
}
}
return buffer;
}
using path_node = basic_path_node<char>;
using wpath_node = basic_path_node<wchar_t>;
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
#endif // JSONCONS_EXT_JSONPATH_PATH_NODE_HPP
|