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.

313 lines
8.8 KiB

​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
  1. /*
  2. * libjingle
  3. * Copyright 2004--2005, Google Inc.
  4. *
  5. * Redistribution and use in source and binary forms, with or without
  6. * modification, are permitted provided that the following conditions are met:
  7. *
  8. * 1. Redistributions of source code must retain the above copyright notice,
  9. * this list of conditions and the following disclaimer.
  10. * 2. Redistributions in binary form must reproduce the above copyright notice,
  11. * this list of conditions and the following disclaimer in the documentation
  12. * and/or other materials provided with the distribution.
  13. * 3. The name of the author may not be used to endorse or promote products
  14. * derived from this software without specific prior written permission.
  15. *
  16. * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR IMPLIED
  17. * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
  18. * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO
  19. * EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  20. * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
  21. * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
  22. * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
  23. * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
  24. * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
  25. * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  26. */
  27. #include "talk/base/json.h"
  28. #include <errno.h>
  29. #include <climits>
  30. #include <cstdlib>
  31. #include <sstream>
  32. bool GetStringFromJson(const Json::Value& in, std::string* out) {
  33. if (!in.isString()) {
  34. std::ostringstream s;
  35. if (in.isBool()) {
  36. s << std::boolalpha << in.asBool();
  37. } else if (in.isInt()) {
  38. s << in.asInt();
  39. } else if (in.isUInt()) {
  40. s << in.asUInt();
  41. } else if (in.isDouble()) {
  42. s << in.asDouble();
  43. } else {
  44. return false;
  45. }
  46. *out = s.str();
  47. } else {
  48. *out = in.asString();
  49. }
  50. return true;
  51. }
  52. bool GetIntFromJson(const Json::Value& in, int* out) {
  53. bool ret;
  54. if (!in.isString()) {
  55. ret = in.isConvertibleTo(Json::intValue);
  56. if (ret) {
  57. *out = in.asInt();
  58. }
  59. } else {
  60. long val; // NOLINT
  61. const char* c_str = in.asCString();
  62. char* end_ptr;
  63. errno = 0;
  64. val = strtol(c_str, &end_ptr, 10); // NOLINT
  65. ret = (end_ptr != c_str && *end_ptr == '\0' && !errno &&
  66. val >= INT_MIN && val <= INT_MAX);
  67. *out = val;
  68. }
  69. return ret;
  70. }
  71. bool GetUIntFromJson(const Json::Value& in, unsigned int* out) {
  72. bool ret;
  73. if (!in.isString()) {
  74. ret = in.isConvertibleTo(Json::uintValue);
  75. if (ret) {
  76. *out = in.asUInt();
  77. }
  78. } else {
  79. unsigned long val; // NOLINT
  80. const char* c_str = in.asCString();
  81. char* end_ptr;
  82. errno = 0;
  83. val = strtoul(c_str, &end_ptr, 10); // NOLINT
  84. ret = (end_ptr != c_str && *end_ptr == '\0' && !errno &&
  85. val <= UINT_MAX);
  86. *out = val;
  87. }
  88. return ret;
  89. }
  90. bool GetBoolFromJson(const Json::Value& in, bool* out) {
  91. bool ret;
  92. if (!in.isString()) {
  93. ret = in.isConvertibleTo(Json::booleanValue);
  94. if (ret) {
  95. *out = in.asBool();
  96. }
  97. } else {
  98. if (in.asString() == "true") {
  99. *out = true;
  100. ret = true;
  101. } else if (in.asString() == "false") {
  102. *out = false;
  103. ret = true;
  104. } else {
  105. ret = false;
  106. }
  107. }
  108. return ret;
  109. }
  110. bool GetDoubleFromJson(const Json::Value& in, double* out) {
  111. bool ret;
  112. if (!in.isString()) {
  113. ret = in.isConvertibleTo(Json::realValue);
  114. if (ret) {
  115. *out = in.asDouble();
  116. }
  117. } else {
  118. double val;
  119. const char* c_str = in.asCString();
  120. char* end_ptr;
  121. errno = 0;
  122. val = strtod(c_str, &end_ptr);
  123. ret = (end_ptr != c_str && *end_ptr == '\0' && !errno);
  124. *out = val;
  125. }
  126. return ret;
  127. }
  128. namespace {
  129. template<typename T>
  130. bool JsonArrayToVector(const Json::Value& value,
  131. bool (*getter)(const Json::Value& in, T* out),
  132. std::vector<T> *vec) {
  133. vec->clear();
  134. if (!value.isArray()) {
  135. return false;
  136. }
  137. for (Json::Value::ArrayIndex i = 0; i < value.size(); ++i) {
  138. T val;
  139. if (!getter(value[i], &val)) {
  140. return false;
  141. }
  142. vec->push_back(val);
  143. }
  144. return true;
  145. }
  146. // Trivial getter helper
  147. bool GetValueFromJson(const Json::Value& in, Json::Value* out) {
  148. *out = in;
  149. return true;
  150. }
  151. } // unnamed namespace
  152. bool JsonArrayToValueVector(const Json::Value& in,
  153. std::vector<Json::Value>* out) {
  154. return JsonArrayToVector(in, GetValueFromJson, out);
  155. }
  156. bool JsonArrayToIntVector(const Json::Value& in,
  157. std::vector<int>* out) {
  158. return JsonArrayToVector(in, GetIntFromJson, out);
  159. }
  160. bool JsonArrayToUIntVector(const Json::Value& in,
  161. std::vector<unsigned int>* out) {
  162. return JsonArrayToVector(in, GetUIntFromJson, out);
  163. }
  164. bool JsonArrayToStringVector(const Json::Value& in,
  165. std::vector<std::string>* out) {
  166. return JsonArrayToVector(in, GetStringFromJson, out);
  167. }
  168. bool JsonArrayToBoolVector(const Json::Value& in,
  169. std::vector<bool>* out) {
  170. return JsonArrayToVector(in, GetBoolFromJson, out);
  171. }
  172. bool JsonArrayToDoubleVector(const Json::Value& in,
  173. std::vector<double>* out) {
  174. return JsonArrayToVector(in, GetDoubleFromJson, out);
  175. }
  176. namespace {
  177. template<typename T>
  178. Json::Value VectorToJsonArray(const std::vector<T>& vec) {
  179. Json::Value result(Json::arrayValue);
  180. for (size_t i = 0; i < vec.size(); ++i) {
  181. result.append(Json::Value(vec[i]));
  182. }
  183. return result;
  184. }
  185. } // unnamed namespace
  186. Json::Value ValueVectorToJsonArray(const std::vector<Json::Value>& in) {
  187. return VectorToJsonArray(in);
  188. }
  189. Json::Value IntVectorToJsonArray(const std::vector<int>& in) {
  190. return VectorToJsonArray(in);
  191. }
  192. Json::Value UIntVectorToJsonArray(const std::vector<unsigned int>& in) {
  193. return VectorToJsonArray(in);
  194. }
  195. Json::Value StringVectorToJsonArray(const std::vector<std::string>& in) {
  196. return VectorToJsonArray(in);
  197. }
  198. Json::Value BoolVectorToJsonArray(const std::vector<bool>& in) {
  199. return VectorToJsonArray(in);
  200. }
  201. Json::Value DoubleVectorToJsonArray(const std::vector<double>& in) {
  202. return VectorToJsonArray(in);
  203. }
  204. bool GetValueFromJsonArray(const Json::Value& in, size_t n,
  205. Json::Value* out) {
  206. if (!in.isArray() || !in.isValidIndex(static_cast<int>(n))) {
  207. return false;
  208. }
  209. *out = in[static_cast<Json::Value::ArrayIndex>(n)];
  210. return true;
  211. }
  212. bool GetIntFromJsonArray(const Json::Value& in, size_t n,
  213. int* out) {
  214. Json::Value x;
  215. return GetValueFromJsonArray(in, n, &x) && GetIntFromJson(x, out);
  216. }
  217. bool GetUIntFromJsonArray(const Json::Value& in, size_t n,
  218. unsigned int* out) {
  219. Json::Value x;
  220. return GetValueFromJsonArray(in, n, &x) && GetUIntFromJson(x, out);
  221. }
  222. bool GetStringFromJsonArray(const Json::Value& in, size_t n,
  223. std::string* out) {
  224. Json::Value x;
  225. return GetValueFromJsonArray(in, n, &x) && GetStringFromJson(x, out);
  226. }
  227. bool GetBoolFromJsonArray(const Json::Value& in, size_t n,
  228. bool* out) {
  229. Json::Value x;
  230. return GetValueFromJsonArray(in, n, &x) && GetBoolFromJson(x, out);
  231. }
  232. bool GetDoubleFromJsonArray(const Json::Value& in, size_t n,
  233. double* out) {
  234. Json::Value x;
  235. return GetValueFromJsonArray(in, n, &x) && GetDoubleFromJson(x, out);
  236. }
  237. bool GetValueFromJsonObject(const Json::Value& in, const std::string& k,
  238. Json::Value* out) {
  239. if (!in.isObject() || !in.isMember(k)) {
  240. return false;
  241. }
  242. *out = in[k];
  243. return true;
  244. }
  245. bool GetIntFromJsonObject(const Json::Value& in, const std::string& k,
  246. int* out) {
  247. Json::Value x;
  248. return GetValueFromJsonObject(in, k, &x) && GetIntFromJson(x, out);
  249. }
  250. bool GetUIntFromJsonObject(const Json::Value& in, const std::string& k,
  251. unsigned int* out) {
  252. Json::Value x;
  253. return GetValueFromJsonObject(in, k, &x) && GetUIntFromJson(x, out);
  254. }
  255. bool GetStringFromJsonObject(const Json::Value& in, const std::string& k,
  256. std::string* out) {
  257. Json::Value x;
  258. return GetValueFromJsonObject(in, k, &x) && GetStringFromJson(x, out);
  259. }
  260. bool GetBoolFromJsonObject(const Json::Value& in, const std::string& k,
  261. bool* out) {
  262. Json::Value x;
  263. return GetValueFromJsonObject(in, k, &x) && GetBoolFromJson(x, out);
  264. }
  265. bool GetDoubleFromJsonObject(const Json::Value& in, const std::string& k,
  266. double* out) {
  267. Json::Value x;
  268. return GetValueFromJsonObject(in, k, &x) && GetDoubleFromJson(x, out);
  269. }
  270. std::string JsonValueToString(const Json::Value& json) {
  271. Json::FastWriter w;
  272. std::string value = w.write(json);
  273. return value.substr(0, value.size() - 1); // trim trailing newline
  274. }