henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 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 | |
Sam Zackrisson | b45bdb5 | 2018-10-02 16:25:59 +0200 | [diff] [blame] | 11 | #ifndef RTC_BASE_STRINGS_JSON_H_ |
| 12 | #define RTC_BASE_STRINGS_JSON_H_ |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 13 | |
Henrik Kjellander | ec78f1c | 2017-06-29 07:52:50 +0200 | [diff] [blame] | 14 | #include <string> |
| 15 | #include <vector> |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 16 | |
Henrik Kjellander | ec78f1c | 2017-06-29 07:52:50 +0200 | [diff] [blame] | 17 | #if !defined(WEBRTC_EXTERNAL_JSON) |
| 18 | #include "json/json.h" |
| 19 | #else |
| 20 | #include "third_party/jsoncpp/json.h" |
| 21 | #endif |
| 22 | |
| 23 | namespace rtc { |
| 24 | |
| 25 | /////////////////////////////////////////////////////////////////////////////// |
| 26 | // JSON Helpers |
| 27 | /////////////////////////////////////////////////////////////////////////////// |
| 28 | |
| 29 | // Robust conversion operators, better than the ones in JsonCpp. |
| 30 | bool GetIntFromJson(const Json::Value& in, int* out); |
| 31 | bool GetUIntFromJson(const Json::Value& in, unsigned int* out); |
| 32 | bool GetStringFromJson(const Json::Value& in, std::string* out); |
| 33 | bool GetBoolFromJson(const Json::Value& in, bool* out); |
| 34 | bool GetDoubleFromJson(const Json::Value& in, double* out); |
| 35 | |
| 36 | // Pull values out of a JSON array. |
Yves Gerey | 665174f | 2018-06-19 15:03:05 +0200 | [diff] [blame] | 37 | bool GetValueFromJsonArray(const Json::Value& in, size_t n, Json::Value* out); |
| 38 | bool GetIntFromJsonArray(const Json::Value& in, size_t n, int* out); |
| 39 | bool GetUIntFromJsonArray(const Json::Value& in, size_t n, unsigned int* out); |
| 40 | bool GetStringFromJsonArray(const Json::Value& in, size_t n, std::string* out); |
| 41 | bool GetBoolFromJsonArray(const Json::Value& in, size_t n, bool* out); |
| 42 | bool GetDoubleFromJsonArray(const Json::Value& in, size_t n, double* out); |
Henrik Kjellander | ec78f1c | 2017-06-29 07:52:50 +0200 | [diff] [blame] | 43 | |
| 44 | // Convert json arrays to std::vector |
| 45 | bool JsonArrayToValueVector(const Json::Value& in, |
| 46 | std::vector<Json::Value>* out); |
Yves Gerey | 665174f | 2018-06-19 15:03:05 +0200 | [diff] [blame] | 47 | bool JsonArrayToIntVector(const Json::Value& in, std::vector<int>* out); |
Henrik Kjellander | ec78f1c | 2017-06-29 07:52:50 +0200 | [diff] [blame] | 48 | bool JsonArrayToUIntVector(const Json::Value& in, |
| 49 | std::vector<unsigned int>* out); |
| 50 | bool JsonArrayToStringVector(const Json::Value& in, |
| 51 | std::vector<std::string>* out); |
Yves Gerey | 665174f | 2018-06-19 15:03:05 +0200 | [diff] [blame] | 52 | bool JsonArrayToBoolVector(const Json::Value& in, std::vector<bool>* out); |
| 53 | bool JsonArrayToDoubleVector(const Json::Value& in, std::vector<double>* out); |
Henrik Kjellander | ec78f1c | 2017-06-29 07:52:50 +0200 | [diff] [blame] | 54 | |
| 55 | // Convert std::vector to json array |
| 56 | Json::Value ValueVectorToJsonArray(const std::vector<Json::Value>& in); |
| 57 | Json::Value IntVectorToJsonArray(const std::vector<int>& in); |
| 58 | Json::Value UIntVectorToJsonArray(const std::vector<unsigned int>& in); |
| 59 | Json::Value StringVectorToJsonArray(const std::vector<std::string>& in); |
| 60 | Json::Value BoolVectorToJsonArray(const std::vector<bool>& in); |
| 61 | Json::Value DoubleVectorToJsonArray(const std::vector<double>& in); |
| 62 | |
| 63 | // Pull values out of a JSON object. |
Yves Gerey | 665174f | 2018-06-19 15:03:05 +0200 | [diff] [blame] | 64 | bool GetValueFromJsonObject(const Json::Value& in, |
| 65 | const std::string& k, |
Henrik Kjellander | ec78f1c | 2017-06-29 07:52:50 +0200 | [diff] [blame] | 66 | Json::Value* out); |
Yves Gerey | 665174f | 2018-06-19 15:03:05 +0200 | [diff] [blame] | 67 | bool GetIntFromJsonObject(const Json::Value& in, |
| 68 | const std::string& k, |
Henrik Kjellander | ec78f1c | 2017-06-29 07:52:50 +0200 | [diff] [blame] | 69 | int* out); |
Yves Gerey | 665174f | 2018-06-19 15:03:05 +0200 | [diff] [blame] | 70 | bool GetUIntFromJsonObject(const Json::Value& in, |
| 71 | const std::string& k, |
Henrik Kjellander | ec78f1c | 2017-06-29 07:52:50 +0200 | [diff] [blame] | 72 | unsigned int* out); |
Yves Gerey | 665174f | 2018-06-19 15:03:05 +0200 | [diff] [blame] | 73 | bool GetStringFromJsonObject(const Json::Value& in, |
| 74 | const std::string& k, |
Henrik Kjellander | ec78f1c | 2017-06-29 07:52:50 +0200 | [diff] [blame] | 75 | std::string* out); |
Yves Gerey | 665174f | 2018-06-19 15:03:05 +0200 | [diff] [blame] | 76 | bool GetBoolFromJsonObject(const Json::Value& in, |
| 77 | const std::string& k, |
Henrik Kjellander | ec78f1c | 2017-06-29 07:52:50 +0200 | [diff] [blame] | 78 | bool* out); |
Yves Gerey | 665174f | 2018-06-19 15:03:05 +0200 | [diff] [blame] | 79 | bool GetDoubleFromJsonObject(const Json::Value& in, |
| 80 | const std::string& k, |
Henrik Kjellander | ec78f1c | 2017-06-29 07:52:50 +0200 | [diff] [blame] | 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 |
Thiago Farina | cb76b89 | 2015-04-02 09:59:15 +0000 | [diff] [blame] | 87 | |
Sam Zackrisson | b45bdb5 | 2018-10-02 16:25:59 +0200 | [diff] [blame] | 88 | #endif // RTC_BASE_STRINGS_JSON_H_ |