Mercurial > minori
diff 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 |
line wrap: on
line diff
--- a/include/core/json.h Fri Jul 25 10:05:23 2025 -0400 +++ b/include/core/json.h Fri Jul 25 10:16:02 2025 -0400 @@ -7,8 +7,10 @@ namespace nlohmann { +/* hack to get std::optional working */ template<typename T> -void to_json(nlohmann::json& j, const std::optional<T>& v) { +void to_json(nlohmann::json &j, const std::optional<T> &v) +{ if (v.has_value()) j = v.value(); else @@ -16,7 +18,8 @@ } template<typename T> -void from_json(const nlohmann::json& j, std::optional<T>& v) { +void from_json(const nlohmann::json &j, std::optional<T> &v) +{ v = j.is_null() ? std::nullopt : j.get<T>(); } @@ -25,7 +28,8 @@ namespace JSON { template<typename T = std::string> -T GetString(const nlohmann::json& json, const nlohmann::json::json_pointer& ptr, T def) { +T GetString(const nlohmann::json &json, const nlohmann::json::json_pointer &ptr, T def) +{ if (json.contains(ptr) && json[ptr].is_string()) return json[ptr].get<T>(); else @@ -33,7 +37,8 @@ } template<typename T = int> -T GetNumber(const nlohmann::json& json, const nlohmann::json::json_pointer& ptr, T def = 0) { +T GetNumber(const nlohmann::json &json, const nlohmann::json::json_pointer &ptr, T def = 0) +{ if (json.contains(ptr) && json[ptr].is_number()) return json[ptr].get<T>(); else @@ -41,15 +46,16 @@ } template<typename T = std::vector<std::string>> -T GetArray(const nlohmann::json& json, const nlohmann::json::json_pointer& ptr, T def = 0) { +T GetArray(const nlohmann::json &json, const nlohmann::json::json_pointer &ptr, T def = 0) +{ if (json.contains(ptr) && json[ptr].is_array()) return json[ptr].get<T>(); else return def; } -nlohmann::json GetValue(const nlohmann::json& json, const nlohmann::json::json_pointer& ptr); -bool GetBoolean(const nlohmann::json& json, const nlohmann::json::json_pointer& ptr, bool def = false); +nlohmann::json GetValue(const nlohmann::json &json, const nlohmann::json::json_pointer &ptr); +bool GetBoolean(const nlohmann::json &json, const nlohmann::json::json_pointer &ptr, bool def = false); } // namespace JSON