mirror of
https://github.com/microsoft/PowerToys.git
synced 2024-12-15 20:19:17 +08:00
1476395829
* Enable non-ascii chars * Follow file naming policy * Use stream read and write methods * Remove obsolete close() calls * Revert "Remove obsolete close() calls" This reverts commit7006bcb038
. * Revert "Use stream read and write methods" This reverts commitec06a1a05b
.
33 lines
855 B
C++
33 lines
855 B
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())
|
|
{
|
|
using isbi = std::istreambuf_iterator<char>;
|
|
std::string obj_str{ isbi{ file }, isbi{} };
|
|
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::wstring obj_str{ obj.Stringify().c_str() };
|
|
std::ofstream{ file_name.data(), std::ios::binary } << winrt::to_string(obj_str);
|
|
}
|
|
}
|