flavio / qjson

QJson is a qt-based library that maps JSON data to QVariant objects.
http://qjson.sourceforge.net
GNU Lesser General Public License v2.1
287 stars 140 forks source link

QJson and QProperty #5

Open Moth13 opened 11 years ago

Moth13 commented 11 years ago

Hi Flavio,

Thanks for your genius lib, great job, very powerful.

I'm trying to use your QJson library to parse content from D3 Api. I use QProperty to initialize my objects. It worked fine with standard value as int, QString, QDate, but how can use QList?

Example :

QJson from D3 Api :

{
"heroes" : [ {
"name" : "Toto",
"id" : 3
}, {
"name" : "Toto2",
"id" : 4
} ],
"lastHeroPlayed" : 3
}

My Class :

class D3_Heroe : public QObject
{
Q_Object
Q_PROPERTY(QString name READ getName WRITE setName)
Q_PROPERTY(int id READ getID WRITE setID)
...
}

class D3_Career : public QObject
{
Q_Object
Q_PROPERTY(QList<D3_Heroe> hereos READ getHeroes WRITE setHeroes)
Q_PROPERTY(QString lastHeroPlayed READ getlastHeroPlayed WRITE setlastHeroPlayed)
...
}

Using your QJson::QObjectHelper :

// _pReply is a QNetworkReply coming from D3_API
QVariant res = parser.parse( _pReply->readAll(), &bOk );
D3_Career pCareer = new D3_Career();
QJson::QObjectHelper::qvariant2qobject( res.toMap(), pCareer );

Any ideas?

Regards, Jérémie

Moth13 commented 11 years ago

Hi again, I resolve my issue by using QVariant instead of QList. So it use your QVariant::Type test in qvariant2qobject correctly. And in D3_Career, in WRITE function, I parse this QVariant, still using qvariant2qobject.

By curiosity, why qvariant2qobject function don't handle QVariant::UserType?

Regards, Jérémie

flavio commented 11 years ago

By curiosity, why qvariant2qobject function don't handle QVariant::UserType?

Good catch, I'll take care of that with the next release.