Cleaner qobject2qvariant().

This commit is contained in:
Francois Ferrand 2013-04-11 20:42:00 +02:00
parent ea992bc3e6
commit eef51f26f0
3 changed files with 25 additions and 27 deletions

View File

@ -105,25 +105,6 @@ static QString encrypt(const QString & data, SymmetricCipher & cipher)
return encode64(encrypt2(data.toUtf8(), cipher)); return encode64(encrypt2(data.toUtf8(), cipher));
} }
static QVariant qobject2qvariant(const QObject * object, const QStringList ignoredProperties = QStringList(QString(QLatin1String("objectName"))))
{
QVariantMap result;
const QMetaObject *metaobject = object->metaObject();
int count = metaobject->propertyCount();
for (int i=0; i<count; ++i) {
QMetaProperty metaproperty = metaobject->property(i);
const char *name = metaproperty.name();
if (ignoredProperties.contains(QLatin1String(name)) || (!metaproperty.isReadable()))
continue;
QVariant value = object->property(name);
if (!value.isNull() /*&& value.isValid()*/) //Do not add NULL or invalid fields
result[QLatin1String(name)] = value;
}
return result;
}
//////////////////////////////////////////////////////////////////////////////////////////////////// ////////////////////////////////////////////////////////////////////////////////////////////////////
/// Request /// Request
@ -315,7 +296,7 @@ void Response::setVerifier(QString key)
QString Response::toJson() QString Response::toJson()
{ {
QVariant result = qobject2qvariant(this); QVariant result = QJson::QObjectHelper::qobject2qvariant(this, QJson::QObjectHelper::Flag_None);
QJson::Serializer s; QJson::Serializer s;
s.setIndentMode(QJson::IndentCompact); s.setIndentMode(QJson::IndentCompact);
@ -360,7 +341,7 @@ QVariant Response::getEntries() const
QList<QVariant> res; QList<QVariant> res;
res.reserve(m_entries.size()); res.reserve(m_entries.size());
Q_FOREACH(const Entry &entry, m_entries) Q_FOREACH(const Entry &entry, m_entries)
res.append(qobject2qvariant(&entry)); res.append(QJson::QObjectHelper::qobject2qvariant(&entry, QJson::QObjectHelper::Flag_None));
return res; return res;
} }
@ -427,8 +408,6 @@ void Response::setError(const QString &error)
/// ResponseEntry /// ResponseEntry
//////////////////////////////////////////////////////////////////////////////////////////////////// ////////////////////////////////////////////////////////////////////////////////////////////////////
Q_DECLARE_METATYPE(Entry)
Entry::Entry() Entry::Entry()
{} {}

View File

@ -41,7 +41,7 @@ QObjectHelper::~QObjectHelper()
delete d; delete d;
} }
QVariantMap QObjectHelper::qobject2qvariant( const QObject* object, QVariantMap QObjectHelper::qobject2qvariant(const QObject* object, Flags flags,
const QStringList& ignoredProperties) const QStringList& ignoredProperties)
{ {
QVariantMap result; QVariantMap result;
@ -55,11 +55,20 @@ QVariantMap QObjectHelper::qobject2qvariant( const QObject* object,
continue; continue;
QVariant value = object->property(name); QVariant value = object->property(name);
if (value.isNull() && !flags.testFlag(Flag_StoreNullVariants))
continue;
if (!value.isValid() && !flags.testFlag(Flag_StoreInvalidVariants))
continue;
result[QLatin1String(name)] = value; result[QLatin1String(name)] = value;
} }
return result; return result;
} }
QVariantMap QObjectHelper::qobject2qvariant(const QObject *object, const QStringList &ignoredProperties)
{
return qobject2qvariant(object, Flag_All, ignoredProperties);
}
void QObjectHelper::qvariant2qobject(const QVariantMap& variant, QObject* object) void QObjectHelper::qvariant2qobject(const QVariantMap& variant, QObject* object)
{ {
const QMetaObject *metaobject = object->metaObject(); const QMetaObject *metaobject = object->metaObject();

View File

@ -120,14 +120,24 @@ namespace QJson {
QObjectHelper(); QObjectHelper();
~QObjectHelper(); ~QObjectHelper();
enum Flag {
Flag_None,
Flag_StoreNullVariants,
Flag_StoreInvalidVariants,
Flag_All = Flag_StoreNullVariants | Flag_StoreInvalidVariants
};
Q_DECLARE_FLAGS(Flags, Flag)
/** /**
* This method converts a QObject instance into a QVariantMap. * This method converts a QObject instance into a QVariantMap.
* *
* @param object The QObject instance to be converted. * @param object The QObject instance to be converted.
* @param ignoredProperties Properties that won't be converted. * @param ignoredProperties Properties that won't be converted.
*/ */
static QVariantMap qobject2qvariant( const QObject* object, static QVariantMap qobject2qvariant(const QObject* object, Flags flags = Flag_All,
const QStringList& ignoredProperties = QStringList(QString(QLatin1String("objectName")))); const QStringList& ignoredProperties = QStringList(QString(QLatin1String("objectName"))));
static QVariantMap qobject2qvariant(const QObject* object,
const QStringList& ignoredProperties);
/** /**
* This method converts a QVariantMap instance into a QObject * This method converts a QVariantMap instance into a QObject