Remove ensure JSON helpers

Signed-off-by: TheKodeToad <TheKodeToad@proton.me>
This commit is contained in:
TheKodeToad
2025-11-09 18:30:07 +00:00
parent 7e8cf628e8
commit e42c9d2a1d
38 changed files with 228 additions and 239 deletions

View File

@@ -180,7 +180,7 @@ bool processMCMeta(DataPack* pack, QByteArray&& raw_data)
auto json_doc = QJsonDocument::fromJson(raw_data);
auto pack_obj = Json::requireObject(json_doc.object(), "pack", {});
pack->setPackFormat(Json::ensureInteger(pack_obj, "pack_format", 0));
pack->setPackFormat(pack_obj["pack_format"].toInt(0));
pack->setDescription(DataPackUtils::processComponent(pack_obj.value("description")));
} catch (Json::JsonException& e) {
qWarning() << "JsonException: " << e.what() << e.cause();
@@ -192,19 +192,19 @@ bool processMCMeta(DataPack* pack, QByteArray&& raw_data)
QString buildStyle(const QJsonObject& obj)
{
QStringList styles;
if (auto color = Json::ensureString(obj, "color"); !color.isEmpty()) {
if (auto color = obj["color"].toString(); !color.isEmpty()) {
styles << QString("color: %1;").arg(color);
}
if (obj.contains("bold")) {
QString weight = "normal";
if (Json::ensureBoolean(obj, "bold", false)) {
if (obj["bold"].toBool()) {
weight = "bold";
}
styles << QString("font-weight: %1;").arg(weight);
}
if (obj.contains("italic")) {
QString style = "normal";
if (Json::ensureBoolean(obj, "italic", false)) {
if (obj["italic"].toBool()) {
style = "italic";
}
styles << QString("font-style: %1;").arg(style);
@@ -223,10 +223,10 @@ QString processComponent(const QJsonArray& value, bool strikethrough, bool under
QString processComponent(const QJsonObject& obj, bool strikethrough, bool underline)
{
underline = Json::ensureBoolean(obj, "underlined", underline);
strikethrough = Json::ensureBoolean(obj, "strikethrough", strikethrough);
underline = obj["underlined"].toBool(underline);
strikethrough = obj["strikethrough"].toBool(strikethrough);
QString result = Json::ensureString(obj, "text");
QString result = obj["text"].toString();
if (underline) {
result = QString("<u>%1</u>").arg(result);
}
@@ -234,14 +234,14 @@ QString processComponent(const QJsonObject& obj, bool strikethrough, bool underl
result = QString("<s>%1</s>").arg(result);
}
// the extra needs to be a array
result += processComponent(Json::ensureArray(obj, "extra"), strikethrough, underline);
result += processComponent(obj["extra"].toArray(), strikethrough, underline);
if (auto style = buildStyle(obj); !style.isEmpty()) {
result = QString("<span %1>%2</span>").arg(style, result);
}
if (obj.contains("clickEvent")) {
auto click_event = Json::ensureObject(obj, "clickEvent");
auto action = Json::ensureString(click_event, "action");
auto value = Json::ensureString(click_event, "value");
auto click_event = obj["clickEvent"].toObject();
auto action = click_event["action"].toString();
auto value = click_event["value"].toString();
if (action == "open_url" && !value.isEmpty()) {
result = QString("<a href=\"%1\">%2</a>").arg(value, result);
}
@@ -366,4 +366,4 @@ void LocalDataPackParseTask::executeTask()
}
emitSucceeded();
}
}

View File

@@ -75,11 +75,11 @@ ModDetails ReadMCModInfo(QByteArray contents)
val = jsonDoc.object().value("modListVersion");
}
int version = Json::ensureInteger(val, -1);
int version = val.toInt(-1);
// Some mods set the number with "", so it's a String instead
if (version < 0)
version = Json::ensureString(val, "").toInt();
version = val.toString("").toInt();
if (version != 2) {
qWarning() << QString(R"(The value of 'modListVersion' is "%1" (expected "2")! The file may be corrupted.)").arg(version);
@@ -298,26 +298,26 @@ ModDetails ReadQuiltModInfo(QByteArray contents)
QJsonParseError jsonError;
QJsonDocument jsonDoc = QJsonDocument::fromJson(contents, &jsonError);
auto object = Json::requireObject(jsonDoc, "quilt.mod.json");
auto schemaVersion = Json::ensureInteger(object.value("schema_version"), 0, "Quilt schema_version");
auto schemaVersion = object.value("schema_version").toInt();
// https://github.com/QuiltMC/rfcs/blob/be6ba280d785395fefa90a43db48e5bfc1d15eb4/specification/0002-quilt.mod.json.md
if (schemaVersion == 1) {
auto modInfo = Json::requireObject(object.value("quilt_loader"), "Quilt mod info");
auto modInfo = object.value("quilt_loader").toObject();
details.mod_id = Json::requireString(modInfo.value("id"), "Mod ID");
details.version = Json::requireString(modInfo.value("version"), "Mod version");
details.mod_id = modInfo.value("id").toString();
details.version = modInfo.value("version").toString();
auto modMetadata = Json::ensureObject(modInfo.value("metadata"));
auto modMetadata = modInfo.value("metadata").toObject();
details.name = Json::ensureString(modMetadata.value("name"), details.mod_id);
details.description = Json::ensureString(modMetadata.value("description"));
details.name = modMetadata.value("name").toString(details.mod_id);
details.description = modMetadata.value("description").toString();
auto modContributors = Json::ensureObject(modMetadata.value("contributors"));
auto modContributors = modMetadata.value("contributors").toObject();
// We don't really care about the role of a contributor here
details.authors += modContributors.keys();
auto modContact = Json::ensureObject(modMetadata.value("contact"));
auto modContact = modMetadata.value("contact").toObject();
if (modContact.contains("homepage")) {
details.homeurl = Json::requireString(modContact.value("homepage"));