Files
PowerToys/src/common/json.cpp
Andrey Nekrasov cf1b53831f Formatting chores (#1441)
* format_sources: exclude 3rd party sources

* format common project

* format leftovers in runner & settings projects

* move source formatting-related files according to #939
2020-03-05 13:07:06 +03:00

27 lines
649 B
C++

#include "pch.h"
#include "json.h"
#include <fstream>
namespace json
{
std::optional<JsonObject> from_file(std::wstring_view file_name)
{
try
{
std::wifstream file(file_name.data(), std::ios::binary);
using isbi = std::istreambuf_iterator<wchar_t>;
return JsonValue::Parse(std::wstring{ isbi{ file }, isbi{} }).GetObjectW();
}
catch (...)
{
return std::nullopt;
}
}
void to_file(std::wstring_view file_name, const JsonObject& obj)
{
std::wofstream{ file_name.data(), std::ios::binary } << obj.Stringify().c_str();
}
}