summaryrefslogtreecommitdiff
path: root/src/discord/json.hpp
blob: 837080bf19b077737b4fd41d85459037fe98b3d5 (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
141
142
143
144
145
146
147
148
#pragma once
#include <nlohmann/json.hpp>
#include <optional>
#include "util.hpp"

namespace detail { // more or less because idk what to name this stuff
template<typename T>
inline void json_direct(const ::nlohmann::json &j, const char *key, T &val) {
    if constexpr (::util::is_optional<T>::value)
        val = j.at(key).get<typename 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 (::util::is_optional<T>::value) {
        if (j.contains(key))
            val = j.at(key).get<typename 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 (::util::is_optional<T>::value) {
        const auto &at = j.at(key);
        if (!at.is_null())
            val = at.get<typename 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 (::util::is_optional<T>::value) {
        if (j.contains(key)) {
            const auto &at = j.at(key);
            if (!at.is_null())
                val = at.get<typename 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 (::util::is_optional<T>::value) {
        if (j.contains(key)) {
            const auto &at = j.at(key);
            if (!at.is_null())
                val = at.get<typename 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 (::util::is_optional<T>::value) {
        if (j.contains(key)) {
            const auto &at = j.at(key);
            if (at.is_null())
                val = fallback;
            else
                val = at.get<typename 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

// get a json value that is guaranteed to be present and non-null
#define JS_D(k, t)                    \
    do {                              \
        detail::json_direct(j, k, t); \
    } while (0)

// get a json value that may not be present
#define JS_O(k, t)                      \
    do {                                \
        detail::json_optional(j, k, t); \
    } while (0)

// get a json value that may be null
#define JS_N(k, t)                      \
    do {                                \
        detail::json_nullable(j, k, t); \
    } while (0)

// get a json value that may not be present or may be null
#define JS_ON(k, t)                              \
    do {                                         \
        detail::json_optional_nullable(j, k, t); \
    } while (0)

// set from a json value only if it is present. null will assign default-constructed value
#define JS_RD(k, t)                                     \
    do {                                                \
        detail::json_update_optional_nullable(j, k, t); \
    } while (0)

// set from a json value only if it is present. null will assign the given default
#define JS_RV(k, t, d)                                             \
    do {                                                           \
        detail::json_update_optional_nullable_default(j, k, t, d); \
    } while (0)

// set a json value from a std::optional only if it has a value
#define JS_IF(k, v)        \
    do {                   \
        if (v.has_value()) \
            j[k] = *v;     \
    } while (0)