Merge pull request #226 from Laupetin/fix/t6-material-json-errors

fix: not properly handling errors when loading json
This commit is contained in:
Jan 2024-08-08 19:43:25 +02:00 committed by GitHub
commit e2464410b2
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
9 changed files with 81 additions and 15 deletions

View File

@ -18,7 +18,7 @@ namespace nlohmann
template<class T> void optional_from_json(const nlohmann::json& j, const char* name, std::optional<T>& value)
{
const auto it = j.find(name);
if (it != j.end())
if (it != j.end() && !it->is_null())
value = it->get<T>();
else
value = std::nullopt;

View File

@ -36,9 +36,18 @@ namespace
return false;
}
try
{
const auto jLeaderboard = jRoot.get<JsonLeaderboardDef>();
return CreateLeaderboardFromJson(jLeaderboard, leaderboardDef);
}
catch (const json::exception& e)
{
std::cerr << std::format("Failed to parse json of leaderboard: {}\n", e.what());
}
return false;
}
private:
bool CreateColumnDefFromJson(const JsonColumnDef& jColumn, LbColumnDef& lbColumnDef, LeaderboardDef& leaderboardDef) const

View File

@ -36,9 +36,18 @@ namespace
return false;
}
try
{
const auto jLeaderboard = jRoot.get<JsonLeaderboardDef>();
return CreateLeaderboardFromJson(jLeaderboard, leaderboardDef);
}
catch (const json::exception& e)
{
std::cerr << std::format("Failed to parse json of leaderboard: {}\n", e.what());
}
return false;
}
private:
static bool CreateTrackTypeFlagsFromJson(const JsonLeaderboardDef& jLeaderboardDef, int& trackTypeFlags)

View File

@ -44,9 +44,18 @@ namespace
return false;
}
try
{
const auto jAttachment = jRoot.get<JsonWeaponAttachment>();
return CreateWeaponAttachmentFromJson(jAttachment, attachment);
}
catch (const json::exception& e)
{
std::cerr << std::format("Failed to parse json of attachment: {}\n", e.what());
}
return false;
}
private:
static void PrintError(const WeaponAttachment& attachment, const std::string& message)

View File

@ -37,9 +37,18 @@ namespace
return false;
}
try
{
const auto jLeaderboard = jRoot.get<JsonLeaderboardDef>();
return CreateLeaderboardFromJson(jLeaderboard, leaderboardDef);
}
catch (const json::exception& e)
{
std::cerr << std::format("Failed to parse json of leaderboard: {}\n", e.what());
}
return false;
}
private:
static bool CreateTrackTypeFlagsFromJson(const JsonLeaderboardDef& jLeaderboardDef, int& trackTypeFlags)

View File

@ -39,9 +39,18 @@ namespace
return false;
}
try
{
const auto jMaterial = jRoot.get<JsonMaterial>();
return CreateMaterialFromJson(jMaterial, material);
}
catch (const json::exception& e)
{
std::cerr << std::format("Failed to parse json of material: {}\n", e.what());
}
return false;
}
private:
static void PrintError(const Material& material, const std::string& message)

View File

@ -39,9 +39,18 @@ namespace
return false;
}
try
{
const auto jWeaponCamo = jRoot.get<JsonWeaponCamo>();
return CreateWeaponCamoFromJson(jWeaponCamo, weaponCamo);
}
catch (const json::exception& e)
{
std::cerr << std::format("Failed to parse json of weapon camo: {}\n", e.what());
}
return false;
}
private:
static void PrintError(const WeaponCamo& weaponCamo, const std::string& message)

View File

@ -40,9 +40,18 @@ namespace
return false;
}
try
{
const auto jXModel = jRoot.get<JsonXModel>();
return CreateXModelFromJson(jXModel, xmodel);
}
catch (const json::exception& e)
{
std::cerr << std::format("Failed to parse json of xmodel: {}\n", e.what());
}
return false;
}
private:
static void PrintError(const XModel& xmodel, const std::string& message)

3
thirdparty/json.lua vendored
View File

@ -2,6 +2,9 @@ json = {}
function json:include(includes)
if includes:handle(self:name()) then
defines {
"JSON_DIAGNOSTICS=1"
}
includedirs {
path.join(ThirdPartyFolder(), "json", "single_include")
}