[Serialization] Make JsonSerializationContext FilesystemAppComponent-compatible

This commit is contained in:
SweetId
2024-03-11 19:08:10 -04:00
parent cc2c36c75d
commit 8cc1719d1b
2 changed files with 25 additions and 3 deletions

View File

@@ -36,11 +36,20 @@ namespace Nz
bool JsonSerializationContext::Load(const std::filesystem::path& path)
{
std::ifstream file(path);
if (!file || !file.is_open())
Nz::File file(path, Nz::OpenMode::Read);
return Load(file);
}
bool JsonSerializationContext::Load(Stream& stream)
{
if (!stream.IsReadable())
return false;
m_impl->json = nlohmann::json::parse(file);
auto size = stream.GetSize();
std::vector<char> content(size);
stream.Read(content.data(), content.size());
m_impl->json = nlohmann::json::parse(content.begin(), content.end());
m_impl->stack.push(&m_impl->json);
return true;
}
@@ -129,4 +138,12 @@ namespace Nz
*value = m_impl->Top()[name].get<std::string>();
return true;
}
std::shared_ptr<JsonSerializationContext> JsonSerializationContext::LoadFromStream(Stream& stream, const JsonSerializationContext::Params&)
{
std::shared_ptr<JsonSerializationContext> context = std::make_shared<JsonSerializationContext>();
if (context->Load(stream))
return context;
return {};
}
}