没有使用多态的情况比较简单,完整代码如下:
#include <QCoreApplication> #include <memory> #include <vector> #include <string> #include <iostream> #include <algorithm> #include <QFile> #include <QJsonDocument> #include <QJsonObject> #include <QJsonArray> class Data { public: Data(void){} Data(const std::wstring &name) : dataName(name){} virtual ~Data() {} std::wstring getName(void){ return dataName; } virtual void write(QJsonObject &json){ json["Name"] = QString::fromStdWString(dataName.c_str()); } virtual void read(const QJsonObject &json){ dataName = json["Name"].toString().toStdWString(); } virtual void print(std::wostream &out){ out << dataName.c_str() << std::endl; } private: std::wstring dataName; }; class DataAnalog : public Data { public: DataAnalog(void){} DataAnalog(const std::wstring &name, const double value):Data(name), dataValue(value) {} virtual ~DataAnalog(){} virtual void write(QJsonObject &json) override{ Data::write(json); json["Value"] = dataValue; } virtual void read(const QJsonObject &json) override{ Data::read(json); dataValue = json["Value"].toDouble(); } virtual void print(std::wostream &out){ out << getName().c_str() << L"=" << dataValue << std::endl; } private: double dataValue = 0; }; class DataDigital : public Data { public: DataDigital(void) {} DataDigital(const std::wstring &name, const bool value):Data(name), dataValue(value){} virtual ~DataDigital(){} virtual void write(QJsonObject &json) override{ Data::write(json); json["Value"] = dataValue; } virtual void read(const QJsonObject &json) override{ Data::read(json); dataValue = json["Value"].toBool(); } virtual void print(std::wostream &out) override{ out << getName().c_str() << L"=" << (dataValue?L"true":L"false") << std::endl; } private: bool dataValue = false; }; int main(int argc, char *argv[]) { QCoreApplication a(argc, argv); {//保存 std::vector<std::shared_ptr<Data>> analogs; analogs.emplace_back(new DataAnalog(L"Anlog_1", 1.1)); analogs.emplace_back(new DataAnalog(L"Anlog_2", 2.2)); std::vector<std::shared_ptr<Data>> digitals; digitals.emplace_back(new DataDigital(L"Digita_1", true)); digitals.emplace_back(new DataDigital(L"Digita_2", false)); QFile file(QCoreApplication::applicationDirPath() + "\\data.json"); if(file.open(QFile::WriteOnly)) { QJsonArray analogArray; for(auto data:analogs) { QJsonObject obj; data->write(obj); analogArray.append(obj); } QJsonArray digitalArray; for(auto data:digitals) { QJsonObject obj; data->write(obj); digitalArray.append(obj); } QJsonObject json; json["Analogs"] = analogArray; json["Digitals"] = digitalArray; QJsonDocument doc(json); file.write(doc.toJson()); } } {//加载 QFile file(QCoreApplication::applicationDirPath() + "\\data.json"); if(file.open(QFile::ReadOnly)) { QJsonDocument doc = QJsonDocument::fromJson(file.readAll()); if(!doc.isNull()) { QJsonObject json = doc.object(); std::vector<std::shared_ptr<Data>> analogs; QJsonArray analogArray = json["Analogs"].toArray(); for(auto i : analogArray) { analogs.emplace_back(new DataAnalog()); analogs.back()->read(i.toObject()); } std::vector<std::shared_ptr<Data>> digitals; QJsonArray digitalArray = json["Digitals"].toArray(); for(auto i : digitalArray) { digitals.emplace_back(new DataDigital()); digitals.back()->read((*i).toObject()); } std::for_each(analogs.begin(), analogs.end() , [](std::shared_ptr<Data> data){data->print(std::wcout);}); std::for_each(digitals.begin(), digitals.end() , [](std::shared_ptr<Data> data){data->print(std::wcout);}); } } } return a.exec(); }
时间: 2024-10-10 15:10:18