Mercurial > minori
comparison src/json.cpp @ 7:07a9095eaeed
Update
Refactored some code, moved some around
| author | Paper <mrpapersonic@gmail.com> |
|---|---|
| date | Thu, 24 Aug 2023 23:11:38 -0400 |
| parents | 1d82f6e04d7d |
| children |
comparison
equal
deleted
inserted
replaced
| 6:1d82f6e04d7d | 7:07a9095eaeed |
|---|---|
| 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, nlohmann::json::json_pointer const& ptr) { | 5 std::string GetString(nlohmann::json const& json, nlohmann::json::json_pointer const& ptr, std::string def) { |
| 6 if (json.contains(ptr) && json[ptr].is_string()) | 6 if (json.contains(ptr) && json[ptr].is_string()) |
| 7 return json[ptr].get<std::string>(); | 7 return json[ptr].get<std::string>(); |
| 8 else return ""; | 8 else return def; |
| 9 } | 9 } |
| 10 | 10 |
| 11 int GetInt(nlohmann::json const& json, nlohmann::json::json_pointer const& ptr) { | 11 int GetInt(nlohmann::json const& json, nlohmann::json::json_pointer const& ptr, int def) { |
| 12 if (json.contains(ptr) && json[ptr].is_number()) | 12 if (json.contains(ptr) && json[ptr].is_number()) |
| 13 return json[ptr].get<int>(); | 13 return json[ptr].get<int>(); |
| 14 else return 0; | 14 else return def; |
| 15 } | 15 } |
| 16 | 16 |
| 17 bool GetBoolean(nlohmann::json const& json, nlohmann::json::json_pointer const& ptr) { | 17 bool GetBoolean(nlohmann::json const& json, nlohmann::json::json_pointer const& ptr, bool def) { |
| 18 if (json.contains(ptr) && json[ptr].is_boolean()) | 18 if (json.contains(ptr) && json[ptr].is_boolean()) |
| 19 return json[ptr].get<bool>(); | 19 return json[ptr].get<bool>(); |
| 20 else return false; | 20 else return def; |
| 21 } | 21 } |
| 22 | 22 |
| 23 double GetDouble(nlohmann::json const& json, nlohmann::json::json_pointer const& ptr) { | 23 double GetDouble(nlohmann::json const& json, nlohmann::json::json_pointer const& ptr, double def) { |
| 24 if (json.contains(ptr) && json[ptr].is_number()) | 24 if (json.contains(ptr) && json[ptr].is_number()) |
| 25 return json[ptr].get<double>(); | 25 return json[ptr].get<double>(); |
| 26 else return 0; | 26 else return def; |
| 27 } | 27 } |
| 28 | 28 |
| 29 } | 29 } |
| 30 | 30 |
