From 6061b0145464d35a179d4df058fccfc2b571b6fd Mon Sep 17 00:00:00 2001 From: mzh Date: Tue, 5 Nov 2024 17:25:45 +0800 Subject: [PATCH] =?UTF-8?q?1=E3=80=81=E6=B7=BB=E5=8A=A0jsonAdd=E7=B1=BB?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../CFDStructDataManagerBase.cpp | 60 +++++++++++++++++++ .../CFDStructDataManagerBase.h | 5 ++ 2 files changed, 65 insertions(+) diff --git a/CFDStruct/CFDStructDataManager/CFDStructDataManagerBase.cpp b/CFDStruct/CFDStructDataManager/CFDStructDataManagerBase.cpp index ef78d7e..6383854 100644 --- a/CFDStruct/CFDStructDataManager/CFDStructDataManagerBase.cpp +++ b/CFDStruct/CFDStructDataManager/CFDStructDataManagerBase.cpp @@ -17,3 +17,63 @@ CUIConfig *CFDStructDataManagerBase::getParamUIConfig() { return nullptr; } + +// 写入json +void jsonAdd(rapidjson::Document& document, QVariant value, QVector path) { + using namespace rapidjson; + + // 获取分配器 + Document::AllocatorType& allocator = document.GetAllocator(); + + // 递归地创建路径中的对象 + Value* current = &document; + for (int i = 0; i < path.size(); ++i) { +// const std::string& key = path[i]; + QVariant key = path.at(i); + + // 如果到了最后一层路径,设置值 + if (i == path.size() - 1) { + + if(key.type() == QVariant::String) + { + //如果是路径 + // 检查路径末尾是否存在 + if (current->HasMember(key.toString().toStdString().c_str())) { + if(value.type() == QVariant::Int) + (*current)[key.toString().toStdString().c_str()].SetInt(value.toInt()); // 如果存在则替换 + if(value.type() == QVariant::Double) + (*current)[key.toString().toStdString().c_str()].SetDouble(value.toDouble()); // 如果存在则替换 + if(value.type() == QVariant::String) + (*current)[key.toString().toStdString().c_str()].SetString(value.toString().toStdString().c_str(),allocator); // 如果存在则替换 + } else { + + if(value.type() == QVariant::Int) + current->AddMember(Value().SetString(key.toString().toStdString().c_str(), allocator), Value().SetInt(value.toInt()), allocator); // 不存在则添加 + if(value.type() == QVariant::Double) + current->AddMember(Value().SetString(key.toString().toStdString().c_str(), allocator), Value().SetDouble(value.toDouble()), allocator); // 不存在则添加 + if(value.type() == QVariant::String) + current->AddMember(Value().SetString(key.toString().toStdString().c_str(), allocator), Value().SetString(value.toString().toStdString().c_str(),allocator), allocator); // 不存在则添加 + } + }else if(key.type() == QVariant::Int){ + //如果是数组idx + + } + } else { + if(key.type() == QVariant::String) + { + //如果是路径 + + // 检查是否存在该路径,不存在则创建 + if (!current->HasMember(key.toString().toStdString().c_str())) { + Value newObject(kObjectType); // 创建一个新的对象 + current->AddMember(Value().SetString(key.toString().toStdString().c_str(), allocator), newObject, allocator); + } + // 将 current 指向下一级 + current = &(*current)[key.toString().toStdString().c_str()]; + }else if(key.type() == QVariant::Int){ + //如果是数组idx + + } + } + } +} diff --git a/CFDStruct/CFDStructDataManager/CFDStructDataManagerBase.h b/CFDStruct/CFDStructDataManager/CFDStructDataManagerBase.h index 72a020d..16d9227 100644 --- a/CFDStruct/CFDStructDataManager/CFDStructDataManagerBase.h +++ b/CFDStruct/CFDStructDataManager/CFDStructDataManagerBase.h @@ -6,6 +6,10 @@ #include "CUIProperty/CUISigsCenter.h" #include "CUIProperty/CUIConfig.h" #include "CUIProperty/CUIDefine.h" +#include "rapidjson/document.h" +#include "rapidjson/writer.h" +#include "rapidjson/stringbuffer.h" + class CFDSTRUCTDATAMANAGER_EXPORT CFDStructDataManagerBase : public QObject { @@ -20,6 +24,7 @@ signals: protected: CUIConfig* m_uiConfig = nullptr; CUISigsCenter* m_cuiSigsCenter = nullptr; + void jsonAdd(rapidjson::Document& document, QVariant value, QVector path); }; #endif // CFDSTRUCTDATAMANAGERBASE_H