comparison include/core/json.h @ 369:47c9f8502269

*: clang-format all the things I've edited the formatting a bit. Now pointer asterisks (and reference ampersands) are on the variable instead of the type, as well as having newlines for function braces (but nothing else)
author Paper <paper@tflc.us>
date Fri, 25 Jul 2025 10:16:02 -0400
parents 1686fac290c5
children
comparison
equal deleted inserted replaced
368:6d37a998cf91 369:47c9f8502269
5 5
6 #include <optional> 6 #include <optional>
7 7
8 namespace nlohmann { 8 namespace nlohmann {
9 9
10 /* hack to get std::optional working */
10 template<typename T> 11 template<typename T>
11 void to_json(nlohmann::json& j, const std::optional<T>& v) { 12 void to_json(nlohmann::json &j, const std::optional<T> &v)
13 {
12 if (v.has_value()) 14 if (v.has_value())
13 j = v.value(); 15 j = v.value();
14 else 16 else
15 j = nullptr; 17 j = nullptr;
16 } 18 }
17 19
18 template<typename T> 20 template<typename T>
19 void from_json(const nlohmann::json& j, std::optional<T>& v) { 21 void from_json(const nlohmann::json &j, std::optional<T> &v)
22 {
20 v = j.is_null() ? std::nullopt : j.get<T>(); 23 v = j.is_null() ? std::nullopt : j.get<T>();
21 } 24 }
22 25
23 } // namespace nlohmann 26 } // namespace nlohmann
24 27
25 namespace JSON { 28 namespace JSON {
26 29
27 template<typename T = std::string> 30 template<typename T = std::string>
28 T GetString(const nlohmann::json& json, const nlohmann::json::json_pointer& ptr, T def) { 31 T GetString(const nlohmann::json &json, const nlohmann::json::json_pointer &ptr, T def)
32 {
29 if (json.contains(ptr) && json[ptr].is_string()) 33 if (json.contains(ptr) && json[ptr].is_string())
30 return json[ptr].get<T>(); 34 return json[ptr].get<T>();
31 else 35 else
32 return def; 36 return def;
33 } 37 }
34 38
35 template<typename T = int> 39 template<typename T = int>
36 T GetNumber(const nlohmann::json& json, const nlohmann::json::json_pointer& ptr, T def = 0) { 40 T GetNumber(const nlohmann::json &json, const nlohmann::json::json_pointer &ptr, T def = 0)
41 {
37 if (json.contains(ptr) && json[ptr].is_number()) 42 if (json.contains(ptr) && json[ptr].is_number())
38 return json[ptr].get<T>(); 43 return json[ptr].get<T>();
39 else 44 else
40 return def; 45 return def;
41 } 46 }
42 47
43 template<typename T = std::vector<std::string>> 48 template<typename T = std::vector<std::string>>
44 T GetArray(const nlohmann::json& json, const nlohmann::json::json_pointer& ptr, T def = 0) { 49 T GetArray(const nlohmann::json &json, const nlohmann::json::json_pointer &ptr, T def = 0)
50 {
45 if (json.contains(ptr) && json[ptr].is_array()) 51 if (json.contains(ptr) && json[ptr].is_array())
46 return json[ptr].get<T>(); 52 return json[ptr].get<T>();
47 else 53 else
48 return def; 54 return def;
49 } 55 }
50 56
51 nlohmann::json GetValue(const nlohmann::json& json, const nlohmann::json::json_pointer& ptr); 57 nlohmann::json GetValue(const nlohmann::json &json, const nlohmann::json::json_pointer &ptr);
52 bool GetBoolean(const nlohmann::json& json, const nlohmann::json::json_pointer& ptr, bool def = false); 58 bool GetBoolean(const nlohmann::json &json, const nlohmann::json::json_pointer &ptr, bool def = false);
53 59
54 } // namespace JSON 60 } // namespace JSON
55 61
56 #endif // MINORI_CORE_JSON_H_ 62 #endif // MINORI_CORE_JSON_H_