NOISSUE use Json parsing helpers to process technic search results

This commit is contained in:
Petr Mrázek 2020-10-13 22:18:42 +02:00
parent 4689571c24
commit a7216ecca1

View File

@ -16,6 +16,7 @@
#include "TechnicModel.h" #include "TechnicModel.h"
#include "Env.h" #include "Env.h"
#include "MultiMC.h" #include "MultiMC.h"
#include "Json.h"
#include <QIcon> #include <QIcon>
@ -117,26 +118,34 @@ void Technic::ListModel::searchRequestFinished()
} }
QList<Modpack> newList; QList<Modpack> newList;
auto objs = doc["modpacks"].toArray(); try {
for (auto technicPack: objs) { auto root = Json::requireObject(doc);
Modpack pack; auto objs = Json::requireArray(root, "modpacks");
auto technicPackObject = technicPack.toObject(); for (auto technicPack: objs) {
pack.name = technicPackObject["name"].toString(); Modpack pack;
pack.slug = technicPackObject["slug"].toString(); auto technicPackObject = Json::requireObject(technicPack);
if (pack.slug == "vanilla") pack.name = Json::requireString(technicPackObject, "name");
continue; pack.slug = Json::requireString(technicPackObject, "slug");
if (technicPackObject["iconUrl"].isString()) if (pack.slug == "vanilla")
{ continue;
pack.logoUrl = technicPackObject["iconUrl"].toString();
pack.logoName = pack.logoUrl.section(QLatin1Char('/'), -1).section(QLatin1Char('.'), 0, 0); auto rawURL = Json::ensureString(technicPackObject, "iconUrl", "null");
if(rawURL == "null") {
pack.logoUrl = "null";
pack.logoName = "null";
}
else {
pack.logoUrl = rawURL;
pack.logoName = rawURL.section(QLatin1Char('/'), -1).section(QLatin1Char('.'), 0, 0);
}
pack.broken = false;
newList.append(pack);
} }
else }
{ catch (const JSONValidationError &err)
pack.logoUrl = "null"; {
pack.logoName = "null"; qCritical() << "Couldn't parse technic search results:" << err.cause() ;
} return;
pack.broken = false;
newList.append(pack);
} }
searchState = Finished; searchState = Finished;
beginInsertRows(QModelIndex(), modpacks.size(), modpacks.size() + newList.size() - 1); beginInsertRows(QModelIndex(), modpacks.size(), modpacks.size() + newList.size() - 1);