2019-12-06 16:40:23 +08:00
|
|
|
#include "pch.h"
|
|
|
|
#include "json.h"
|
|
|
|
|
|
|
|
#include <fstream>
|
|
|
|
|
|
|
|
namespace json
|
|
|
|
{
|
2020-03-05 18:07:06 +08:00
|
|
|
std::optional<JsonObject> from_file(std::wstring_view file_name)
|
2019-12-06 16:40:23 +08:00
|
|
|
{
|
2020-03-05 18:07:06 +08:00
|
|
|
try
|
|
|
|
{
|
|
|
|
std::wifstream file(file_name.data(), std::ios::binary);
|
2020-03-18 18:17:57 +08:00
|
|
|
if (file.is_open())
|
|
|
|
{
|
|
|
|
using isbi = std::istreambuf_iterator<wchar_t>;
|
|
|
|
return JsonValue::Parse(std::wstring{ isbi{ file }, isbi{} }).GetObjectW();
|
|
|
|
}
|
|
|
|
return std::nullopt;
|
2020-03-05 18:07:06 +08:00
|
|
|
}
|
|
|
|
catch (...)
|
|
|
|
{
|
|
|
|
return std::nullopt;
|
|
|
|
}
|
2019-12-06 16:40:23 +08:00
|
|
|
}
|
2020-03-05 18:07:06 +08:00
|
|
|
|
|
|
|
void to_file(std::wstring_view file_name, const JsonObject& obj)
|
2019-12-06 16:40:23 +08:00
|
|
|
{
|
2020-03-05 18:07:06 +08:00
|
|
|
std::wofstream{ file_name.data(), std::ios::binary } << obj.Stringify().c_str();
|
2019-12-06 16:40:23 +08:00
|
|
|
}
|
|
|
|
}
|