In the case of polymorphic use, the type information of the object is saved by serializing the pointer list of the base class. Constructs the correct subclass object through type information when deserializing. The type information in the code is obtained using C + + typeID to ensure portability.
The full 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); {std::vector<std::shared_ptr<Data>>datas; Datas.emplace_back (NewDataanalog (L"anlog_1",1.1)); Datas.emplace_back (NewDataanalog (L"anlog_2",2.2)); Datas.emplace_back (NewDatadigital (L"digita_1",true)); Datas.emplace_back (NewDatadigital (L"digita_2",false)); QFile file (Qcoreapplication::applicationdirpath ()+"\\data.json"); if(File.Open (qfile::writeonly)) {Qjsonarray dataarray; for(Auto Data:datas) {Qjsonobject obj; obj["Class"] = typeid (*data). Name (); Qjsonobject Dataobj; Data-write (dataobj); obj["Data"] =Dataobj; Dataarray.append (obj); } Qjsonobject JSON; json["Datas"] =DataArray; 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>>datas; Qjsonarray DataArray= json["Datas"].toarray (); for(Auto I:dataarray) {Qjsonobject obj=I.toobject (); QString ClassName= obj["Class"].tostring (); if(classname==typeid (dataanalog). Name ()) {Datas.emplace_back (NewDataanalog ()); } Else if(ClassName = =typeid (datadigital). Name ()) {Datas.emplace_back (Newdatadigital ()); } datas.back ()->read (obj["Data"].toobject ()); } Std::for_each (Datas.begin (), Datas.end (), [] (Std::shared_ptr<Data> Data) {data->print (std::wcout);}); } } } returna.exec ();}
Using JSON to sequence and deserialize custom objects in QT two