blob: 618cb71b04005facefb9ac755a9c63590cca2b9e [file] [log] [blame]
henrike@webrtc.orgf0488722014-05-13 18:00:26 +00001/*
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 Zackrissonb45bdb52018-10-02 16:25:59 +020011#ifndef RTC_BASE_STRINGS_JSON_H_
12#define RTC_BASE_STRINGS_JSON_H_
henrike@webrtc.orgf0488722014-05-13 18:00:26 +000013
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +020014#include <string>
15#include <vector>
henrike@webrtc.orgf0488722014-05-13 18:00:26 +000016
Ali Tofigh7fa90572022-03-17 15:47:49 +010017#include "absl/strings/string_view.h"
18
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +020019#if !defined(WEBRTC_EXTERNAL_JSON)
20#include "json/json.h"
21#else
22#include "third_party/jsoncpp/json.h"
23#endif
24
25namespace rtc {
26
27///////////////////////////////////////////////////////////////////////////////
28// JSON Helpers
29///////////////////////////////////////////////////////////////////////////////
30
31// Robust conversion operators, better than the ones in JsonCpp.
32bool GetIntFromJson(const Json::Value& in, int* out);
33bool GetUIntFromJson(const Json::Value& in, unsigned int* out);
34bool GetStringFromJson(const Json::Value& in, std::string* out);
35bool GetBoolFromJson(const Json::Value& in, bool* out);
36bool GetDoubleFromJson(const Json::Value& in, double* out);
37
38// Pull values out of a JSON array.
Yves Gerey665174f2018-06-19 15:03:05 +020039bool GetValueFromJsonArray(const Json::Value& in, size_t n, Json::Value* out);
40bool GetIntFromJsonArray(const Json::Value& in, size_t n, int* out);
41bool GetUIntFromJsonArray(const Json::Value& in, size_t n, unsigned int* out);
42bool GetStringFromJsonArray(const Json::Value& in, size_t n, std::string* out);
43bool GetBoolFromJsonArray(const Json::Value& in, size_t n, bool* out);
44bool GetDoubleFromJsonArray(const Json::Value& in, size_t n, double* out);
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +020045
46// Convert json arrays to std::vector
47bool JsonArrayToValueVector(const Json::Value& in,
48 std::vector<Json::Value>* out);
Yves Gerey665174f2018-06-19 15:03:05 +020049bool JsonArrayToIntVector(const Json::Value& in, std::vector<int>* out);
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +020050bool JsonArrayToUIntVector(const Json::Value& in,
51 std::vector<unsigned int>* out);
52bool JsonArrayToStringVector(const Json::Value& in,
53 std::vector<std::string>* out);
Yves Gerey665174f2018-06-19 15:03:05 +020054bool JsonArrayToBoolVector(const Json::Value& in, std::vector<bool>* out);
55bool JsonArrayToDoubleVector(const Json::Value& in, std::vector<double>* out);
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +020056
57// Convert std::vector to json array
58Json::Value ValueVectorToJsonArray(const std::vector<Json::Value>& in);
59Json::Value IntVectorToJsonArray(const std::vector<int>& in);
60Json::Value UIntVectorToJsonArray(const std::vector<unsigned int>& in);
61Json::Value StringVectorToJsonArray(const std::vector<std::string>& in);
62Json::Value BoolVectorToJsonArray(const std::vector<bool>& in);
63Json::Value DoubleVectorToJsonArray(const std::vector<double>& in);
64
65// Pull values out of a JSON object.
Yves Gerey665174f2018-06-19 15:03:05 +020066bool GetValueFromJsonObject(const Json::Value& in,
Ali Tofigh7fa90572022-03-17 15:47:49 +010067 absl::string_view k,
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +020068 Json::Value* out);
Ali Tofigh7fa90572022-03-17 15:47:49 +010069bool GetIntFromJsonObject(const Json::Value& in, absl::string_view k, int* out);
Yves Gerey665174f2018-06-19 15:03:05 +020070bool GetUIntFromJsonObject(const Json::Value& in,
Ali Tofigh7fa90572022-03-17 15:47:49 +010071 absl::string_view k,
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +020072 unsigned int* out);
Yves Gerey665174f2018-06-19 15:03:05 +020073bool GetStringFromJsonObject(const Json::Value& in,
Ali Tofigh7fa90572022-03-17 15:47:49 +010074 absl::string_view k,
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +020075 std::string* out);
Yves Gerey665174f2018-06-19 15:03:05 +020076bool GetBoolFromJsonObject(const Json::Value& in,
Ali Tofigh7fa90572022-03-17 15:47:49 +010077 absl::string_view k,
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +020078 bool* out);
Yves Gerey665174f2018-06-19 15:03:05 +020079bool GetDoubleFromJsonObject(const Json::Value& in,
Ali Tofigh7fa90572022-03-17 15:47:49 +010080 absl::string_view k,
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +020081 double* out);
82
83// Writes out a Json value as a string.
84std::string JsonValueToString(const Json::Value& json);
85
86} // namespace rtc
Thiago Farinacb76b892015-04-02 09:59:15 +000087
Sam Zackrissonb45bdb52018-10-02 16:25:59 +020088#endif // RTC_BASE_STRINGS_JSON_H_