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