| 6 | * tree. An additional intellectual property rights grant can be found |
| 7 | * in the file PATENTS. All contributing project authors may |
| 8 | * be found in the AUTHORS file in the root of the source tree. |
| 9 | */ |
| 10 |
| 11 | #ifndef RTC_BASE_STRINGS_JSON_H_ |
| 12 | #define RTC_BASE_STRINGS_JSON_H_ |
| 13 |
| 14 | #include <string> |
| 15 | #include <vector> |
| 16 |
| 17 | #include "absl/strings/string_view.h" |
| 18 | #include "third_party/jsoncpp/json.h" |
| 19 |
| 20 | namespace rtc { |
| 21 |
| 22 | /////////////////////////////////////////////////////////////////////////////// |
| 23 | // JSON Helpers |
| 24 | /////////////////////////////////////////////////////////////////////////////// |
| 25 |
| 26 | // Robust conversion operators, better than the ones in JsonCpp. |
| 27 | bool GetIntFromJson(const Json::Value& in, int* out); |
| 28 | bool GetUIntFromJson(const Json::Value& in, unsigned int* out); |
| 29 | bool GetStringFromJson(const Json::Value& in, std::string* out); |
| 30 | bool GetBoolFromJson(const Json::Value& in, bool* out); |
| 31 | bool GetDoubleFromJson(const Json::Value& in, double* out); |
| 32 |
| 33 | // Pull values out of a JSON array. |
| 34 | bool GetValueFromJsonArray(const Json::Value& in, size_t n, Json::Value* out); |
| 35 | bool GetIntFromJsonArray(const Json::Value& in, size_t n, int* out); |
| 36 | bool GetUIntFromJsonArray(const Json::Value& in, size_t n, unsigned int* out); |
| 37 | bool GetStringFromJsonArray(const Json::Value& in, size_t n, std::string* out); |
| 38 | bool GetBoolFromJsonArray(const Json::Value& in, size_t n, bool* out); |
| 39 | bool GetDoubleFromJsonArray(const Json::Value& in, size_t n, double* out); |
| 40 |
| 41 | // Convert json arrays to std::vector |
| 42 | bool JsonArrayToValueVector(const Json::Value& in, |
| 43 | std::vector<Json::Value>* out); |
| 44 | bool JsonArrayToIntVector(const Json::Value& in, std::vector<int>* out); |
| 45 | bool JsonArrayToUIntVector(const Json::Value& in, |
| 46 | std::vector<unsigned int>* out); |
| 47 | bool JsonArrayToStringVector(const Json::Value& in, |
| 48 | std::vector<std::string>* out); |
| 49 | bool JsonArrayToBoolVector(const Json::Value& in, std::vector<bool>* out); |
| 50 | bool JsonArrayToDoubleVector(const Json::Value& in, std::vector<double>* out); |
| 51 |
| 52 | // Convert std::vector to json array |
| 53 | Json::Value ValueVectorToJsonArray(const std::vector<Json::Value>& in); |
| 54 | Json::Value IntVectorToJsonArray(const std::vector<int>& in); |
| 55 | Json::Value UIntVectorToJsonArray(const std::vector<unsigned int>& in); |
| 56 | Json::Value StringVectorToJsonArray(const std::vector<std::string>& in); |
| 57 | Json::Value BoolVectorToJsonArray(const std::vector<bool>& in); |
| 58 | Json::Value DoubleVectorToJsonArray(const std::vector<double>& in); |
| 59 |
| 60 | // Pull values out of a JSON object. |
| 61 | bool GetValueFromJsonObject(const Json::Value& in, |
| 62 | absl::string_view k, |
| 63 | Json::Value* out); |
| 64 | bool GetIntFromJsonObject(const Json::Value& in, absl::string_view k, int* out); |
| 65 | bool GetUIntFromJsonObject(const Json::Value& in, |
| 66 | absl::string_view k, |
| 67 | unsigned int* out); |
| 68 | bool GetStringFromJsonObject(const Json::Value& in, |
| 69 | absl::string_view k, |
| 70 | std::string* out); |
| 71 | bool GetBoolFromJsonObject(const Json::Value& in, |
| 72 | absl::string_view k, |
| 73 | bool* out); |
| 74 | bool GetDoubleFromJsonObject(const Json::Value& in, |
| 75 | absl::string_view k, |
| 76 | double* out); |
| 77 |
| 78 | // Writes out a Json value as a string. |
| 79 | std::string JsonValueToString(const Json::Value& json); |
| 80 |
| 81 | } // namespace rtc |
| 82 |
| 83 | #endif // RTC_BASE_STRINGS_JSON_H_ |
| 84 |