PowerToys/src/common/json.cpp
stefansjfw 1476395829
Enable non-ascii chars (#1669)
* 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 commit 7006bcb038.

* Revert "Use stream read and write methods"

This reverts commit ec06a1a05b.
2020-03-25 11:11:27 +01:00

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);
}
}