2016-05-04 19:24:41 +02:00
|
|
|
/*****************************************************************************
|
2024-12-31 20:30:04 +01:00
|
|
|
* Copyright (c) 2014-2025 OpenRCT2 developers
|
2016-05-04 19:24:41 +02:00
|
|
|
*
|
2018-06-15 14:07:34 +02:00
|
|
|
* For a complete list of all authors, please refer to contributors.md
|
|
|
|
|
* Interested in contributing? Visit https://github.com/OpenRCT2/OpenRCT2
|
2016-05-04 19:24:41 +02:00
|
|
|
*
|
2018-06-15 14:07:34 +02:00
|
|
|
* OpenRCT2 is licensed under the GNU General Public License version 3.
|
2016-05-04 19:24:41 +02:00
|
|
|
*****************************************************************************/
|
|
|
|
|
|
2016-01-28 22:21:36 +00:00
|
|
|
#include "Json.hpp"
|
2018-06-22 22:58:39 +02:00
|
|
|
|
2024-07-09 16:23:31 +02:00
|
|
|
#include "../Diagnostic.h"
|
2020-11-07 13:42:04 +01:00
|
|
|
#include "FileStream.h"
|
2016-01-28 22:21:36 +00:00
|
|
|
#include "Memory.hpp"
|
|
|
|
|
#include "String.hpp"
|
|
|
|
|
|
2024-07-26 09:59:58 +02:00
|
|
|
namespace OpenRCT2::Json
|
2016-01-28 22:21:36 +00:00
|
|
|
{
|
2022-02-12 14:13:47 +01:00
|
|
|
json_t ReadFromFile(u8string_view path, size_t maxSize)
|
2016-01-28 22:21:36 +00:00
|
|
|
{
|
2025-03-25 18:23:36 +01:00
|
|
|
auto fs = OpenRCT2::FileStream(path, OpenRCT2::FileMode::open);
|
2016-01-28 22:21:36 +00:00
|
|
|
|
2020-04-22 17:09:29 +02:00
|
|
|
size_t fileLength = static_cast<size_t>(fs.GetLength());
|
2016-01-28 22:21:36 +00:00
|
|
|
if (fileLength > maxSize)
|
|
|
|
|
{
|
2016-01-30 11:12:25 +00:00
|
|
|
throw IOException("Json file too large.");
|
2016-01-28 22:21:36 +00:00
|
|
|
}
|
|
|
|
|
|
2020-08-12 00:56:28 +01:00
|
|
|
auto fileData = std::string(static_cast<size_t>(fileLength) + 1, '\0');
|
|
|
|
|
fs.Read(static_cast<void*>(fileData.data()), fileLength);
|
2016-01-28 22:21:36 +00:00
|
|
|
|
2020-08-12 00:56:28 +01:00
|
|
|
json_t json;
|
2016-01-28 22:21:36 +00:00
|
|
|
|
2020-08-12 00:56:28 +01:00
|
|
|
try
|
2016-01-28 22:21:36 +00:00
|
|
|
{
|
2024-05-06 16:15:00 +02:00
|
|
|
json = json_t::parse(fileData, /* callback */ nullptr, /* allow_exceptions */ true, /* ignore_comments */ true);
|
2020-08-12 00:56:28 +01:00
|
|
|
}
|
|
|
|
|
catch (const json_t::exception& e)
|
|
|
|
|
{
|
2025-07-31 11:01:48 +02:00
|
|
|
throw JsonException(
|
|
|
|
|
String::stdFormat(
|
|
|
|
|
"Unable to parse JSON file (%.*s)\n\t%s", static_cast<int>(path.length()), path.data(), e.what()));
|
2016-01-28 22:21:36 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return json;
|
|
|
|
|
}
|
|
|
|
|
|
2022-02-12 14:13:47 +01:00
|
|
|
void WriteToFile(u8string_view path, const json_t& jsonData, int indentSize)
|
2016-01-28 22:21:36 +00:00
|
|
|
{
|
|
|
|
|
// Serialise JSON
|
2020-08-12 00:56:28 +01:00
|
|
|
std::string jsonOutput = jsonData.dump(indentSize);
|
2016-01-28 22:21:36 +00:00
|
|
|
|
|
|
|
|
// Write to file
|
2025-03-25 18:23:36 +01:00
|
|
|
auto fs = OpenRCT2::FileStream(path, OpenRCT2::FileMode::write);
|
2020-08-12 00:56:28 +01:00
|
|
|
fs.Write(jsonOutput.data(), jsonOutput.size());
|
2016-01-28 22:21:36 +00:00
|
|
|
}
|
2018-01-08 16:22:45 +01:00
|
|
|
|
2020-08-12 00:56:28 +01:00
|
|
|
json_t FromString(std::string_view raw)
|
2018-01-08 16:22:45 +01:00
|
|
|
{
|
2020-08-12 00:56:28 +01:00
|
|
|
json_t json;
|
|
|
|
|
|
|
|
|
|
try
|
2018-01-08 16:22:45 +01:00
|
|
|
{
|
2024-05-06 16:15:00 +02:00
|
|
|
json = json_t::parse(raw, /* callback */ nullptr, /* allow_exceptions */ true, /* ignore_comments */ true);
|
2018-01-08 16:22:45 +01:00
|
|
|
}
|
2020-08-12 00:56:28 +01:00
|
|
|
catch (const json_t::exception& e)
|
|
|
|
|
{
|
2023-01-17 13:24:51 +01:00
|
|
|
LOG_ERROR("Unable to parse JSON string (%.*s)\n\t%s", static_cast<int>(raw.length()), raw.data(), e.what());
|
2020-08-12 00:56:28 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return json;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
json_t FromVector(const std::vector<uint8_t>& vec)
|
|
|
|
|
{
|
|
|
|
|
json_t json;
|
|
|
|
|
|
|
|
|
|
try
|
|
|
|
|
{
|
2024-05-06 16:15:00 +02:00
|
|
|
json = json_t::parse(
|
|
|
|
|
vec.begin(), vec.end(), /* callback */ nullptr, /* allow_exceptions */ true, /* ignore_comments */ true);
|
2020-08-12 00:56:28 +01:00
|
|
|
}
|
|
|
|
|
catch (const json_t::exception& e)
|
|
|
|
|
{
|
2023-01-17 13:24:51 +01:00
|
|
|
LOG_ERROR("Unable to parse JSON vector (%.*s)\n\t%s", static_cast<int>(vec.size()), vec.data(), e.what());
|
2020-08-12 00:56:28 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return json;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
std::string GetString(const json_t& jsonObj, const std::string& defaultValue)
|
|
|
|
|
{
|
|
|
|
|
return jsonObj.is_string() ? jsonObj.get<std::string>() : defaultValue;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
bool GetBoolean(const json_t& jsonObj, bool defaultValue)
|
|
|
|
|
{
|
|
|
|
|
return jsonObj.is_boolean() ? jsonObj.get<bool>() : defaultValue;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
json_t AsObject(const json_t& jsonObj)
|
|
|
|
|
{
|
|
|
|
|
return jsonObj.is_object() ? jsonObj : json_t::object();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
json_t AsArray(const json_t& jsonObj)
|
|
|
|
|
{
|
|
|
|
|
if (jsonObj.is_array())
|
|
|
|
|
{
|
|
|
|
|
return jsonObj;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
json_t retVal = json_t::array();
|
|
|
|
|
|
|
|
|
|
if (jsonObj.is_object())
|
|
|
|
|
{
|
|
|
|
|
for (const auto& jItem : jsonObj)
|
|
|
|
|
{
|
|
|
|
|
retVal.push_back(jItem);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
else if (!jsonObj.is_null())
|
|
|
|
|
{
|
|
|
|
|
retVal.push_back(jsonObj);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return retVal;
|
2018-01-08 16:22:45 +01:00
|
|
|
}
|
2024-07-26 09:59:58 +02:00
|
|
|
} // namespace OpenRCT2::Json
|