json.h 3.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788
  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. #ifndef RTC_BASE_STRINGS_JSON_H_
  11. #define RTC_BASE_STRINGS_JSON_H_
  12. #include <string>
  13. #include <vector>
  14. #if !defined(WEBRTC_EXTERNAL_JSON)
  15. #include "json/json.h"
  16. #else
  17. #include "third_party/jsoncpp/json.h"
  18. #endif
  19. namespace rtc {
  20. ///////////////////////////////////////////////////////////////////////////////
  21. // JSON Helpers
  22. ///////////////////////////////////////////////////////////////////////////////
  23. // Robust conversion operators, better than the ones in JsonCpp.
  24. bool GetIntFromJson(const Json::Value& in, int* out);
  25. bool GetUIntFromJson(const Json::Value& in, unsigned int* out);
  26. bool GetStringFromJson(const Json::Value& in, std::string* out);
  27. bool GetBoolFromJson(const Json::Value& in, bool* out);
  28. bool GetDoubleFromJson(const Json::Value& in, double* out);
  29. // Pull values out of a JSON array.
  30. bool GetValueFromJsonArray(const Json::Value& in, size_t n, Json::Value* out);
  31. bool GetIntFromJsonArray(const Json::Value& in, size_t n, int* out);
  32. bool GetUIntFromJsonArray(const Json::Value& in, size_t n, unsigned int* out);
  33. bool GetStringFromJsonArray(const Json::Value& in, size_t n, std::string* out);
  34. bool GetBoolFromJsonArray(const Json::Value& in, size_t n, bool* out);
  35. bool GetDoubleFromJsonArray(const Json::Value& in, size_t n, double* out);
  36. // Convert json arrays to std::vector
  37. bool JsonArrayToValueVector(const Json::Value& in,
  38. std::vector<Json::Value>* out);
  39. bool JsonArrayToIntVector(const Json::Value& in, std::vector<int>* out);
  40. bool JsonArrayToUIntVector(const Json::Value& in,
  41. std::vector<unsigned int>* out);
  42. bool JsonArrayToStringVector(const Json::Value& in,
  43. std::vector<std::string>* out);
  44. bool JsonArrayToBoolVector(const Json::Value& in, std::vector<bool>* out);
  45. bool JsonArrayToDoubleVector(const Json::Value& in, std::vector<double>* out);
  46. // Convert std::vector to json array
  47. Json::Value ValueVectorToJsonArray(const std::vector<Json::Value>& in);
  48. Json::Value IntVectorToJsonArray(const std::vector<int>& in);
  49. Json::Value UIntVectorToJsonArray(const std::vector<unsigned int>& in);
  50. Json::Value StringVectorToJsonArray(const std::vector<std::string>& in);
  51. Json::Value BoolVectorToJsonArray(const std::vector<bool>& in);
  52. Json::Value DoubleVectorToJsonArray(const std::vector<double>& in);
  53. // Pull values out of a JSON object.
  54. bool GetValueFromJsonObject(const Json::Value& in,
  55. const std::string& k,
  56. Json::Value* out);
  57. bool GetIntFromJsonObject(const Json::Value& in,
  58. const std::string& k,
  59. int* out);
  60. bool GetUIntFromJsonObject(const Json::Value& in,
  61. const std::string& k,
  62. unsigned int* out);
  63. bool GetStringFromJsonObject(const Json::Value& in,
  64. const std::string& k,
  65. std::string* out);
  66. bool GetBoolFromJsonObject(const Json::Value& in,
  67. const std::string& k,
  68. bool* out);
  69. bool GetDoubleFromJsonObject(const Json::Value& in,
  70. const std::string& k,
  71. double* out);
  72. // Writes out a Json value as a string.
  73. std::string JsonValueToString(const Json::Value& json);
  74. } // namespace rtc
  75. #endif // RTC_BASE_STRINGS_JSON_H_