81 lines
2.0 KiB
C
81 lines
2.0 KiB
C
|
#ifndef SERIALIZATION_H
|
|||
|
#define SERIALIZATION_H
|
|||
|
|
|||
|
#include <QString>
|
|||
|
#include <QDebug>
|
|||
|
#include <QDateTime>
|
|||
|
#include <QMap>
|
|||
|
#include <iostream>
|
|||
|
|
|||
|
#include "DataManagerGlobal.h"
|
|||
|
#include "Util.h"
|
|||
|
#include "Error.h"
|
|||
|
|
|||
|
#include "pugixml.hpp"
|
|||
|
|
|||
|
#include "nlohmann/json.hpp"
|
|||
|
|
|||
|
// XML JSON序列化与反序列化类,抽象基类
|
|||
|
class Serialization
|
|||
|
{
|
|||
|
|
|||
|
public:
|
|||
|
//virtual int fromXml(const pugi::xml_node node) = 0;
|
|||
|
//virtual int toXml(pugi::xml_node node) = 0;
|
|||
|
|
|||
|
virtual int fromXml()
|
|||
|
{
|
|||
|
return 0;
|
|||
|
}
|
|||
|
virtual int toXml()
|
|||
|
{
|
|||
|
return 0;
|
|||
|
}
|
|||
|
|
|||
|
template <typename T>
|
|||
|
static void deserializeDics(T &dic, pugi::xml_node xDic)
|
|||
|
{
|
|||
|
dic.clear();
|
|||
|
if (xDic.empty())
|
|||
|
return;
|
|||
|
for (auto e : xDic.children("Item"))
|
|||
|
{
|
|||
|
|
|||
|
std::string key = e.attribute("Key").value();
|
|||
|
std::string keyType = e.attribute("KeyType").value();
|
|||
|
std::string value = e.attribute("Value").value();
|
|||
|
std::string valueType = e.attribute("ValueType").value();
|
|||
|
|
|||
|
if (keyType == "System.String" && valueType == "System.Int32")
|
|||
|
{
|
|||
|
std::string str = key;
|
|||
|
int i = std::stoi(value);
|
|||
|
dic.push_back({str, i});
|
|||
|
}
|
|||
|
}
|
|||
|
}
|
|||
|
|
|||
|
template <typename T>
|
|||
|
static void serializeDics(const T &dic, pugi::xml_node xDic)
|
|||
|
{
|
|||
|
for (const auto &pair : dic)
|
|||
|
{
|
|||
|
// 获取键值对
|
|||
|
const std::string &key = pair.first;
|
|||
|
int value = pair.second;
|
|||
|
|
|||
|
// 创建 Item 节点
|
|||
|
pugi::xml_node itemNode = xDic.append_child("Item");
|
|||
|
|
|||
|
// 设置属性(Key、KeyType、Value、ValueType)
|
|||
|
itemNode.append_attribute("Key") = key.c_str();
|
|||
|
itemNode.append_attribute("KeyType") = "System.String";
|
|||
|
itemNode.append_attribute("Value") = std::to_string(value).c_str();
|
|||
|
itemNode.append_attribute("ValueType") = "System.Int32";
|
|||
|
}
|
|||
|
}
|
|||
|
|
|||
|
private:
|
|||
|
};
|
|||
|
|
|||
|
#endif
|