discord_ipc_cpp 1.0.0
C++ library for interfacing with Discord IPC socket.
Loading...
Searching...
No Matches
json.hpp
1/*
2 Copyright 2025 Peter Duanmu
3
4 You should have received a copy of the GNU General Public License along
5 with discord_ipc_cpp. If not, see <https://www.gnu.org/licenses/>.
6*/
7
8#ifndef DISCORD_IPC_CPP_INCLUDE_DISCORD_IPC_CPP_JSON_HPP_
9#define DISCORD_IPC_CPP_INCLUDE_DISCORD_IPC_CPP_JSON_HPP_
10
11#include <map>
12#include <string>
13#include <sstream>
14#include <optional>
15#include <variant>
16#include <vector>
17
27namespace discord_ipc_cpp::json {
28class JSON;
29
33using JSONNull = std::nullptr_t;
37using JSONString = std::string;
41using JSONInt = int;
45using JSONLong = int64_t;
49using JSONDouble = double;
53using JSONBool = bool;
57using JSONObject = std::map<std::string, JSON>;
61using JSONArray = std::vector<JSON>;
62
69class JSON {
70 private:
74 using JSONValue = std::variant<
77 JSONInt,
83 >;
84
90 JSONValue _value;
91
92 private:
98 void stringify(std::ostream& os) const;
99
100 public:
114 explicit JSON(JSONNull value);
122 explicit JSON(const JSONString& value);
130 explicit JSON(const char* value);
138 explicit JSON(JSONInt value);
146 explicit JSON(JSONLong value);
154 explicit JSON(JSONDouble value);
162 explicit JSON(JSONBool value);
170 explicit JSON(const JSONArray& value);
178 explicit JSON(const JSONObject& value);
179
209 const JSON& operator[](const JSONString& key) const;
210
222 const std::optional<JSON> safe_at(const JSONString& key) const;
223
236 template<typename T>
237 T as() const;
249 template<typename T>
250 std::optional<T> safe_as() const;
251
262 template<typename T>
263 bool is() const;
264
277 bool has(const JSONString& key) const;
278
290 void push_back(const JSON& item);
291
298 std::string to_string() const;
299};
300} // namespace discord_ipc_cpp::json
301
302#endif // DISCORD_IPC_CPP_INCLUDE_DISCORD_IPC_CPP_JSON_HPP_
Basic abstract JSON item.
Definition json.hpp:69
bool has(const JSONString &key) const
Checks current JSON for key.
void push_back(const JSON &item)
Appends item to current JSON.
std::string to_string() const
Stringifies JSON object.
JSON()
Creates an empty JSON object.
const JSON & operator[](const JSONString &key) const
Retrieve a nested value.
JSON(const JSONArray &value)
Creates an array JSON item.
T as() const
Retrieve the current JSON item as a specific type.
JSON(const char *value)
Creates a string JSON item.
const std::optional< JSON > safe_at(const JSONString &key) const
Retrieves a nested value safely.
std::optional< T > safe_as() const
Retrieve the current JSON item as a specific type.
JSON(const JSONObject &value)
Creates another nested JSON item.
bool is() const
Checks current JSON item type.
JSON(JSONInt value)
Creates a number JSON item.
JSON & operator[](const JSONString &key)
Retrieve a nested value.
JSON(JSONLong value)
Creates a number JSON item.
JSON(const JSONString &value)
Creates a string JSON item.
JSON(JSONNull value)
Creates a null JSON item.
JSON(JSONBool value)
Creates a bool JSON item.
JSON(JSONDouble value)
Creates a number JSON item.
Custom JSON implementation.
std::map< std::string, JSON > JSONObject
Representation of a key-value pair.
Definition json.hpp:57
bool JSONBool
Representation of bool.
Definition json.hpp:53
int64_t JSONLong
Representation of number.
Definition json.hpp:45
int JSONInt
Representation of number.
Definition json.hpp:41
double JSONDouble
Representation of number.
Definition json.hpp:49
std::nullptr_t JSONNull
Representation of null.
Definition json.hpp:33
std::vector< JSON > JSONArray
Representation of array.
Definition json.hpp:61
std::string JSONString
Representation of string.
Definition json.hpp:37