Mercurial > minori
comparison src/core/json.cc @ 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 | d02fdf1d6708 |
| children | 9b10175be389 |
comparison
equal
deleted
inserted
replaced
| 173:de0a8d2f28b3 | 174:f88eda79c60a |
|---|---|
| 1 #include "core/json.h" | 1 #include "core/json.h" |
| 2 | 2 |
| 3 namespace JSON { | 3 namespace JSON { |
| 4 | 4 |
| 5 std::string GetString(const nlohmann::json& json, const nlohmann::json::json_pointer& ptr, std::string def) { | 5 nlohmann::json GetValue(const nlohmann::json& json, const nlohmann::json::json_pointer& ptr) { |
| 6 if (json.contains(ptr) && json[ptr].is_string()) | 6 if (json.contains(ptr)) |
| 7 return json[ptr].get<std::string>(); | 7 return json.at(ptr); |
| 8 else | 8 else |
| 9 return def; | 9 return nlohmann::json(); |
| 10 } | |
| 11 | |
| 12 int GetInt(const nlohmann::json& json, const nlohmann::json::json_pointer& ptr, int def) { | |
| 13 if (json.contains(ptr) && json[ptr].is_number()) | |
| 14 return json[ptr].get<int>(); | |
| 15 else | |
| 16 return def; | |
| 17 } | 10 } |
| 18 | 11 |
| 19 bool GetBoolean(const nlohmann::json& json, const nlohmann::json::json_pointer& ptr, bool def) { | 12 bool GetBoolean(const nlohmann::json& json, const nlohmann::json::json_pointer& ptr, bool def) { |
| 20 if (json.contains(ptr) && json[ptr].is_boolean()) | 13 if (json.contains(ptr) && json[ptr].is_boolean()) |
| 21 return json[ptr].get<bool>(); | 14 return json[ptr].get<bool>(); |
| 22 else | 15 else |
| 23 return def; | 16 return def; |
| 24 } | 17 } |
| 25 | 18 |
| 26 double GetDouble(const nlohmann::json& json, const nlohmann::json::json_pointer& ptr, double def) { | |
| 27 if (json.contains(ptr) && json[ptr].is_number()) | |
| 28 return json[ptr].get<double>(); | |
| 29 else | |
| 30 return def; | |
| 31 } | |
| 32 | |
| 33 } // namespace JSON | 19 } // namespace JSON |
