Mercurial > minori
comparison include/core/json.h @ 174:f88eda79c60a
anime/db: add some more json functionality, still doesn't compile :/
| author | Paper <mrpapersonic@gmail.com> | 
|---|---|
| date | Wed, 29 Nov 2023 13:53:56 -0500 | 
| parents | b315f3759c56 | 
| children | 9b10175be389 | 
   comparison
  equal
  deleted
  inserted
  replaced
| 173:de0a8d2f28b3 | 174:f88eda79c60a | 
|---|---|
| 3 | 3 | 
| 4 #include "json/json.h" | 4 #include "json/json.h" | 
| 5 | 5 | 
| 6 namespace JSON { | 6 namespace JSON { | 
| 7 | 7 | 
| 8 std::string GetString(nlohmann::json const& json, nlohmann::json::json_pointer const& ptr, std::string def = ""); | 8 template<typename T = std::string> | 
| 9 int GetInt(nlohmann::json const& json, nlohmann::json::json_pointer const& ptr, int def = 0); | 9 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); | 10 if (json.contains(ptr) && json[ptr].is_string()) | 
| 11 double GetDouble(nlohmann::json const& json, nlohmann::json::json_pointer const& ptr, double def = 0); | 11 return json[ptr].get<T>(); | 
| 12 else | |
| 13 return def; | |
| 14 } | |
| 15 | |
| 16 template<typename T = int> | |
| 17 T GetNumber(const nlohmann::json& json, const nlohmann::json::json_pointer& ptr, T def = 0) { | |
| 18 if (json.contains(ptr) && json[ptr].is_number()) | |
| 19 return json[ptr].get<T>(); | |
| 20 else | |
| 21 return def; | |
| 22 } | |
| 23 | |
| 24 template<typename T = std::vector<std::string>> | |
| 25 T GetArray(const nlohmann::json& json, const nlohmann::json::json_pointer& ptr, T def = 0) { | |
| 26 if (json.contains(ptr) && json[ptr].is_array()) | |
| 27 return json[ptr].get<T>(); | |
| 28 else | |
| 29 return def; | |
| 30 } | |
| 31 | |
| 32 nlohmann::json GetValue(const nlohmann::json& json, const nlohmann::json::json_pointer& ptr); | |
| 33 bool GetBoolean(const nlohmann::json& json, const nlohmann::json::json_pointer& ptr, bool def = false); | |
| 12 | 34 | 
| 13 } // namespace JSON | 35 } // namespace JSON | 
| 14 | 36 | 
| 15 #endif // __core__json_h | 37 #endif // __core__json_h | 
