/
waterstone
/
json_inspector
Обзор
Документация
Войти
/
waterstone
/
json_inspector
Код
Запросы
0
Пакеты
0
Релизы
0
Аналитика
Безопасность
master
json_node.cpp
117 строк
3 KB
Peter Sakhno
Switch from DOM to SAX. Use QPromise for thread control and progrees indication.
18 ноя 2025, 23:54
18 ноя 2025, 23:54
aae5d62
Код
Авторство
О чём код?
#include "json_node.h" JSONNode::JSONNode(JSONNodeWP parent, std::string tag, Type type, ID id) : m_parent(parent), m_tag(std::move(tag)), m_type(type) { m_id = (id << 2) + static_cast<uint8_t>(type); } const JSONNode::SortedVariants& JSONNode::GetValues() const { return m_values; } void JSONNode::AddValue(JSONNode::Variant val) { auto res = m_values.insert(std::move(val)); } std::optional<JSONNodeWPC> JSONNode::FindNode(ID id) const { if (m_id == id) return weak_from_this(); const Type type = static_cast<decltype(type)>(id & 3); if (Type::Undefined == type) return {}; if (Type::Value == type) { auto value_cit = std::find_if(m_value_nodes.cbegin(), m_value_nodes.cend(), [id](const auto& i) { return i->GetId() == id; }); if (value_cit != m_value_nodes.cend()) return *value_cit; } auto child_cit = std::find_if(m_child_nodes.cbegin(), m_child_nodes.cend(), [id](const auto& i) { return i->GetId() == id; }); if (child_cit != m_child_nodes.cend()) return *child_cit; for (const auto& child : m_child_nodes) { if (auto child_child = child->FindNode(id)) return child_child; } return {}; } JSONNodeWP JSONNode::GetChildAt(int row) const { if (0 <= row && row < m_value_nodes.size()) return m_value_nodes.at(row); row = row - static_cast<int>(m_value_nodes.size()); if (0 <= row && row < m_child_nodes.size()) return m_child_nodes.at(row); return {}; } JSONNodeWP JSONNode::GetChild(const std::string& tag, Type type) const { if (Type::Undefined == type) return {}; if (Type::Value == type) { auto cit = std::find_if(m_value_nodes.cbegin(), m_value_nodes.cend(), [&tag](const auto& i) { return (i->GetTag() == tag); }); return (cit != m_value_nodes.cend()) ? *cit : JSONNodeWP(); } auto cit = std::find_if(m_child_nodes.cbegin(), m_child_nodes.cend(), [&tag](const auto& i) { return (i->GetTag() == tag); }); return (cit != m_child_nodes.cend()) ? *cit : JSONNodeWP(); } JSONNodeWP JSONNode::AddChild(std::string tag, Type type, ID id) { if (Type::Undefined == type) return {}; auto node = std::make_shared<JSONNode>(weak_from_this(), tag, type, id); auto ret = node; if (node->GetType() == Type::Value) m_value_nodes.push_back(std::move(node)); else m_child_nodes.push_back(std::move(node)); return ret; } int JSONNode::Row() { JSONNodeSP parent_node = m_parent.lock(); if (!parent_node) return 0; return parent_node->IndexOf(shared_from_this()); } int JSONNode::IndexOf(const JSONNodeSP& node) const { constexpr int kNoIndex = -1; if (!node) return kNoIndex; const auto type = node->GetType(); if (Type::Undefined == type) return kNoIndex; if (Type::Value == node->GetType()) { auto cit = std::find(m_value_nodes.cbegin(), m_value_nodes.cend(), node); if (cit != m_value_nodes.cend()) return static_cast<int>(std::distance(m_value_nodes.cbegin(), cit)); return kNoIndex; } auto cit = std::find(m_child_nodes.cbegin(), m_child_nodes.cend(), node); if (cit != m_child_nodes.cend()) return static_cast<int>(m_value_nodes.size() + std::distance(m_child_nodes.cbegin(), cit)); return kNoIndex; }