1
0
mirror of https://github.com/OpenRCT2/OpenRCT2 synced 2026-01-16 19:43:06 +01:00

Json: Fix wrong implicit path typecasts in ReadFromFile and WriteToFile

These functions accepted fs::path which meant passing a u8string to them
wrongly assumed an ANSI encoding and not UTF-8. This would be
a non-issue in C++20 where char8_t is separate, but until then it was an issue
causing incorrect character conversions, and thus an exception.
This commit is contained in:
Silent
2022-02-12 14:13:47 +01:00
parent 46deac8620
commit 4621cc4264
3 changed files with 11 additions and 25 deletions

View File

@@ -283,7 +283,7 @@ void ShortcutManager::LoadLegacyBindings(const fs::path& path)
void ShortcutManager::LoadUserBindings(const fs::path& path)
{
auto root = Json::ReadFromFile(path);
auto root = Json::ReadFromFile(path.u8string());
if (root.is_object())
{
for (auto it = root.begin(); it != root.end(); ++it)
@@ -329,7 +329,7 @@ void ShortcutManager::SaveUserBindings(const fs::path& path)
json_t root;
if (fs::exists(path))
{
root = Json::ReadFromFile(path);
root = Json::ReadFromFile(path.u8string());
}
for (const auto& shortcut : Shortcuts)
@@ -349,7 +349,7 @@ void ShortcutManager::SaveUserBindings(const fs::path& path)
}
}
Json::WriteToFile(path, root);
Json::WriteToFile(path.u8string(), root);
}
std::string_view ShortcutManager::GetLegacyShortcutId(size_t index)

View File

@@ -15,7 +15,7 @@
namespace Json
{
json_t ReadFromFile(const utf8* path, size_t maxSize)
json_t ReadFromFile(u8string_view path, size_t maxSize)
{
auto fs = OpenRCT2::FileStream(path, OpenRCT2::FILE_MODE_OPEN);
@@ -36,19 +36,14 @@ namespace Json
}
catch (const json_t::exception& e)
{
throw JsonException(String::Format("Unable to parse JSON file (%s)\n\t%s", path, e.what()));
throw JsonException(String::StdFormat(
"Unable to parse JSON file (%.*s)\n\t%s", static_cast<int>(path.length()), path.data(), e.what()));
}
return json;
}
json_t ReadFromFile(const fs::path& path, size_t maxSize)
{
auto path8 = path.u8string();
return ReadFromFile(path8.c_str(), maxSize);
}
void WriteToFile(const utf8* path, const json_t& jsonData, int indentSize)
void WriteToFile(u8string_view path, const json_t& jsonData, int indentSize)
{
// Serialise JSON
std::string jsonOutput = jsonData.dump(indentSize);
@@ -58,12 +53,6 @@ namespace Json
fs.Write(jsonOutput.data(), jsonOutput.size());
}
void WriteToFile(const fs::path& path, const json_t& jsonData, int indentSize)
{
auto path8 = path.u8string();
WriteToFile(path8.c_str(), jsonData, indentSize);
}
json_t FromString(std::string_view raw)
{
json_t json;
@@ -74,7 +63,7 @@ namespace Json
}
catch (const json_t::exception& e)
{
log_error("Unable to parse JSON string (%s)\n\t%s", raw, e.what());
log_error("Unable to parse JSON string (%.*s)\n\t%s", static_cast<int>(raw.length()), raw.data(), e.what());
}
return json;
@@ -90,7 +79,7 @@ namespace Json
}
catch (const json_t::exception& e)
{
log_error("Unable to parse JSON vector (%s)\n\t%s", vec.data(), e.what());
log_error("Unable to parse JSON vector (%.*s)\n\t%s", static_cast<int>(vec.size()), vec.data(), e.what());
}
return json;

View File

@@ -11,7 +11,6 @@
#include "../common.h"
#include "../core/String.hpp"
#include "FileSystem.hpp"
#include <nlohmann/json.hpp>
#include <string>
@@ -35,8 +34,7 @@ namespace Json
* @return A JSON representation of the file
* @note This function will throw an exception if the JSON file cannot be parsed
*/
json_t ReadFromFile(const utf8* path, size_t maxSize = MAX_JSON_SIZE);
json_t ReadFromFile(const fs::path& path, size_t maxSize = MAX_JSON_SIZE);
json_t ReadFromFile(u8string_view path, size_t maxSize = MAX_JSON_SIZE);
/**
* Read JSON file and parse the contents
@@ -44,8 +42,7 @@ namespace Json
* @param jsonData A JSON object
* @param indentSize The number of spaces in an indent, or removes whitespace on -1
*/
void WriteToFile(const utf8* path, const json_t& jsonData, int indentSize = 4);
void WriteToFile(const fs::path& path, const json_t& jsonData, int indentSize = 4);
void WriteToFile(u8string_view path, const json_t& jsonData, int indentSize = 4);
/**
* Parse JSON from a string