Minor clean up in JsonParser.
This commit is contained in:
@ -12,13 +12,13 @@ QList<ModelItemValues> JsonParser::toItemValuesList(const QByteArray& jsonData,
|
||||
if (jsonData.isEmpty()) {
|
||||
return result;
|
||||
}
|
||||
// TODO tidy up the following code and encapsulate into functions;
|
||||
|
||||
// NEXT tidy up the following code and encapsulate into functions;
|
||||
QJsonDocument doc = QJsonDocument::fromJson(jsonData);
|
||||
|
||||
/// check if there is an array or there is only one object inside the root object;
|
||||
// TODO ? rename objectName into jsonValueName?
|
||||
/// case one: json value name in plural -> should contain an array of items
|
||||
if (rootValueName == ITEMS_KEY_STRING) {
|
||||
QJsonArray itemArray = extractItemArray(jsonData, rootValueName);
|
||||
QJsonArray itemArray = extractItemArray(doc, rootValueName);
|
||||
|
||||
foreach (QJsonValue value, itemArray) {
|
||||
QJsonObject itemJsonObject = value.toObject();
|
||||
@ -26,9 +26,8 @@ QList<ModelItemValues> JsonParser::toItemValuesList(const QByteArray& jsonData,
|
||||
result.append(values);
|
||||
}
|
||||
}
|
||||
/// case two: json value name in singular -> should contain an object of one item
|
||||
if (rootValueName == ITEM_KEY_STRING) {
|
||||
// QJsonArray itemArray = extractItemArray(jsonData, objectName);
|
||||
QJsonDocument doc = QJsonDocument::fromJson(jsonData);
|
||||
QJsonObject rootObject = doc.object();
|
||||
QJsonObject itemJsonObject = rootObject.value(rootValueName).toObject();
|
||||
ModelItemValues values = jsonObjectToItemValues(itemJsonObject);
|
||||
@ -73,17 +72,14 @@ QByteArray JsonParser::itemValuesListToJson(const QList<ModelItemValues>& itemVa
|
||||
|
||||
JsonParser::JsonParser() {}
|
||||
|
||||
QJsonArray JsonParser::extractItemArray(const QByteArray& jsonData, const QString& objectName) {
|
||||
QJsonDocument doc = QJsonDocument::fromJson(jsonData);
|
||||
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;
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user