Mercurial > minori
annotate include/core/json.h @ 231:69f4768a820c
chore: merge divergent branches
| author | Paper <paper@paper.us.eu.org> | 
|---|---|
| date | Sat, 13 Jan 2024 09:43:41 -0500 | 
| parents | 79a87a6dd39d | 
| children | 862d0d8619f6 | 
| rev | line source | 
|---|---|
| 9 | 1 #ifndef __core__json_h | 
| 2 #define __core__json_h | |
| 64 | 3 | 
| 175 | 4 #include "json/json.hpp" | 
| 64 | 5 | 
| 198 
bc1ae1810855
dep/animia: switch from using classes to global functions
 Paper <mrpapersonic@gmail.com> parents: 
175diff
changeset | 6 #include <optional> | 
| 
bc1ae1810855
dep/animia: switch from using classes to global functions
 Paper <mrpapersonic@gmail.com> parents: 
175diff
changeset | 7 | 
| 
bc1ae1810855
dep/animia: switch from using classes to global functions
 Paper <mrpapersonic@gmail.com> parents: 
175diff
changeset | 8 namespace nlohmann { | 
| 
bc1ae1810855
dep/animia: switch from using classes to global functions
 Paper <mrpapersonic@gmail.com> parents: 
175diff
changeset | 9 | 
| 
bc1ae1810855
dep/animia: switch from using classes to global functions
 Paper <mrpapersonic@gmail.com> parents: 
175diff
changeset | 10 template<typename T> | 
| 
bc1ae1810855
dep/animia: switch from using classes to global functions
 Paper <mrpapersonic@gmail.com> parents: 
175diff
changeset | 11 void to_json(nlohmann::json& j, const std::optional<T>& v) | 
| 
bc1ae1810855
dep/animia: switch from using classes to global functions
 Paper <mrpapersonic@gmail.com> parents: 
175diff
changeset | 12 { | 
| 
bc1ae1810855
dep/animia: switch from using classes to global functions
 Paper <mrpapersonic@gmail.com> parents: 
175diff
changeset | 13 if (v.has_value()) | 
| 
bc1ae1810855
dep/animia: switch from using classes to global functions
 Paper <mrpapersonic@gmail.com> parents: 
175diff
changeset | 14 j = v.value(); | 
| 
bc1ae1810855
dep/animia: switch from using classes to global functions
 Paper <mrpapersonic@gmail.com> parents: 
175diff
changeset | 15 else | 
| 
bc1ae1810855
dep/animia: switch from using classes to global functions
 Paper <mrpapersonic@gmail.com> parents: 
175diff
changeset | 16 j = nullptr; | 
| 
bc1ae1810855
dep/animia: switch from using classes to global functions
 Paper <mrpapersonic@gmail.com> parents: 
175diff
changeset | 17 } | 
| 
bc1ae1810855
dep/animia: switch from using classes to global functions
 Paper <mrpapersonic@gmail.com> parents: 
175diff
changeset | 18 | 
| 
bc1ae1810855
dep/animia: switch from using classes to global functions
 Paper <mrpapersonic@gmail.com> parents: 
175diff
changeset | 19 template<typename T> | 
| 
bc1ae1810855
dep/animia: switch from using classes to global functions
 Paper <mrpapersonic@gmail.com> parents: 
175diff
changeset | 20 void from_json(const nlohmann::json& j, std::optional<T>& v) | 
| 
bc1ae1810855
dep/animia: switch from using classes to global functions
 Paper <mrpapersonic@gmail.com> parents: 
175diff
changeset | 21 { | 
| 220 | 22 v = j.is_null() ? std::nullopt : j.get<T>(); | 
| 198 
bc1ae1810855
dep/animia: switch from using classes to global functions
 Paper <mrpapersonic@gmail.com> parents: 
175diff
changeset | 23 } | 
| 
bc1ae1810855
dep/animia: switch from using classes to global functions
 Paper <mrpapersonic@gmail.com> parents: 
175diff
changeset | 24 | 
| 
bc1ae1810855
dep/animia: switch from using classes to global functions
 Paper <mrpapersonic@gmail.com> parents: 
175diff
changeset | 25 } // namespace nlohmann | 
| 
bc1ae1810855
dep/animia: switch from using classes to global functions
 Paper <mrpapersonic@gmail.com> parents: 
