144 lines
4.7 KiB
C++
144 lines
4.7 KiB
C++
#include "jsonparser.h"
|
|
|
|
#include <QJsonArray>
|
|
#include <QJsonObject>
|
|
|
|
#include "../model/metadata.h"
|
|
|
|
QList<ModelItemValues> JsonParser::toItemValuesList(const QByteArray& jsonData,
|
|
const QString& rootValueName) {
|
|
QList<ModelItemValues> result;
|
|
|
|
if (jsonData.isEmpty()) {
|
|
return result;
|
|
}
|
|
// TODO tidy up the following code and encapsulate into functions;
|
|
|
|
QJsonDocument doc = QJsonDocument::fromJson(jsonData);
|
|
|
|
/// case one: json value name in plural -> should contain an array of items
|
|
if (rootValueName == ITEMS_KEY_STRING) {
|
|
QJsonArray itemArray = extractItemArray(doc, rootValueName);
|
|
|
|
foreach (QJsonValue value, itemArray) {
|
|
QJsonObject itemJsonObject = value.toObject();
|
|
ModelItemValues values = jsonObjectToItemValues(itemJsonObject);
|
|
result.append(values);
|
|
}
|
|
}
|
|
/// case two: json value name in singular -> should contain an object of one item
|
|
if (rootValueName == ITEM_KEY_STRING) {
|
|
QJsonObject rootObject = doc.object();
|
|
QJsonObject itemJsonObject = rootObject.value(rootValueName).toObject();
|
|
ModelItemValues values = jsonObjectToItemValues(itemJsonObject);
|
|
result.append(values);
|
|
}
|
|
|
|
return result;
|
|
}
|
|
|
|
QByteArray JsonParser::itemValuesListToJson(const QList<ModelItemValues>& itemValuesList,
|
|
const QString& objectName) {
|
|
QJsonDocument jsonDoc;
|
|
QJsonObject rootObject;
|
|
QJsonArray itemArray;
|
|
for (const ModelItemValues& itemValues : itemValuesList) {
|
|
QJsonObject itemObject = itemValuesToJsonObject(itemValues);
|
|
itemArray.append(itemObject);
|
|
}
|
|
|
|
rootObject.insert(objectName, itemArray);
|
|
jsonDoc.setObject(rootObject);
|
|
|
|
return jsonDoc.toJson(QJsonDocument::Compact);
|
|
}
|
|
|
|
QJsonObject JsonParser::itemValuesToJsonObject(const ModelItemValues& itemValues) {
|
|
QJsonObject result;
|
|
|
|
// TODO add dates (entry, modification, end)
|
|
const UserRoles idRole = IdRole;
|
|
const QJsonValue idValue = extractJsonValue(itemValues, idRole);
|
|
if (!idValue.isNull()) {
|
|
const QString idRoleName = ROLE_NAMES.value(idRole);
|
|
result.insert(idRoleName, idValue);
|
|
}
|
|
|
|
QListIterator<UserRoles> i(USER_FACING_ROLES);
|
|
while (i.hasNext()) {
|
|
const UserRoles role = i.next();
|
|
const QJsonValue jsonValue = extractJsonValue(itemValues, role);
|
|
const QString roleName = ROLE_NAMES.value(role);
|
|
result.insert(roleName, jsonValue);
|
|
}
|
|
return result;
|
|
}
|
|
|
|
JsonParser::JsonParser() {}
|
|
|
|
QJsonArray JsonParser::extractItemArray(const QJsonDocument& doc, const QString& objectName) {
|
|
QJsonArray itemArray;
|
|
if (objectName.isEmpty()) {
|
|
itemArray = doc.array();
|
|
} else {
|
|
QJsonObject rootObject = doc.object();
|
|
itemArray = rootObject.value(objectName).toArray();
|
|
}
|
|
return itemArray;
|
|
}
|
|
|
|
ModelItemValues JsonParser::jsonObjectToItemValues(const QJsonObject& itemJsonObject) {
|
|
ModelItemValues values;
|
|
|
|
const UserRoles idRole = IdRole;
|
|
const QString idRoleName = ROLE_NAMES.value(idRole);
|
|
// QVariant idValue = data(idRole);
|
|
if (itemJsonObject.contains(idRoleName)) {
|
|
std::pair<int, QVariant> keyValuePair = getKeyValuePair(itemJsonObject, idRole);
|
|
values.insert(keyValuePair.first, keyValuePair.second);
|
|
}
|
|
|
|
QListIterator<UserRoles> i(USER_FACING_ROLES);
|
|
while (i.hasNext()) {
|
|
const UserRoles role = i.next();
|
|
std::pair<int, QVariant> keyValuePair = getKeyValuePair(itemJsonObject, role);
|
|
values.insert(keyValuePair.first, keyValuePair.second);
|
|
}
|
|
return values;
|
|
}
|
|
|
|
pair<int, QVariant> JsonParser::getKeyValuePair(const QJsonObject& itemJsonObject, const int role) {
|
|
QVariant result;
|
|
const QJsonValue jsonValue = itemJsonObject[ROLE_NAMES.value(role)];
|
|
if (STRING_ROLES.contains(role)) {
|
|
result = jsonValue.toString();
|
|
} else if (INT_ROLES.contains(role)) {
|
|
result = jsonValue.toInt();
|
|
} else if (DOUBLE_ROLES.contains(role)) {
|
|
result = jsonValue.toDouble();
|
|
} else if (TYPE_ROLES.contains(role)) {
|
|
result = jsonValue.toString();
|
|
} else {
|
|
qCritical() << QString("Cant find data type of role %1!!!").arg(role);
|
|
}
|
|
return pair<int, QVariant>(role, result);
|
|
}
|
|
|
|
QJsonValue JsonParser::extractJsonValue(const ModelItemValues& itemValues, const int role) {
|
|
QJsonValue result;
|
|
const QString roleName = ROLE_NAMES.value(role);
|
|
const QVariant value = itemValues.value(role);
|
|
if (STRING_ROLES.contains(role)) {
|
|
result = value.toString();
|
|
} else if (INT_ROLES.contains(role)) {
|
|
result = value.toInt();
|
|
} else if (DOUBLE_ROLES.contains(role)) {
|
|
result = value.toDouble();
|
|
} else if (TYPE_ROLES.contains(role)) {
|
|
result = value.toString();
|
|
} else {
|
|
qCritical() << QString("Can't find data type for role %1!!!").arg(role);
|
|
}
|
|
return result;
|
|
}
|