summaryrefslogtreecommitdiff
path: root/discord/json.hpp
blob: 40f078f9525beb317cf09315000f12e4ca46141e (plain)
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
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
#pragma once
#include <nlohmann/json.hpp>
#include <optional>

namespace detail { // more or less because idk what to name this stuff
template<typename T>
struct is_optional : ::std::false_type {};

template<typename T>
struct is_optional<::std::optional<T>> : ::std::true_type {};

template<typename T>
inline void json_direct(const ::nlohmann::json &j, const char *key, T &val) {
    if constexpr (is_optional<T>::value)
        val = j.at(key).get<T::value_type>();
    else
        j.at(key).get_to(val);
}

template<typename T>
inline void json_optional(const ::nlohmann::json &j, const char *key, T &val) {
    if constexpr (is_optional<T>::value) {
        if (j.contains(key))
            val = j.at(key).get<T::value_type>();
        else
            val = ::std::nullopt;
    } else {
        if (j.contains(key))
            j.at(key).get_to(val);
    }
}

template<typename T>
inline void json_nullable(const ::nlohmann::json &j, const char *key, T &val) {
    if constexpr (is_optional<T>::value) {
        const auto &at = j.at(key);
        if (!at.is_null())
            val = at.get<T::value_type>();
        else
            val = std::nullopt;
    } else {
        const auto &at = j.at(key);
        if (!at.is_null())
            at.get_to(val);
    }
}

template<typename T>
inline void json_optional_nullable(const ::nlohmann::json &j, const char *key, T &val) {
    if constexpr (is_optional<T>::value) {
        if (j.contains(key)) {
            const auto &at = j.at(key);
            if (!at.is_null())
                val = at.get<T::value_type>();
            else
                val = std::nullopt;
        } else {
            val = std::nullopt;
        }
    } else {
        if (j.contains(key)) {
            const auto &at = j.at(key);
            if (!at.is_null())
                at.get_to(val);
        }
    }
}

template<typename T>
inline void json_update_optional_nullable(const ::nlohmann::json &j, const char *key, T &val) {
    if constexpr (is_optional<T>::value) {
        if (j.contains(key)) {
            const auto &at = j.at(key);
            if (!at.is_null())
                val = at.get<T::value_type>();
            else
                val = std::nullopt;
        }
    } else {
        if (j.contains(key)) {
            const auto &at = j.at(key);
            if (!at.is_null())
                at.get_to(val);
            else
                val = T();
        }
    }
}

template<typename T, typename U>
inline void json_update_optional_nullable_default(const ::nlohmann::json &j, const char *key, T &val, const U &fallback) {
    if constexpr (is_optional<T>::value) {
        if (j.contains(key)) {
            const auto &at = j.at(key);
            if (at.is_null())
                val = fallback;
            else
                val = at.get<T::value_type>();
        }
    } else {
        if (j.contains(key)) {
            const auto &at = j.at(key);
            if (at.is_null())
                val = fallback;
            else
                at.get_to(val);
        }
    }
}
} // namespace detail

#define JS_D(k, t)                    \
    do {                              \
        detail::json_direct(j, k, t); \
    } while (0)

#define JS_O(k, t)                      \
    do {                                \
        detail::json_optional(j, k, t); \
    } while (0)

#define JS_N(k, t)                      \
    do {                                \
        detail::json_nullable(j, k, t); \
    } while (0)

#define JS_ON(k, t)                              \
    do {                                         \
        detail::json_optional_nullable(j, k, t); \
    } while (0)

#define JS_RD(k, t)                                     \
    do {                                                \
        detail::json_update_optional_nullable(j, k, t); \
    } while (0)

#define JS_RV(k, t, d)                                             \
    do {                                                           \
        detail::json_update_optional_nullable_default(j, k, t, d); \
    } while (0)