One of the sequence and deserialization of a custom object using JSON in QT

Source: Internet
Author: User

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.