comparison include/core/json.h @ 198:bc1ae1810855

dep/animia: switch from using classes to global functions the old idea was ok, but sort of hackish; this method doesn't use classes at all, and this way (especially important!) we can do wayland stuff AND x11 at the same time, which wasn't really possible without stupid workarounds in the other method
author Paper <mrpapersonic@gmail.com>
date Sun, 24 Dec 2023 02:59:42 -0500
parents 9b10175be389
children 79a87a6dd39d
comparison
equal deleted inserted replaced
197:c4ca035c565d 198:bc1ae1810855
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.hpp" 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 template<typename T = std::string> 29 template<typename T = std::string>
9 T GetString(const nlohmann::json& json, const nlohmann::json::json_pointer& ptr, T def) { 30 T GetString(const nlohmann::json& json, const nlohmann::json::json_pointer& ptr, T def) {