mirror of
https://github.com/microsoft/PowerToys.git
synced 2025-12-29 00:24:42 +01:00
43 lines
1.1 KiB
C++
43 lines
1.1 KiB
C++
#include "pch.h"
|
|
#include "json.h"
|
|
|
|
#include <fstream>
|
|
|
|
namespace json
|
|
{
|
|
std::optional<JsonObject> from_file(std::wstring_view file_name)
|
|
{
|
|
try
|
|
{
|
|
std::ifstream file(file_name.data(), std::ios::binary);
|
|
if (file.is_open())
|
|
{
|
|
file.seekg(0, file.end);
|
|
auto length = file.tellg();
|
|
file.seekg(0, file.beg);
|
|
std::string obj_str;
|
|
obj_str.resize(length);
|
|
file.read(obj_str.data(), length);
|
|
|
|
return JsonValue::Parse(winrt::to_hstring(obj_str)).GetObjectW();
|
|
}
|
|
return std::nullopt;
|
|
}
|
|
catch (...)
|
|
{
|
|
return std::nullopt;
|
|
}
|
|
}
|
|
|
|
void to_file(std::wstring_view file_name, const JsonObject& obj)
|
|
{
|
|
std::ofstream file{ file_name.data(), std::ios::binary };
|
|
|
|
if(file.is_open())
|
|
{
|
|
std::string obj_str{winrt::to_string(obj.Stringify())};
|
|
file.write(obj_str.c_str(), obj_str.size());
|
|
}
|
|
}
|
|
}
|