You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

294 lines
7.8 KiB

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