/
waterstone
/
json_inspector
Обзор
Документация
Войти
/
waterstone
/
json_inspector
Код
Запросы
0
Пакеты
0
Релизы
0
Аналитика
Безопасность
master
sax_reader.cpp
120 строк
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 "sax_reader.h" bool SAXReaderHandler::Start(JSONNode::Type type) { if (m_controller.IsStopped()) return false; m_controller.UpdateProgress(); if (!m_root_node) { m_root_node = std::make_shared<JSONNode>(JSONNodeWP(), m_current_key, type, ++m_id_counter); m_current_node = m_root_node; return static_cast<bool>(m_root_node); } auto current_node_ptr = m_current_node.lock(); if (!current_node_ptr) return false; if (m_current_key.empty() && JSONNode::Type::Object == current_node_ptr->GetType()) return false; auto child_node_ptr = current_node_ptr->GetChild(m_current_key, type).lock(); if (!child_node_ptr) { child_node_ptr = current_node_ptr->AddChild(std::move(m_current_key), type, ++m_id_counter).lock(); if (!child_node_ptr) return false; } else m_current_key.clear(); m_current_node = child_node_ptr; return true; } bool SAXReaderHandler::End(JSONNode::Type type) { if (m_controller.IsStopped()) return false; m_controller.UpdateProgress(); if (!m_root_node) return false; auto current_node_ptr = m_current_node.lock(); if (!current_node_ptr) return false; if (type != current_node_ptr->GetType()) return false; m_current_node = current_node_ptr->GetParent(); return true; } bool SAXReaderHandler::Key(const BaseReaderHandler::Ch* str, rapidjson::SizeType length, bool /*copy*/) { if (m_controller.IsStopped()) return false; m_controller.UpdateProgress(); if (!m_root_node) return false; m_current_key = std::string(str, length); return true; } bool SAXReaderHandler::AddValue(JSONNode::Variant&& var_val) { constexpr JSONNode::Type kType = JSONNode::Type::Value; if (m_controller.IsStopped()) return false; m_controller.UpdateProgress(); if (!m_root_node) return false; auto current_node_ptr = m_current_node.lock(); if (!current_node_ptr) return false; if (JSONNode::Type::Object != current_node_ptr->GetType() && JSONNode::Type::Array != current_node_ptr->GetType()) { return false; } if (m_current_key.empty() && JSONNode::Type::Object == current_node_ptr->GetType()) return false; if (m_current_key == "$id") { m_current_key.clear(); return true; } auto child_node_ptr = current_node_ptr->GetChild(m_current_key, kType).lock(); if (!child_node_ptr) { child_node_ptr = current_node_ptr->AddChild(std::move(m_current_key), kType, ++m_id_counter).lock(); if (!child_node_ptr) return false; } else m_current_key.clear(); child_node_ptr->AddValue(std::move(var_val)); return true; }