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