175diff
changeset | 26 | 
| 9 | 27 namespace JSON { | 
| 64 | 28 | 
| 174 
f88eda79c60a
anime/db: add some more json functionality, still doesn't compile :/
 Paper <mrpapersonic@gmail.com> parents: 
102diff
changeset | 29 template<typename T = std::string> | 
| 
f88eda79c60a
anime/db: add some more json functionality, still doesn't compile :/
 Paper <mrpapersonic@gmail.com> parents: 
102diff
changeset | 30 T GetString(const nlohmann::json& json, const nlohmann::json::json_pointer& ptr, T def) { | 
| 
f88eda79c60a
anime/db: add some more json functionality, still doesn't compile :/
 Paper <mrpapersonic@gmail.com> parents: 
102diff
changeset | 31 if (json.contains(ptr) && json[ptr].is_string()) | 
| 
f88eda79c60a
anime/db: add some more json functionality, still doesn't compile :/
 Paper <mrpapersonic@gmail.com> parents: 
102diff
changeset | 32 return json[ptr].get<T>(); | 
| 
f88eda79c60a
anime/db: add some more json functionality, still doesn't compile :/
 Paper <mrpapersonic@gmail.com> parents: 
102diff
changeset | 33 else | 
| 
f88eda79c60a
anime/db: add some more json functionality, still doesn't compile :/
 Paper <mrpapersonic@gmail.com> parents: 
102diff
changeset | 34 return def; | 
| 
f88eda79c60a
anime/db: add some more json functionality, still doesn't compile :/
 Paper <mrpapersonic@gmail.com> parents: 
102diff
changeset | 35 } | 
| 
f88eda79c60a
anime/db: add some more json functionality, still doesn't compile :/
 Paper <mrpapersonic@gmail.com> parents: 
102diff
changeset | 36 | 
| 
f88eda79c60a
anime/db: add some more json functionality, still doesn't compile :/
 Paper <mrpapersonic@gmail.com> parents: 
102diff
changeset | 37 template<typename T = int> | 
| 
f88eda79c60a
anime/db: add some more json functionality, still doesn't compile :/
 Paper <mrpapersonic@gmail.com> parents: 
102diff
changeset | 38 T GetNumber(const nlohmann::json& json, const nlohmann::json::json_pointer& ptr, T def = 0) { | 
| 
f88eda79c60a
anime/db: add some more json functionality, still doesn't compile :/
 Paper <mrpapersonic@gmail.com> parents: 
102diff
changeset | 39 if (json.contains(ptr) && json[ptr].is_number()) | 
| 
f88eda79c60a
anime/db: add some more json functionality, still doesn't compile :/
 Paper <mrpapersonic@gmail.com> parents: 
102diff
changeset | 40 return json[ptr].get<T>(); | 
| 
f88eda79c60a
anime/db: add some more json functionality, still doesn't compile :/
 Paper <mrpapersonic@gmail.com> parents: 
102diff
changeset | 41 else | 
| 
f88eda79c60a
anime/db: add some more json functionality, still doesn't compile :/
 Paper <mrpapersonic@gmail.com> parents: 
102diff
changeset | 42 return def; | 
| 
f88eda79c60a
anime/db: add some more json functionality, still doesn't compile :/
 Paper <mrpapersonic@gmail.com> parents: 
102diff
changeset | 43 } | 
| 
f88eda79c60a
anime/db: add some more json functionality, still doesn't compile :/
 Paper <mrpapersonic@gmail.com> parents: 
102diff
changeset | 44 | 
| 
f88eda79c60a
anime/db: add some more json functionality, still doesn't compile :/
 Paper <mrpapersonic@gmail.com> parents: 
102diff
changeset | 45 template<typename T = std::vector<std::string>> | 
| 
f88eda79c60a
anime/db: add some more json functionality, still doesn't compile :/
 Paper <mrpapersonic@gmail.com> parents: 
102diff
changeset | 46 T GetArray(const nlohmann::json& json, const nlohmann::json::json_pointer& ptr, T def = 0) { | 
| 
f88eda79c60a
anime/db: add some more json functionality, still doesn't compile :/
 Paper <mrpapersonic@gmail.com> parents: 
102diff
changeset | 47 if (json.contains(ptr) && json[ptr].is_array()) | 
| 
f88eda79c60a
anime/db: add some more json functionality, still doesn't compile :/
 Paper <mrpapersonic@gmail.com> parents: 
102diff
changeset | 48 return json[ptr].get<T>(); | 
| 
f88eda79c60a
anime/db: add some more json functionality, still doesn't compile :/
 Paper <mrpapersonic@gmail.com> parents: 
102diff
changeset | 49 else | 
| 
f88eda79c60a
anime/db: add some more json functionality, still doesn't compile :/
 Paper <mrpapersonic@gmail.com> parents: 
102diff
changeset | 50 return def; | 
| 
f88eda79c60a
anime/db: add some more json functionality, still doesn't compile :/
 Paper <mrpapersonic@gmail.com> parents: 
102diff
changeset | 51 } | 
| 
f88eda79c60a
anime/db: add some more json functionality, still doesn't compile :/
 Paper <mrpapersonic@gmail.com> parents: 
102diff
changeset | 52 | 
| 
f88eda79c60a
anime/db: add some more json functionality, still doesn't compile :/
 Paper <mrpapersonic@gmail.com> parents: 
102diff
changeset | 53 nlohmann::json GetValue(const nlohmann::json& json, const nlohmann::json::json_pointer& ptr); | 
| 
f88eda79c60a
anime/db: add some more json functionality, still doesn't compile :/
 Paper <mrpapersonic@gmail.com> parents: 
102diff
changeset | 54 bool GetBoolean(const nlohmann::json& json, const nlohmann::json::json_pointer& ptr, bool def = false); | 
| 64 | 55 | 
| 9 | 56 } // namespace JSON | 
| 64 | 57 | 
| 9 | 58 #endif // __core__json_h | 
