Search code examples
c++jsonqtqjson

How to create a QJsonDocument?


I would like to create a QJsonDocument but I can't find a way to push my data in the document. At the end of JsonBuilder::CreateContact my document seem to be empty. Some help would be great please X).

I've tried to use some pointer and add them to my doc because I was thinking of the destruction of my different object (contactObj, detailObj, addressObj, date) but with this method I can't build the type QJsonValue (can't access private member of QjsonValue::QJsonValue) so am I wrong ?

JsonBuilder.h

#ifndef JSONBUILDER_H
#define JSONBUILDER_H
#include <QObject>
#include <QJsonDocument>
#include <QMap>
#include <QString>
#include <QList>

class JsonBuilder: public QObject
{
    Q_OBJECT
private:
    QJsonDocument* document;
public:
    JsonBuilder();
    ~JsonBuilder();
    void CreateContact(QMap<QString,QList<QString>> map);
    void CreateMessage(QMap<QString,QString> map);

    QJsonDocument* getDocument(){ return document; }
};

#endif // JSONBUILDER_H

JsonBuilder.cpp

#include "Jsonbuilder.h"
#include <QPair>
#include <QJsonObject>
#include <QJsonValue>
#include <Global.h>
#include <QDebug>
#include <QMapIterator>
#include <QJsonArray>

JsonBuilder::JsonBuilder(){
    document = new QJsonDocument(QJsonArray());
}

void JsonBuilder::CreateContact(QMap<QString, QList<QString>> map){
    QJsonObject contactObj;
    QJsonObject detailObj;
    QJsonObject addressObj;
    QJsonObject date;

    contactObj.insert(Global::KEY_TYPE_OBJ,QJsonValue("contact"));

    QMapIterator<QString, QList<QString>> it(map);

    while (it.hasNext()) {

        it.next();
        qDebug()<< "Key value = "+it.key();
        if(it.key() == Global::KEY_FIRST_NAME){
            contactObj.insert(Global::KEY_FIRST_NAME,QJsonValue(it.value()[0]));
            qDebug()<< "create firstName in Json " + it.value()[0];
        }

        if(it.key() == Global::KEY_LAST_NAME){
            contactObj.insert(Global::KEY_LAST_NAME,QJsonValue(it.value()[0]));
            qDebug()<< "create LastName in Json "+ it.value()[0];
        }

        if(it.key() == Global::KEY_PSEUDO){
            QJsonArray Pseudos;
            foreach (QString contact, it.value()) {
                Pseudos.append(QJsonValue(contact));
                qDebug()<< "create Pseudo in Json "+ contact;
            }
            contactObj.insert(Global::KEY_PSEUDO,QJsonValue(Pseudos));
        }

        //Detail Object
        if(it.key() == Global::KEY_DETAIL_ROLE){
            detailObj.insert(Global::KEY_DETAIL_ROLE,QJsonValue(it.value()[0]));
            qDebug()<< "create DetailRole in Json "+ it.value()[0];
        }

        if(it.key() == Global::KEY_DETAIL_ORGANIZATION){
            detailObj.insert(Global::KEY_DETAIL_ORGANIZATION,QJsonValue(it.value()[0]));
            qDebug()<< "create DetailOrganization in Json "+ it.value()[0];
        }

        if(it.key() == Global::KEY_DETAIL_DEPARTMENT){
            detailObj.insert(Global::KEY_DETAIL_DEPARTMENT,QJsonValue(it.value()[0]));
            qDebug()<< "create DetailDepartment in Json "+ it.value()[0];
        }

        if(it.key() == Global::KEY_DETAIL_FUNCTION){
            detailObj.insert(Global::KEY_DETAIL_FUNCTION,QJsonValue(it.value()[0]));
            qDebug()<< "create DetailFunction in Json "+ it.value()[0];
        }

        if(it.key() == Global::KEY_PHONE){
            QJsonArray phones;
            foreach (QString phone, it.value()) {
                phones.append(QJsonValue(phone));
                qDebug()<< "create Phones in Json "+phone;
            }
            contactObj.insert(Global::KEY_PHONE,QJsonValue(phones));
        }

        if(it.key() == Global::KEY_EMAIL){
            QJsonArray emails;
            foreach (QString email, it.value()) {
                emails.append(QJsonValue(email));
                qDebug()<< "create Emails in Json "+email;
            }
            contactObj.insert(Global::KEY_EMAIL,QJsonValue(emails));

        }

        //Dates Object
        if(it.key() == Global::KEY_BIRTHDAY){
            date.insert(Global::KEY_BIRTHDAY,QJsonValue(it.value()[0]));
            qDebug()<< "create Birthday in Json "+it.value()[0];
        }

        if(it.key() == Global::KEY_WEDDING){
            foreach (QString dateWedding, it.value()) {
                date.insert(Global::KEY_WEDDING, QJsonValue(dateWedding));
                qDebug()<< "create wedding in Json "+dateWedding;
            }
        }

        //Address Object
        if(it.key() == Global::KEY_STREET){
            addressObj.insert(Global::KEY_STREET,QJsonValue(it.value()[0]));
            qDebug()<< "create street in Json "+it.value()[0];
        }

        if(it.key() == Global::KEY_POSTAL_CODE){
            addressObj.insert(Global::KEY_POSTAL_CODE,QJsonValue(it.value()[0]));
            qDebug()<< "create PostalCode in Json "+it.value()[0];
        }

        if(it.key() == Global::KEY_CITY){
            addressObj.insert(Global::KEY_CITY, QJsonValue(it.value()[0]));
            qDebug()<< "create city in Json "+it.value()[0];
        }

        if(it.key() == Global::KEY_COUNTRY){
            addressObj.insert(Global::KEY_COUNTRY, QJsonValue(it.value()[0]));
            qDebug()<< "create Country in Json "+it.value()[0];
        }
    }

    contactObj.insert(Global::KEY_DETAIL,QJsonValue(detailObj));
    contactObj.insert(Global::KEY_ADDRESS_OBJ,QJsonValue(addressObj));
    contactObj.insert(Global::KEY_DATE,QJsonValue(date));

    if(document->isArray()){
        document->array().append(contactObj);
    }
    else {
        qDebug()<<"JsonDocument not ready for insert";
    }

    qDebug()<< "Doc in function create : ";
    qDebug()<< *document;
}

void JsonBuilder::CreateMessage(QMap<QString, QString> map){

}

JsonBuilder::~JsonBuilder(){
    delete document;
}

Solution

  • Notice (in documentation) the signature of the array() method:

    QJsonArray QJsonDocument::array() const
    

    When you use it this way:

    document->array().append(contactObj);
    

    you're appending an object to a temporary copy of the doc array.

    You'd be better create an array, append your objects, then set the array to the document, this way:

    QJsonArray array;
    array.append(contactObj);
    //append every else object you need
    document->setArray(array);
    

    instead of

    if(document->isArray()){
        document->array().append(contactObj);
    }
    else {
        qDebug()<<"JsonDocument not ready for insert";
    }