Files
OpenRCT2-Web/src/openrct2/core/Json.cpp
T

129 lines
3.6 KiB
C++
Raw Normal View History

/*****************************************************************************
2024-12-31 20:30:04 +01:00
* Copyright (c) 2014-2025 OpenRCT2 developers
*
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
*
2018-06-15 14:07:34 +02:00
* OpenRCT2 is licensed under the GNU General Public License version 3.
*****************************************************************************/
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"
#include "FileStream.h"
2016-01-28 22:21:36 +00:00
#include "Memory.hpp"
#include "String.hpp"
namespace OpenRCT2::Json
2016-01-28 22:21:36 +00: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)
{
throw IOException("Json file too large.");
2016-01-28 22:21:36 +00: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
json_t json;
2016-01-28 22:21:36 +00: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);
}
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;
}
void WriteToFile(u8string_view path, const json_t& jsonData, int indentSize)
2016-01-28 22:21:36 +00:00
{
// Serialise JSON
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);
fs.Write(jsonOutput.data(), jsonOutput.size());
2016-01-28 22:21:36 +00:00
}
2018-01-08 16:22:45 +01:00
json_t FromString(std::string_view raw)
2018-01-08 16:22:45 +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
}
catch (const json_t::exception& e)
{
LOG_ERROR("Unable to parse JSON string (%.*s)\n\t%s", static_cast<int>(raw.length()), raw.data(), e.what());
}
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);
}
catch (const json_t::exception& e)
{
LOG_ERROR("Unable to parse JSON vector (%.*s)\n\t%s", static_cast<int>(vec.size()), vec.data(), e.what());
}
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
}
} // namespace OpenRCT2::Json