|
25 | 25 | #include "metaenumvariable_p.h"
|
26 | 26 |
|
27 | 27 | #include <QtCore/QDebug>
|
| 28 | +#include <QJsonValue> |
| 29 | +#include <QJsonArray> |
| 30 | +#include <QJsonObject> |
| 31 | +#include <QJsonDocument> |
28 | 32 |
|
29 | 33 | using namespace Cutelee;
|
30 | 34 |
|
@@ -106,12 +110,94 @@ static QVariant doQobjectLookUp(const QObject *const object,
|
106 | 110 | return object->property(property.toUtf8().constData());
|
107 | 111 | }
|
108 | 112 |
|
| 113 | +static QVariant doJsonArrayLookUp(const QJsonArray &list, |
| 114 | + const QString &property) |
| 115 | +{ |
| 116 | + if (property == QLatin1String("count") || property == QLatin1String("size")) { |
| 117 | + return list.size(); |
| 118 | + } |
| 119 | + |
| 120 | + bool ok = false; |
| 121 | + const int listIndex = property.toInt(&ok); |
| 122 | + if (!ok || listIndex >= list.size()) { |
| 123 | + return QVariant(); |
| 124 | + } |
| 125 | + |
| 126 | + return list.at(listIndex).toVariant(); |
| 127 | +} |
| 128 | + |
| 129 | +static QVariant doJsonObjectLookUp(const QJsonObject &obj, |
| 130 | + const QString &property) |
| 131 | +{ |
| 132 | + if (property == QLatin1String("count") || property == QLatin1String("size")) { |
| 133 | + return obj.size(); |
| 134 | + } |
| 135 | + |
| 136 | + if (property == QLatin1String("items")) { |
| 137 | + QVariantList list; |
| 138 | + list.reserve(obj.size()); |
| 139 | + for (auto it = obj.constBegin(); it != obj.constEnd(); ++it) { |
| 140 | + list.push_back(QVariantList{it.key(), it.value().toVariant()}); |
| 141 | + } |
| 142 | + return list; |
| 143 | + } |
| 144 | + |
| 145 | + if (property == QLatin1String("keys")) { |
| 146 | + return obj.keys(); |
| 147 | + } |
| 148 | + |
| 149 | + if (property == QLatin1String("values")) { |
| 150 | + QVariantList list; |
| 151 | + list.reserve(obj.size()); |
| 152 | + for (auto it = obj.constBegin(); it != obj.constEnd(); ++it) { |
| 153 | + list.push_back(it.value().toVariant()); |
| 154 | + } |
| 155 | + return list; |
| 156 | + } |
| 157 | + |
| 158 | + return obj.value(property).toVariant(); |
| 159 | +} |
| 160 | + |
109 | 161 | QVariant Cutelee::MetaType::lookup(const QVariant &object,
|
110 |
| - const QString &property) |
| 162 | + const QString &property) |
111 | 163 | {
|
112 | 164 | if (object.canConvert<QObject *>()) {
|
113 | 165 | return doQobjectLookUp(object.value<QObject *>(), property);
|
114 | 166 | }
|
| 167 | + if (object.userType() == QMetaType::QJsonDocument) { |
| 168 | + const auto doc = object.toJsonDocument(); |
| 169 | + if (doc.isObject()) { |
| 170 | + return doJsonObjectLookUp(doc.object(), property); |
| 171 | + } |
| 172 | + if (doc.isArray()) { |
| 173 | + return doJsonArrayLookUp(doc.array(), property); |
| 174 | + } |
| 175 | + return QVariant(); |
| 176 | + } |
| 177 | + if (object.userType() == QMetaType::QJsonValue) { |
| 178 | + const auto val = object.toJsonValue(); |
| 179 | + |
| 180 | + switch (val.type()) { |
| 181 | + case QJsonValue::Bool: |
| 182 | + return val.toBool(); |
| 183 | + case QJsonValue::Double: |
| 184 | + return val.toDouble(); |
| 185 | + case QJsonValue::String: |
| 186 | + return val.toString(); |
| 187 | + case QJsonValue::Array: |
| 188 | + return doJsonArrayLookUp(val.toArray(), property); |
| 189 | + case QJsonValue::Object: |
| 190 | + return doJsonObjectLookUp(val.toObject(), property); |
| 191 | + default: |
| 192 | + return QVariant(); |
| 193 | + } |
| 194 | + } |
| 195 | + if (object.userType() == QMetaType::QJsonArray) { |
| 196 | + return doJsonArrayLookUp(object.toJsonArray(), property); |
| 197 | + } |
| 198 | + if (object.userType() == QMetaType::QJsonObject) { |
| 199 | + return doJsonObjectLookUp(object.toJsonObject(), property); |
| 200 | + } |
115 | 201 | if (object.canConvert<QVariantList>()) {
|
116 | 202 | auto iter = object.value<QSequentialIterable>();
|
117 | 203 | if (property == QStringLiteral("size")
|
|
0 commit comments