Mercurial > minori
comparison src/json.cpp @ 6:1d82f6e04d7d
Update: add first parts to the settings dialog
| author | Paper <mrpapersonic@gmail.com> |
|---|---|
| date | Wed, 16 Aug 2023 00:49:17 -0400 |
| parents | 23d0d9319a00 |
| children | 07a9095eaeed |
comparison
equal
deleted
inserted
replaced
| 5:51ae25154b70 | 6:1d82f6e04d7d |
|---|---|
| 1 #include "json.h" | 1 #include "json.h" |
| 2 | 2 |
| 3 namespace JSON { | 3 namespace JSON { |
| 4 | 4 |
| 5 std::string GetString(nlohmann::json const& json, std::string const& key) { | 5 std::string GetString(nlohmann::json const& json, nlohmann::json::json_pointer const& ptr) { |
| 6 auto item = json.find(key); | 6 if (json.contains(ptr) && json[ptr].is_string()) |
| 7 if (item != json.end() && item->is_string()) | 7 return json[ptr].get<std::string>(); |
| 8 return item->get<std::string>(); | |
| 9 else return ""; | 8 else return ""; |
| 10 } | 9 } |
| 11 | 10 |
| 12 int GetInt(nlohmann::json const& json, std::string const& key) { | 11 int GetInt(nlohmann::json const& json, nlohmann::json::json_pointer const& ptr) { |
| 13 auto item = json.find(key); | 12 if (json.contains(ptr) && json[ptr].is_number()) |
| 14 if (item != json.end() && item->is_number()) | 13 return json[ptr].get<int>(); |
| 15 return item->get<int>(); | |
| 16 else return 0; | 14 else return 0; |
| 17 } | 15 } |
| 18 | 16 |
| 19 bool GetBoolean(nlohmann::json const& json, std::string const& key) { | 17 bool GetBoolean(nlohmann::json const& json, nlohmann::json::json_pointer const& ptr) { |
| 20 auto item = json.find(key); | 18 if (json.contains(ptr) && json[ptr].is_boolean()) |
| 21 if (item != json.end() && item->is_boolean()) | 19 return json[ptr].get<bool>(); |
| 22 return item->get<bool>(); | |
| 23 else return false; | 20 else return false; |
| 24 } | 21 } |
| 25 | 22 |
| 26 double GetDouble(nlohmann::json const& json, std::string const& key) { | 23 double GetDouble(nlohmann::json const& json, nlohmann::json::json_pointer const& ptr) { |
| 27 auto item = json.find(key); | 24 if (json.contains(ptr) && json[ptr].is_number()) |
| 28 if (item != json.end() && item->is_number()) | 25 return json[ptr].get<double>(); |
| 29 return item->get<double>(); | |
| 30 else return 0; | 26 else return 0; |
| 31 } | 27 } |
| 32 | 28 |
| 33 } | 29 } |
| 34 | 30 |
