QJson home page
Static Public Member Functions
QJson::QObjectHelper Class Reference

Class used to convert QObject into QVariant and vivce-versa. During these operations only the class attributes defined as properties will be considered. Properties marked as 'non-stored' will be ignored. More...

#include <qobjecthelper.h>

List of all members.

Static Public Member Functions

static QVariantMap qobject2qvariant (const QObject *object, const QStringList &ignoredProperties=QStringList(QString(QLatin1String("objectName"))))
static void qvariant2qobject (const QVariantMap &variant, QObject *object)

Detailed Description

Class used to convert QObject into QVariant and vivce-versa. During these operations only the class attributes defined as properties will be considered. Properties marked as 'non-stored' will be ignored.

Suppose the declaration of the Person class looks like this:

class Person : public QObject
{
Q_OBJECT
Q_PROPERTY(QString name READ name WRITE setName)
Q_PROPERTY(int phoneNumber READ phoneNumber WRITE setPhoneNumber)
Q_PROPERTY(Gender gender READ gender WRITE setGender)
Q_PROPERTY(QDate dob READ dob WRITE setDob)
Q_ENUMS(Gender)
public:
Person(QObject* parent = 0);
~Person();
QString name() const;
void setName(const QString& name);
int phoneNumber() const;
void setPhoneNumber(const int phoneNumber);
enum Gender {Male, Female};
void setGender(Gender gender);
Gender gender() const;
QDate dob() const;
void setDob(const QDate& dob);
private:
QString m_name;
int m_phoneNumber;
Gender m_gender;
QDate m_dob;
};

The following code will serialize an instance of Person to JSON :

Person person;
person.setName("Flavio");
person.setPhoneNumber(123456);
person.setGender(Person::Male);
person.setDob(QDate(1982, 7, 12));
QVariantMap variant = QObjectHelper::qobject2qvariant(&person);
Serializer serializer;
qDebug() << serializer.serialize( variant);

The generated output will be:

{ "dob" : "1982-07-12", "gender" : 0, "name" : "Flavio", "phoneNumber" : 123456 }

It's also possible to initialize a QObject using the values stored inside of a QVariantMap.

Suppose you have the following JSON data stored into a QString:

{ "dob" : "1982-07-12", "gender" : 0, "name" : "Flavio", "phoneNumber" : 123456 }

The following code will initialize an already allocated instance of Person using the JSON values:

Parser parser;
QVariant variant = parser.parse(json);
Person person;
QObjectHelper::qvariant2qobject(variant.toMap(), &person);
See also:
Parser
Serializer

Definition at line 116 of file qobjecthelper.h.


Member Function Documentation

QVariantMap QObjectHelper::qobject2qvariant ( const QObject *  object,
const QStringList &  ignoredProperties = QStringList(QString(QLatin1String("objectName"))) 
)
static

This method converts a QObject instance into a QVariantMap.

Parameters:
objectThe QObject instance to be converted.
ignoredPropertiesProperties that won't be converted.

Definition at line 44 of file qobjecthelper.cpp.

void QObjectHelper::qvariant2qobject ( const QVariantMap &  variant,
QObject *  object 
)
static

This method converts a QVariantMap instance into a QObject

Parameters:
variantAttributes to assign to the object.
objectThe QObject instance to update.

Definition at line 63 of file qobjecthelper.cpp.


The documentation for this class was generated from the following files:

SourceForge Logo hosts this site. Send comments to:
QJson Developers