| 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 | #include "rtc_base/strings/json.h" |
| 12 |
| 13 | #include <errno.h> |
| 14 | #include <limits.h> |
| 15 | #include <stdlib.h> |
| 16 |
| 17 | #include "absl/strings/string_view.h" |
| 18 | #include "rtc_base/string_encode.h" |
| 19 |
| 20 | namespace rtc { |
| 21 |
| 22 | bool GetStringFromJson(const Json::Value& in, std::string* out) { |
| 23 | if (!in.isString()) { |
| 24 | if (in.isBool()) { |
| 25 | *out = rtc::ToString(in.asBool()); |
| 26 | } else if (in.isInt()) { |
| 27 | *out = rtc::ToString(in.asInt()); |
| 28 | } else if (in.isUInt()) { |
| 29 | *out = rtc::ToString(in.asUInt()); |
| 30 | } else if (in.isDouble()) { |
| 31 | *out = rtc::ToString(in.asDouble()); |
| 32 | } else { |
| 33 | return false; |
| 34 | } |
| 35 | } else { |
| 36 | *out = in.asString(); |
| 37 | } |
| 38 | return true; |
| 39 | } |
| 40 |
| 41 | bool GetIntFromJson(const Json::Value& in, int* out) { |
| 42 | bool ret; |
| 43 | if (!in.isString()) { |
| 44 | ret = in.isConvertibleTo(Json::intValue); |
| 45 | if (ret) { |
| 46 | *out = in.asInt(); |
| 47 | } |
| 48 | } else { |
| 49 | long val; // NOLINT |
| 50 | const char* c_str = in.asCString(); |
| 51 | char* end_ptr; |
| 52 | errno = 0; |
| 53 | val = strtol(c_str, &end_ptr, 10); // NOLINT |
| 54 | ret = (end_ptr != c_str && *end_ptr == '\0' && !errno && val >= INT_MIN && |
| 55 | val <= INT_MAX); |
| 56 | *out = val; |
| 57 | } |
| 58 | return ret; |
| 59 | } |
| 60 |
| 61 | bool GetUIntFromJson(const Json::Value& in, unsigned int* out) { |
| 62 | bool ret; |
| 63 | if (!in.isString()) { |
| 64 | ret = in.isConvertibleTo(Json::uintValue); |
| 65 | if (ret) { |
| 66 | *out = in.asUInt(); |
| 67 | } |
| 68 | } else { |
| 69 | unsigned long val; // NOLINT |
| 70 | const char* c_str = in.asCString(); |
| 71 | char* end_ptr; |
| 72 | errno = 0; |
| 73 | val = strtoul(c_str, &end_ptr, 10); // NOLINT |
| 74 | ret = (end_ptr != c_str && *end_ptr == '\0' && !errno && val <= UINT_MAX); |
| 75 | *out = val; |
| 76 | } |
| 77 | return ret; |
| 78 | } |
| 79 |
| 80 | bool GetBoolFromJson(const Json::Value& in, bool* out) { |
| 81 | bool ret; |
| 82 | if (!in.isString()) { |
| 83 | ret = in.isConvertibleTo(Json::booleanValue); |
| 84 | if (ret) { |
| 85 | *out = in.asBool(); |
| 86 | } |
| 87 | } else { |
| 88 | if (in.asString() == "true") { |
| 89 | *out = true; |
| 90 | ret = true; |
| 91 | } else if (in.asString() == "false") { |
| 92 | *out = false; |
| 93 | ret = true; |
| 94 | } else { |
| 95 | ret = false; |
| 96 | } |
| 97 | } |
| 98 | return ret; |
| 99 | } |
| 100 |
| 101 | bool GetDoubleFromJson(const Json::Value& in, double* out) { |
| 102 | bool ret; |
| 103 | if (!in.isString()) { |
| 104 | ret = in.isConvertibleTo(Json::realValue); |
| 105 | if (ret) { |
| 106 | *out = in.asDouble(); |
| 107 | } |
| 108 | } else { |
| 109 | double val; |
| 110 | const char* c_str = in.asCString(); |
| 111 | char* end_ptr; |
| 112 | errno = 0; |
| 113 | val = strtod(c_str, &end_ptr); |
| 114 | ret = (end_ptr != c_str && *end_ptr == '\0' && !errno); |
| 115 | *out = val; |
| 116 | } |
| 117 | return ret; |
| 118 | } |
| 119 |
| 120 | namespace { |
| 121 | template <typename T> |
| 122 | bool JsonArrayToVector(const Json::Value& value, |
| 123 | bool (*getter)(const Json::Value& in, T* out), |
| 124 | std::vector<T>* vec) { |
| 125 | vec->clear(); |
| 126 | if (!value.isArray()) { |
| 127 | return false; |
| 128 | } |
| 129 |
| 130 | for (Json::Value::ArrayIndex i = 0; i < value.size(); ++i) { |
| 131 | T val; |
| 132 | if (!getter(value[i], &val)) { |
| 133 | return false; |
| 134 | } |
| 135 | vec->push_back(val); |
| 136 | } |
| 137 |
| 138 | return true; |
| 139 | } |
| 140 | // Trivial getter helper |
| 141 | bool GetValueFromJson(const Json::Value& in, Json::Value* out) { |
| 142 | *out = in; |
| 143 | return true; |
| 144 | } |
| 145 | } // unnamed namespace |
| 146 |
| 147 | bool JsonArrayToValueVector(const Json::Value& in, |
| 148 | std::vector<Json::Value>* out) { |
| 149 | return JsonArrayToVector(in, GetValueFromJson, out); |
| 150 | } |
| 151 |
| 152 | bool JsonArrayToIntVector(const Json::Value& in, std::vector<int>* out) { |
| 153 | return JsonArrayToVector(in, GetIntFromJson, out); |
| 154 | } |
| 155 |
| 156 | bool JsonArrayToUIntVector(const Json::Value& in, |
| 157 | std::vector<unsigned int>* out) { |
| 158 | return JsonArrayToVector(in, GetUIntFromJson, out); |
| 159 | } |
| 160 |
| 161 | bool JsonArrayToStringVector(const Json::Value& in, |
| 162 | std::vector<std::string>* out) { |
| 163 | return JsonArrayToVector(in, GetStringFromJson, out); |
| 164 | } |
| 165 |
| 166 | bool JsonArrayToBoolVector(const Json::Value& in, std::vector<bool>* out) { |
| 167 | return JsonArrayToVector(in, GetBoolFromJson, out); |
| 168 | } |
| 169 |
| 170 | bool JsonArrayToDoubleVector(const Json::Value& in, std::vector<double>* out) { |
| 171 | return JsonArrayToVector(in, GetDoubleFromJson, out); |
| 172 | } |
| 173 |
| 174 | namespace { |
| 175 | template <typename T> |
| 176 | Json::Value VectorToJsonArray(const std::vector<T>& vec) { |
| 177 | Json::Value result(Json::arrayValue); |
| 178 | for (size_t i = 0; i < vec.size(); ++i) { |
| 179 | result.append(Json::Value(vec[i])); |
| 180 | } |
| 181 | return result; |
| 182 | } |
| 183 | } // unnamed namespace |
| 184 |
| 185 | Json::Value ValueVectorToJsonArray(const std::vector<Json::Value>& in) { |
| 186 | return VectorToJsonArray(in); |
| 187 | } |
| 188 |
| 189 | Json::Value IntVectorToJsonArray(const std::vector<int>& in) { |
| 190 | return VectorToJsonArray(in); |
| 191 | } |
| 192 |
| 193 | Json::Value UIntVectorToJsonArray(const std::vector<unsigned int>& in) { |
| 194 | return VectorToJsonArray(in); |
| 195 | } |
| 196 |
| 197 | Json::Value StringVectorToJsonArray(const std::vector<std::string>& in) { |
| 198 | return VectorToJsonArray(in); |
| 199 | } |
| 200 |
| 201 | Json::Value BoolVectorToJsonArray(const std::vector<bool>& in) { |
| 202 | return VectorToJsonArray(in); |
| 203 | } |
| 204 |
| 205 | Json::Value DoubleVectorToJsonArray(const std::vector<double>& in) { |
| 206 | return VectorToJsonArray(in); |
| 207 | } |
| 208 |
| 209 | bool GetValueFromJsonArray(const Json::Value& in, size_t n, Json::Value* out) { |
| 210 | if (!in.isArray() || !in.isValidIndex(static_cast<int>(n))) { |
| 211 | return false; |
| 212 | } |
| 213 |
| 214 | *out = in[static_cast<Json::Value::ArrayIndex>(n)]; |
| 215 | return true; |
| 216 | } |
| 217 |
| 218 | bool GetIntFromJsonArray(const Json::Value& in, size_t n, int* out) { |
| 219 | Json::Value x; |
| 220 | return GetValueFromJsonArray(in, n, &x) && GetIntFromJson(x, out); |
| 221 | } |
| 222 |
| 223 | bool GetUIntFromJsonArray(const Json::Value& in, size_t n, unsigned int* out) { |
| 224 | Json::Value x; |
| 225 | return GetValueFromJsonArray(in, n, &x) && GetUIntFromJson(x, out); |
| 226 | } |
| 227 |
| 228 | bool GetStringFromJsonArray(const Json::Value& in, size_t n, std::string* out) { |
| 229 | Json::Value x; |
| 230 | return GetValueFromJsonArray(in, n, &x) && GetStringFromJson(x, out); |
| 231 | } |
| 232 |
| 233 | bool GetBoolFromJsonArray(const Json::Value& in, size_t n, bool* out) { |
| 234 | Json::Value x; |
| 235 | return GetValueFromJsonArray(in, n, &x) && GetBoolFromJson(x, out); |
| 236 | } |
| 237 |
| 238 | bool GetDoubleFromJsonArray(const Json::Value& in, size_t n, double* out) { |
| 239 | Json::Value x; |
| 240 | return GetValueFromJsonArray(in, n, &x) && GetDoubleFromJson(x, out); |
| 241 | } |
| 242 |
| 243 | bool GetValueFromJsonObject(const Json::Value& in, |
| 244 | absl::string_view k, |
| 245 | Json::Value* out) { |
| 246 | std::string k_str(k); |
| 247 | if (!in.isObject() || !in.isMember(k_str)) { |
| 248 | return false; |
| 249 | } |
| 250 |
| 251 | *out = in[k_str]; |
| 252 | return true; |
| 253 | } |
| 254 |
| 255 | bool GetIntFromJsonObject(const Json::Value& in, |
| 256 | absl::string_view k, |
| 257 | int* out) { |
| 258 | Json::Value x; |
| 259 | return GetValueFromJsonObject(in, k, &x) && GetIntFromJson(x, out); |
| 260 | } |
| 261 |
| 262 | bool GetUIntFromJsonObject(const Json::Value& in, |
| 263 | absl::string_view k, |
| 264 | unsigned int* out) { |
| 265 | Json::Value x; |
| 266 | return GetValueFromJsonObject(in, k, &x) && GetUIntFromJson(x, out); |
| 267 | } |
| 268 |
| 269 | bool GetStringFromJsonObject(const Json::Value& in, |
| 270 | absl::string_view k, |
| 271 | std::string* out) { |
| 272 | Json::Value x; |
| 273 | return GetValueFromJsonObject(in, k, &x) && GetStringFromJson(x, out); |
| 274 | } |
| 275 |
| 276 | bool GetBoolFromJsonObject(const Json::Value& in, |
| 277 | absl::string_view k, |
| 278 | bool* out) { |
| 279 | Json::Value x; |
| 280 | return GetValueFromJsonObject(in, k, &x) && GetBoolFromJson(x, out); |
| 281 | } |
| 282 |
| 283 | bool GetDoubleFromJsonObject(const Json::Value& in, |
| 284 | absl::string_view k, |
| 285 | double* out) { |
| 286 | Json::Value x; |
| 287 | return GetValueFromJsonObject(in, k, &x) && GetDoubleFromJson(x, out); |
| 288 | } |
| 289 |
| 290 | std::string JsonValueToString(const Json::Value& json) { |
| 291 | Json::StreamWriterBuilder builder; |
| 292 | std::string output = Json::writeString(builder, json); |
| 293 | return output.substr(0, output.size() - 1); // trim trailing newline |
| 294 | } |
| 295 |
| 296 | } // namespace rtc |
| 297 |