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
|
|
|
|
{
|
2020-03-25 18:11:27 +08:00
|
|
|
std::ifstream file(file_name.data(), std::ios::binary);
|
2020-03-18 18:17:57 +08:00
|
|
|
if (file.is_open())
|
|
|
|
{
|
2020-03-25 18:11:27 +08:00
|
|
|
using isbi = std::istreambuf_iterator<char>;
|
|
|
|
std::string obj_str{ isbi{ file }, isbi{} };
|
|
|
|
return JsonValue::Parse(winrt::to_hstring(obj_str)).GetObjectW();
|
2020-03-18 18:17:57 +08:00
|
|
|
}
|
|
|
|
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-25 18:11:27 +08:00
|
|
|
std::wstring obj_str{ obj.Stringify().c_str() };
|
|
|
|
std::ofstream{ file_name.data(), std::ios::binary } << winrt::to_string(obj_str);
|
2019-12-06 16:40:23 +08:00
|
|
|
}
|
|
|
|
}
|