Mercurial > minori
comparison include/core/json.h @ 202:71832ffe425a
animia: re-add kvm fd source
this is all being merged from my wildly out-of-date laptop. SORRY!
in other news, I edited the CI file to install the wayland client
as well, so the linux CI build might finally get wayland stuff.
author | Paper <paper@paper.us.eu.org> |
---|---|
date | Tue, 02 Jan 2024 06:05:06 -0500 |
parents | bc1ae1810855 |
children | 79a87a6dd39d |
comparison
equal
deleted
inserted
replaced
201:8f6f8dd2eb23 | 202:71832ffe425a |
---|---|
1 #ifndef __core__json_h | 1 #ifndef __core__json_h |
2 #define __core__json_h | 2 #define __core__json_h |
3 | 3 |
4 #include "json/json.h" | 4 #include "json/json.hpp" |
5 | |
6 #include <optional> | |
7 | |
8 namespace nlohmann { | |
9 | |
10 template<typename T> | |
11 void to_json(nlohmann::json& j, const std::optional<T>& v) | |
12 { | |
13 if (v.has_value()) | |
14 j = v.value(); | |
15 else | |
16 j = nullptr; | |
17 } | |
18 | |
19 template<typename T> | |
20 void from_json(const nlohmann::json& j, std::optional<T>& v) | |
21 { | |
22 return j.is_null() ? std::nullopt : j.get<T>(); | |
23 } | |
24 | |
25 } // namespace nlohmann | |
5 | 26 |
6 namespace JSON { | 27 namespace JSON { |
7 | 28 |
8 std::string GetString(nlohmann::json const& json, nlohmann::json::json_pointer const& ptr, std::string def = ""); | 29 template<typename T = std::string> |
9 int GetInt(nlohmann::json const& json, nlohmann::json::json_pointer const& ptr, int def = 0); | 30 T GetString(const nlohmann::json& json, const nlohmann::json::json_pointer& ptr, T def) { |
10 bool GetBoolean(nlohmann::json const& json, nlohmann::json::json_pointer const& ptr, bool def = false); | 31 if (json.contains(ptr) && json[ptr].is_string()) |
11 double GetDouble(nlohmann::json const& json, nlohmann::json::json_pointer const& ptr, double def = 0); | 32 return json[ptr].get<T>(); |
33 else | |
34 return def; | |
35 } | |
36 | |
37 template<typename T = int> | |
38 T GetNumber(const nlohmann::json& json, const nlohmann::json::json_pointer& ptr, T def = 0) { | |
39 if (json.contains(ptr) && json[ptr].is_number()) | |
40 return json[ptr].get<T>(); | |
41 else | |
42 return def; | |
43 } | |
44 | |
45 template<typename T = std::vector<std::string>> | |
46 T GetArray(const nlohmann::json& json, const nlohmann::json::json_pointer& ptr, T def = 0) { | |
47 if (json.contains(ptr) && json[ptr].is_array()) | |
48 return json[ptr].get<T>(); | |
49 else | |
50 return def; | |
51 } | |
52 | |
53 nlohmann::json GetValue(const nlohmann::json& json, const nlohmann::json::json_pointer& ptr); | |
54 bool GetBoolean(const nlohmann::json& json, const nlohmann::json::json_pointer& ptr, bool def = false); | |
12 | 55 |
13 } // namespace JSON | 56 } // namespace JSON |
14 | 57 |
15 #endif // __core__json_h | 58 #endif // __core__json_h |