The case of not using polymorphism is relatively simple and the complete code is as follows:
#include <QCoreApplication>#include<memory>#include<vector>#include<string>#include<iostream>#include<algorithm>#include<QFile>#include<QJsonDocument>#include<QJsonObject>#include<QJsonArray>classdata{ Public: Data (void) {} Data (ConstStd::wstring &name): Dataname (name) {}Virtual~Data () {} std::wstring GetName (void){ returnDataname; } Virtual voidWrite (Qjsonobject &JSON) {json["Name"] =qstring::fromstdwstring (Dataname.c_str ()); } Virtual voidReadConstQjsonobject &JSON) {Dataname= json["Name"].tostring (). tostdwstring (); } Virtual voidPrint (Std::wostream & out){ out<< dataname.c_str () <<Std::endl; }Private: Std::wstring dataname;};classDataanalog: Publicdata{ Public: Dataanalog (void) {} dataanalog (ConstStd::wstring &name,Const Doublevalue):D ATA (name), DataValue (value) {}Virtual~Dataanalog () {}Virtual voidWrite (Qjsonobject &json)Override{data::write (JSON); json["Value"] =DataValue; } Virtual voidReadConstQjsonobject &json)Override{data::read (JSON); DataValue= json["Value"].todouble (); } Virtual voidPrint (Std::wostream & out){ out<< getName (). C_STR () << L"="<< DataValue <<Std::endl; }Private: DoubleDataValue =0;};classDatadigital: Publicdata{ Public: Datadigital (void) {} datadigital (ConstStd::wstring &name,Const BOOLvalue):D ATA (name), DataValue (value) {}Virtual~datadigital () {}Virtual voidWrite (Qjsonobject &json)Override{data::write (JSON); json["Value"] =DataValue; } Virtual voidReadConstQjsonobject &json)Override{data::read (JSON); DataValue= json["Value"].tobool (); } Virtual voidPrint (Std::wostream & out)Override{ out<< getName (). C_STR () << L"="<< (DataValue? L"true": L"false") <<Std::endl; }Private: BOOLDataValue =false;};intMainintargcChar*argv[]) {Qcoreapplication A (argc, argv); {//Save std::vector<std::shared_ptr<Data>>analogs; Analogs.emplace_back (NewDataanalog (L"anlog_1",1.1)); Analogs.emplace_back (NewDataanalog (L"anlog_2",2.2)); Std::vector<std::shared_ptr<Data>>digitals; Digitals.emplace_back (NewDatadigital (L"digita_1",true)); Digitals.emplace_back (NewDatadigital (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 ()); }} {//load 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 (NewDataanalog ()); Analogs.back ()-Read (I.toobject ()); } std::vector<std::shared_ptr<Data>>digitals; Qjsonarray Digitalarray= json["digitals"].toarray (); for(Auto I:digitalarray) {digitals.emplace_back (Newdatadigital ()); 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);}); } } } returna.exec ();}
One of the sequence and deserialization of a custom object using JSON in QT