smurfomen / QSerializer

This repo for Qt/C++ serialization objects in JSON or XML based on QtCore
https://smurfomen.github.io/QSerializer/
MIT License
73 stars 27 forks source link

Problems while writing #13

Closed HaonPAPA closed 2 years ago

HaonPAPA commented 2 years ago
class cModelCommon : public cSerializer
{
    Q_GADGET
    QS_SERIALIZABLE

        QS_FIELD(QString, Name)
        QS_FIELD(QString, Memo)
        QS_FIELD(int, Time)
        QS_FIELD(int, Unit)
        QS_FIELD(QDateTime, DateTime)
        QS_COLLECTION(QVector, int, Wave_Length)
        QS_COLLECTION(QVector, int, Cell_type)
};

// [DeSerialize 과정]
    {
        QFile xml(strPath);
        if (!xml.exists())
            qWarning() << "ERROR: ModelCommon.xml is not exist";
        if (xml.open(QIODevice::ReadOnly))
        {
            // empty object
            cModelCommon src;
            qDebug() << "empty xml object:";
            //qDebug() << src.Serialize().constData();

            qDebug() << "contains in file: ModelCommon.xml";
            QByteArray d = xml.readAll();
            qDebug() << d << "\n";

            src.DeSerialize(d);
            qDebug() << "serialized json object from file: ModelCommon.xml";
            qDebug() << src.Serialize().constData();

            xml.close();
        }
    }

I even checked that I wrote about the newly defined variable and saved it as an xml file. In the process of loading, it was discovered that the data for String could not be retrieved normally. It seems to get the initial value through the process of GET in QS_XML_FIELD.

When I do xml.readAll();, I checked that the data was imported normally, but it seems to be missing in the process of writing. What is the problem?

=================================================================================

void cSerializer::fromXml(const QDomNode& val)
{
    QDomNode doc = val;

    const char* className = metaObject()->className();
    auto n = doc.firstChildElement(metaObject()->className());

    if (!n.isNull()) {
        for (int i = 0; i < metaObject()->propertyCount(); i++) 
        {
            QString name = metaObject()->property(i).name();
            QDomElement tmp = metaObject()->property(i).readOnGadget(this).value<QDomNode>().firstChildElement();

            QString str = tmp.tagName();

            //auto f = n.firstChildElement(tmp.tagName());
            **auto f = n.firstChildElement(name);**
            metaObject()->property(i).writeOnGadget(this, QVariant::fromValue<QDomNode>(f));
        }
    }
    else
    {
        for (int i = 0; i < metaObject()->propertyCount(); i++) 
        {
            QString name = metaObject()->property(i).name();
            auto f = doc.firstChildElement(name);
            metaObject()->property(i).writeOnGadget(this, QVariant::fromValue<QDomNode>(f));
        }
    }
}

Why is tmp.tagName() empty? There is an error in retrieving data because of an empty part.

I've modified the source code, but when I edit it like this, it works normally.

smurfomen commented 2 years ago

Thanks for this issue. I found a bug at deserialize macro QS_XML_FIELD and fix it. Problem was into return invalid QDomNode. Before this was returns QDomNode constructs from element, without doc who contains this element. It was usual mistype.

It should be work now. Try it and feedback me after that, thanks.

HaonPAPA commented 2 years ago

Thanks for this issue. I found a bug at deserialize macro QS_XML_FIELD and fix it. Problem was into return invalid QDomNode. Before this was returns QDomNode constructs from element, without doc who contains this element. It was usual mistype.

It should be work now. Try it and feedback me after that, thanks.

Did the modified code have been uploaded to git? Can you tell which parts have been modified?

smurfomen commented 2 years ago

Yes, last commit for master branch. In macro QS_XML_FIELD into GET section replased QDomNode(element) to QDomNode(doc).

HaonPAPA commented 2 years ago

Yes, last commit for master branch. In macro QS_XML_FIELD into GET section replased QDomNode(element) to QDomNode(doc).

Thank you for quick response. I checked that it works Thanks it worked out well.