mirror of
https://github.com/WerWolv/ImHex.git
synced 2026-03-28 07:47:03 -05:00
impr: Better JSON error handling in many places
This commit is contained in:
@@ -27,10 +27,10 @@ namespace hex::plugin::builtin {
|
||||
auto json = nlohmann::json::parse(data.begin(), data.end());
|
||||
|
||||
const auto &endpoints = ContentRegistry::CommunicationInterface::impl::getNetworkEndpoints();
|
||||
if (auto callback = endpoints.find(json["endpoint"].get<std::string>()); callback != endpoints.end()) {
|
||||
log::info("Network endpoint {} called with arguments '{}'", json["endpoint"].get<std::string>(), json.contains("data") ? json["data"].dump() : "");
|
||||
if (auto callback = endpoints.find(json.at("endpoint").get<std::string>()); callback != endpoints.end()) {
|
||||
log::info("Network endpoint {} called with arguments '{}'", json.at("endpoint").get<std::string>(), json.contains("data") ? json.at("data").dump() : "");
|
||||
|
||||
auto responseData = callback->second(json.contains("data") ? json["data"] : nlohmann::json::object());
|
||||
auto responseData = callback->second(json.contains("data") ? json.at("data") : nlohmann::json::object());
|
||||
|
||||
result["status"] = "success";
|
||||
result["data"] = responseData;
|
||||
|
||||
@@ -59,8 +59,8 @@ namespace hex::plugin::builtin {
|
||||
}
|
||||
|
||||
void load(const nlohmann::json &j) override {
|
||||
this->m_size = j["size"];
|
||||
this->m_buffer = j["data"].get<std::vector<u8>>();
|
||||
this->m_size = j.at("size");
|
||||
this->m_buffer = j.at("data").get<std::vector<u8>>();
|
||||
}
|
||||
|
||||
private:
|
||||
@@ -89,7 +89,7 @@ namespace hex::plugin::builtin {
|
||||
}
|
||||
|
||||
void load(const nlohmann::json &j) override {
|
||||
this->m_value = j["data"].get<std::string>();
|
||||
this->m_value = j.at("data").get<std::string>();
|
||||
}
|
||||
|
||||
private:
|
||||
@@ -117,7 +117,7 @@ namespace hex::plugin::builtin {
|
||||
}
|
||||
|
||||
void load(const nlohmann::json &j) override {
|
||||
this->m_value = j["data"];
|
||||
this->m_value = j.at("data");
|
||||
}
|
||||
|
||||
private:
|
||||
@@ -145,7 +145,7 @@ namespace hex::plugin::builtin {
|
||||
}
|
||||
|
||||
void load(const nlohmann::json &j) override {
|
||||
this->m_value = j["data"];
|
||||
this->m_value = j.at("data");
|
||||
}
|
||||
|
||||
private:
|
||||
@@ -184,7 +184,8 @@ namespace hex::plugin::builtin {
|
||||
}
|
||||
|
||||
void load(const nlohmann::json &j) override {
|
||||
this->m_color = ImVec4(j["data"]["r"], j["data"]["g"], j["data"]["b"], j["data"]["a"]);
|
||||
const auto &color = j.at("data");
|
||||
this->m_color = ImVec4(color.at("r"), color.at("g"), color.at("b"), color.at("a"));
|
||||
}
|
||||
|
||||
private:
|
||||
|
||||
@@ -147,11 +147,11 @@ namespace hex::plugin::builtin {
|
||||
|
||||
void load(const nlohmann::json &json) override {
|
||||
try {
|
||||
this->m_polynomial = json["polynomial"];
|
||||
this->m_initialValue = json["initialValue"];
|
||||
this->m_xorOut = json["xorOut"];
|
||||
this->m_reflectIn = json["reflectIn"];
|
||||
this->m_reflectOut = json["reflectOut"];
|
||||
this->m_polynomial = json.at("polynomial");
|
||||
this->m_initialValue = json.at("initialValue");
|
||||
this->m_xorOut = json.at("xorOut");
|
||||
this->m_reflectIn = json.at("reflectIn");
|
||||
this->m_reflectOut = json.at("reflectOut");
|
||||
} catch (std::exception&) { }
|
||||
}
|
||||
|
||||
|
||||
@@ -35,13 +35,13 @@ namespace hex::plugin::builtin {
|
||||
.required = true,
|
||||
.load = [](const std::fs::path &basePath, Tar &tar) {
|
||||
auto json = nlohmann::json::parse(tar.readString(basePath / "providers.json"));
|
||||
auto providerIds = json["providers"].get<std::vector<int>>();
|
||||
auto providerIds = json.at("providers").get<std::vector<int>>();
|
||||
|
||||
bool success = true;
|
||||
for (const auto &id : providerIds) {
|
||||
auto providerSettings = nlohmann::json::parse(tar.readString(basePath / hex::format("{}.json", id)));
|
||||
|
||||
auto provider = ImHexApi::Provider::createProvider(providerSettings["type"].get<std::string>(), true);
|
||||
auto provider = ImHexApi::Provider::createProvider(providerSettings.at("type").get<std::string>(), true);
|
||||
ON_SCOPE_EXIT {
|
||||
if (!success) {
|
||||
for (auto &task : TaskManager::getRunningTasks())
|
||||
@@ -60,7 +60,7 @@ namespace hex::plugin::builtin {
|
||||
}
|
||||
|
||||
provider->setID(id);
|
||||
provider->loadSettings(providerSettings["settings"]);
|
||||
provider->loadSettings(providerSettings.at("settings"));
|
||||
if (!provider->open() || !provider->isAvailable() || !provider->isReadable())
|
||||
success = false;
|
||||
else
|
||||
|
||||
@@ -359,7 +359,7 @@ namespace hex::plugin::builtin {
|
||||
void DiskProvider::loadSettings(const nlohmann::json &settings) {
|
||||
Provider::loadSettings(settings);
|
||||
|
||||
auto path = settings["path"].get<std::string>();
|
||||
auto path = settings.at("path").get<std::string>();
|
||||
this->setPath(std::u8string(path.begin(), path.end()));
|
||||
this->reloadDrives();
|
||||
}
|
||||
|
||||
@@ -217,7 +217,7 @@ namespace hex::plugin::builtin {
|
||||
void FileProvider::loadSettings(const nlohmann::json &settings) {
|
||||
Provider::loadSettings(settings);
|
||||
|
||||
auto pathString = settings["path"].get<std::string>();
|
||||
auto pathString = settings.at("path").get<std::string>();
|
||||
std::fs::path path = std::u8string(pathString.begin(), pathString.end());
|
||||
|
||||
if (auto projectPath = ProjectFile::getPath(); !projectPath.empty())
|
||||
|
||||
@@ -325,9 +325,9 @@ namespace hex::plugin::builtin {
|
||||
void GDBProvider::loadSettings(const nlohmann::json &settings) {
|
||||
Provider::loadSettings(settings);
|
||||
|
||||
this->m_ipAddress = settings["ip"].get<std::string>();
|
||||
this->m_port = settings["port"].get<int>();
|
||||
this->m_size = settings["size"].get<size_t>();
|
||||
this->m_ipAddress = settings.at("ip").get<std::string>();
|
||||
this->m_port = settings.at("port").get<int>();
|
||||
this->m_size = settings.at("size").get<size_t>();
|
||||
}
|
||||
|
||||
nlohmann::json GDBProvider::storeSettings(nlohmann::json settings) const {
|
||||
|
||||
@@ -269,7 +269,7 @@ namespace hex::plugin::builtin {
|
||||
void IntelHexProvider::loadSettings(const nlohmann::json &settings) {
|
||||
Provider::loadSettings(settings);
|
||||
|
||||
auto path = settings["path"].get<std::string>();
|
||||
auto path = settings.at("path").get<std::string>();
|
||||
this->m_sourceFilePath = std::u8string(path.begin(), path.end());
|
||||
}
|
||||
|
||||
|
||||
@@ -643,8 +643,8 @@ namespace hex::plugin::builtin {
|
||||
auto json = nlohmann::json::parse(response.getData());
|
||||
links.push_back({
|
||||
wolv::util::toUTF8String(currFile.filename()),
|
||||
json["data"]["file"]["url"]["short"],
|
||||
json["data"]["file"]["metadata"]["size"]["readable"]
|
||||
json.at("data").at("file").at("url").at("short"),
|
||||
json.at("data").at("file").at("metadata").at("size").at("readable")
|
||||
});
|
||||
} catch (...) {
|
||||
PopupError::open("hex.builtin.tools.file_uploader.invalid_response"_lang);
|
||||
@@ -705,8 +705,8 @@ namespace hex::plugin::builtin {
|
||||
|
||||
auto json = nlohmann::json::parse(response.getData());
|
||||
|
||||
resultTitle = json["query"]["pages"][0]["title"].get<std::string>();
|
||||
resultExtract = json["query"]["pages"][0]["extract"].get<std::string>();
|
||||
resultTitle = json.at("query").at("pages").at(0).at("title").get<std::string>();
|
||||
resultExtract = json.at("query").at("pages").at(0).at("extract").get<std::string>();
|
||||
|
||||
if (!extendedSearch && resultExtract.ends_with(':')) {
|
||||
extendedSearch = true;
|
||||
|
||||
@@ -34,15 +34,15 @@ namespace hex::plugin::builtin {
|
||||
auto fileData = wolv::io::File(file.path(), wolv::io::File::Mode::Read).readString();
|
||||
auto content = nlohmann::json::parse(fileData);
|
||||
|
||||
for (auto value : content["values"]) {
|
||||
for (auto value : content.at("values")) {
|
||||
Constant constant;
|
||||
constant.category = content["name"].get<std::string>();
|
||||
constant.name = value["name"].get<std::string>();
|
||||
constant.category = content.at("name").get<std::string>();
|
||||
constant.name = value.at("name").get<std::string>();
|
||||
if (value.contains("desc"))
|
||||
constant.description = value["desc"].get<std::string>();
|
||||
constant.value = value["value"].get<std::string>();
|
||||
constant.description = value.at("desc").get<std::string>();
|
||||
constant.value = value.at("value").get<std::string>();
|
||||
|
||||
auto type = value["type"];
|
||||
auto type = value.at("type");
|
||||
if (type == "int10")
|
||||
constant.type = ConstantType::Int10;
|
||||
else if (type == "int16be")
|
||||
|
||||
@@ -64,8 +64,8 @@ namespace hex::plugin::builtin {
|
||||
}
|
||||
|
||||
void load(const nlohmann::json &j) override {
|
||||
this->m_name = j["name"].get<std::string>();
|
||||
this->m_type = j["type"];
|
||||
this->m_name = j.at("name").get<std::string>();
|
||||
this->m_type = j.at("type");
|
||||
|
||||
this->setUnlocalizedTitle(this->m_name);
|
||||
this->setAttributes({
|
||||
@@ -128,8 +128,8 @@ namespace hex::plugin::builtin {
|
||||
}
|
||||
|
||||
void load(const nlohmann::json &j) override {
|
||||
this->m_name = j["name"].get<std::string>();
|
||||
this->m_type = j["type"];
|
||||
this->m_name = j.at("name").get<std::string>();
|
||||
this->m_type = j.at("type");
|
||||
|
||||
this->setUnlocalizedTitle(this->m_name);
|
||||
this->setAttributes({
|
||||
@@ -265,7 +265,7 @@ namespace hex::plugin::builtin {
|
||||
}
|
||||
|
||||
void load(const nlohmann::json &j) override {
|
||||
this->m_dataProcessor->loadNodes(this->m_workspace, j["nodes"]);
|
||||
this->m_dataProcessor->loadNodes(this->m_workspace, j.at("nodes"));
|
||||
|
||||
this->m_name = LangEntry(this->getUnlocalizedTitle()).get();
|
||||
this->m_requiresAttributeUpdate = true;
|
||||
@@ -494,7 +494,7 @@ namespace hex::plugin::builtin {
|
||||
try {
|
||||
nlohmann::json nodeJson = nlohmann::json::parse(wolv::io::File(entry.path(), wolv::io::File::Mode::Read).readString());
|
||||
|
||||
this->m_customNodes.push_back(CustomNode { LangEntry(nodeJson["name"]), nodeJson });
|
||||
this->m_customNodes.push_back(CustomNode { LangEntry(nodeJson.at("name")), nodeJson });
|
||||
} catch (nlohmann::json::exception &e) {
|
||||
continue;
|
||||
}
|
||||
@@ -906,7 +906,7 @@ namespace hex::plugin::builtin {
|
||||
|
||||
std::unique_ptr<dp::Node> newNode;
|
||||
for (auto &entry : nodeEntries) {
|
||||
if (entry.name == node["type"].get<std::string>())
|
||||
if (node.contains("name") && entry.name == node["type"].get<std::string>())
|
||||
newNode = entry.creatorFunction();
|
||||
}
|
||||
|
||||
|
||||
@@ -211,7 +211,7 @@ namespace hex::plugin::builtin {
|
||||
const auto &hashes = ContentRegistry::Hashes::impl::getHashes();
|
||||
|
||||
for (const auto &hash : json["hashes"]) {
|
||||
if (!hash.contains("name") || !hash.contains("type"))
|
||||
if (!hash.contains("name") || !hash.contains("type") || !hash.contains("settings"))
|
||||
continue;
|
||||
|
||||
for (const auto &newHash : hashes) {
|
||||
|
||||
@@ -18,7 +18,7 @@ namespace hex::plugin::builtin {
|
||||
.required = false,
|
||||
.load = [](prv::Provider *provider, const std::fs::path &basePath, Tar &tar) {
|
||||
auto json = nlohmann::json::parse(tar.readString(basePath));
|
||||
provider->getPatches() = json["patches"].get<std::map<u64, u8>>();
|
||||
provider->getPatches() = json.at("patches").get<std::map<u64, u8>>();
|
||||
return true;
|
||||
},
|
||||
.store = [](prv::Provider *provider, const std::fs::path &basePath, Tar &tar) {
|
||||
|
||||
@@ -149,8 +149,8 @@ namespace hex::plugin::builtin {
|
||||
try {
|
||||
auto jsonData = nlohmann::json::parse(wolv::io::File(path, wolv::io::File::Mode::Read).readString());
|
||||
uniqueProviders.insert(RecentProvider {
|
||||
.displayName = jsonData["displayName"],
|
||||
.type = jsonData["type"],
|
||||
.displayName = jsonData.at("displayName"),
|
||||
.type = jsonData.at("type"),
|
||||
.filePath = path,
|
||||
.data = jsonData
|
||||
});
|
||||
@@ -592,7 +592,7 @@ namespace hex::plugin::builtin {
|
||||
auto days_since_epoch = std::chrono::duration_cast<std::chrono::days>(now.time_since_epoch());
|
||||
std::mt19937 random(days_since_epoch.count());
|
||||
|
||||
auto chosenCategory = tipsCategories[random()%tipsCategories.size()]["tips"];
|
||||
auto chosenCategory = tipsCategories[random()%tipsCategories.size()].at("tips");
|
||||
auto chosenTip = chosenCategory[random()%chosenCategory.size()];
|
||||
s_tipOfTheDay = chosenTip.get<std::string>();
|
||||
|
||||
|
||||
Reference in New Issue
Block a user