File indexing completed on 2025-01-19 03:55:38

0001 #include "o0jsonresponse.h"
0002 
0003 #include <QByteArray>
0004 #include <QDebug>
0005 #if QT_VERSION >= 0x050000
0006 #include <QJsonDocument>
0007 #include <QJsonObject>
0008 #else
0009 #include <QScriptEngine>
0010 #include <QScriptValueIterator>
0011 #endif
0012 
0013 QVariantMap parseJsonResponse(const QByteArray &data) {
0014 #if QT_VERSION >= 0x050000
0015     QJsonParseError err;
0016     QJsonDocument doc = QJsonDocument::fromJson(data, &err);
0017     if (err.error != QJsonParseError::NoError) {
0018         qWarning() << "parseTokenResponse: Failed to parse token response due to err:" << err.errorString();
0019         return QVariantMap();
0020     }
0021 
0022     if (!doc.isObject()) {
0023         qWarning() << "parseTokenResponse: Token response is not an object";
0024         return QVariantMap();
0025     }
0026 
0027     return doc.object().toVariantMap();
0028 #else
0029     QScriptEngine engine;
0030     QScriptValue value = engine.evaluate("(" + QString(data) + ")");
0031     QScriptValueIterator it(value);
0032     QVariantMap map;
0033 
0034     while (it.hasNext()) {
0035         it.next();
0036         map.insert(it.name(), it.value().toVariant());
0037     }
0038 
0039     return map;
0040 #endif
0041 }