forked from microsoft/PowerToys
-
Notifications
You must be signed in to change notification settings - Fork 0
/
json.h
109 lines (96 loc) · 3.12 KB
/
json.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
#pragma once
#include <winrt/Windows.Foundation.h>
#include <winrt/Windows.Foundation.Collections.h>
#include <winrt/Windows.Data.Json.h>
#include <optional>
#include <fstream>
namespace json
{
using namespace winrt::Windows::Data::Json;
inline 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;
}
}
inline 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);
}
inline bool has(
const json::JsonObject& o,
std::wstring_view name,
const json::JsonValueType type = JsonValueType::Object)
{
return o.HasKey(name) && o.GetNamedValue(name).ValueType() == type;
}
template<typename T>
inline std::enable_if_t<std::is_arithmetic_v<T>, JsonValue> value(const T arithmetic)
{
return json::JsonValue::CreateNumberValue(arithmetic);
}
template<typename T>
inline std::enable_if_t<!std::is_arithmetic_v<T>, JsonValue> value(T s)
{
return json::JsonValue::CreateStringValue(s);
}
inline JsonValue value(const bool boolean)
{
return json::JsonValue::CreateBooleanValue(boolean);
}
inline JsonValue value(JsonObject value)
{
return value.as<JsonValue>();
}
inline JsonValue value(JsonValue value)
{
return value; // identity function overload for convenience
}
template<typename T, typename D = std::optional<T>>
requires std::constructible_from<std::optional<T>, D>
void get(const json::JsonObject& o, const wchar_t* name, T& destination, D default_value = std::nullopt)
{
try
{
if constexpr (std::is_same_v<T, bool>)
{
destination = o.GetNamedBoolean(name);
}
else if constexpr (std::is_arithmetic_v<T>)
{
destination = static_cast<T>(o.GetNamedNumber(name));
}
else if constexpr (std::is_same_v<T, std::wstring>)
{
destination = o.GetNamedString(name);
}
else if constexpr (std::is_same_v<T, json::JsonObject>)
{
destination = o.GetNamedObject(name);
}
else
{
static_assert(std::bool_constant<std::is_same_v<T, T&>>::value, "Unsupported type");
}
}
catch (...)
{
std::optional<T> maybe_default{ std::move(default_value) };
if (maybe_default.has_value())
destination = std::move(*maybe_default);
}
}
}