blob: 5cf153c926d82d700ef5e20419cab71230b310ce [file] [log] [blame]
henrike@webrtc.orgf0488722014-05-13 18:00:261/*
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 14:25:5911#include "rtc_base/strings/json.h"
henrike@webrtc.orgf0488722014-05-13 18:00:2612
13#include <errno.h>
14#include <limits.h>
15#include <stdlib.h>
16
Ali Tofigh7fa90572022-03-17 14:47:4917#include "absl/strings/string_view.h"
Steve Anton10542f22019-01-11 17:11:0018#include "rtc_base/string_encode.h"
henrike@webrtc.orgf0488722014-05-13 18:00:2619
Thiago Farinacb76b892015-04-02 09:59:1520namespace rtc {
21
henrike@webrtc.orgf0488722014-05-13 18:00:2622bool GetStringFromJson(const Json::Value& in, std::string* out) {
23 if (!in.isString()) {
henrike@webrtc.orgf0488722014-05-13 18:00:2624 if (in.isBool()) {
Jonas Olsson941a07c2018-09-13 08:07:0725 *out = rtc::ToString(in.asBool());
henrike@webrtc.orgf0488722014-05-13 18:00:2626 } else if (in.isInt()) {
Jonas Olsson941a07c2018-09-13 08:07:0727 *out = rtc::ToString(in.asInt());
henrike@webrtc.orgf0488722014-05-13 18:00:2628 } else if (in.isUInt()) {
Jonas Olsson941a07c2018-09-13 08:07:0729 *out = rtc::ToString(in.asUInt());
henrike@webrtc.orgf0488722014-05-13 18:00:2630 } else if (in.isDouble()) {
Jonas Olsson941a07c2018-09-13 08:07:0731 *out = rtc::ToString(in.asDouble());
henrike@webrtc.orgf0488722014-05-13 18:00:2632 } else {
33 return false;
34 }
henrike@webrtc.orgf0488722014-05-13 18:00:2635 } else {
36 *out = in.asString();
37 }
38 return true;
39}
40
41bool GetIntFromJson(const Json::Value& in, int* out) {
42 bool ret;
43 if (!in.isString()) {
44 ret = in.isConvertibleTo(Json::intValue);
45 if (ret) {
46 *out = in.asInt();
47 }
48 } else {
49 long val; // NOLINT
50 const char* c_str = in.asCString();
51 char* end_ptr;
52 errno = 0;
53 val = strtol(c_str, &end_ptr, 10); // NOLINT
Yves Gerey665174f2018-06-19 13:03:0554 ret = (end_ptr != c_str && *end_ptr == '\0' && !errno && val >= INT_MIN &&
55 val <= INT_MAX);
henrike@webrtc.orgf0488722014-05-13 18:00:2656 *out = val;
57 }
58 return ret;
59}
60
61bool GetUIntFromJson(const Json::Value& in, unsigned int* out) {
62 bool ret;
63 if (!in.isString()) {
64 ret = in.isConvertibleTo(Json::uintValue);
65 if (ret) {
66 *out = in.asUInt();
67 }
68 } else {
69 unsigned long val; // NOLINT
70 const char* c_str = in.asCString();
71 char* end_ptr;
72 errno = 0;
73 val = strtoul(c_str, &end_ptr, 10); // NOLINT
Yves Gerey665174f2018-06-19 13:03:0574 ret = (end_ptr != c_str && *end_ptr == '\0' && !errno && val <= UINT_MAX);
henrike@webrtc.orgf0488722014-05-13 18:00:2675 *out = val;
76 }
77 return ret;
78}
79
80bool GetBoolFromJson(const Json::Value& in, bool* out) {
81 bool ret;
82 if (!in.isString()) {
83 ret = in.isConvertibleTo(Json::booleanValue);
84 if (ret) {
85 *out = in.asBool();
86 }
87 } else {
88 if (in.asString() == "true") {
89 *out = true;
90 ret = true;
91 } else if (in.asString() == "false") {
92 *out = false;
93 ret = true;
94 } else {
95 ret = false;
96 }
97 }
98 return ret;
99}
100
101bool GetDoubleFromJson(const Json::Value& in, double* out) {
102 bool ret;
103 if (!in.isString()) {
104 ret = in.isConvertibleTo(Json::realValue);
105 if (ret) {
106 *out = in.asDouble();
107 }
108 } else {
109 double val;
110 const char* c_str = in.asCString();
111 char* end_ptr;
112 errno = 0;
113 val = strtod(c_str, &end_ptr);
114 ret = (end_ptr != c_str && *end_ptr == '\0' && !errno);
115 *out = val;
116 }
117 return ret;
118}
119
120namespace {
Yves Gerey665174f2018-06-19 13:03:05121template <typename T>
henrike@webrtc.orgf0488722014-05-13 18:00:26122bool JsonArrayToVector(const Json::Value& value,
123 bool (*getter)(const Json::Value& in, T* out),
Yves Gerey665174f2018-06-19 13:03:05124 std::vector<T>* vec) {
henrike@webrtc.orgf0488722014-05-13 18:00:26125 vec->clear();
126 if (!value.isArray()) {
127 return false;
128 }
129
130 for (Json::Value::ArrayIndex i = 0; i < value.size(); ++i) {
131 T val;
132 if (!getter(value[i], &val)) {
133 return false;
134 }
135 vec->push_back(val);
136 }
137
138 return true;
139}
140// Trivial getter helper
141bool GetValueFromJson(const Json::Value& in, Json::Value* out) {
142 *out = in;
143 return true;
144}
145} // unnamed namespace
146
147bool JsonArrayToValueVector(const Json::Value& in,
148 std::vector<Json::Value>* out) {
149 return JsonArrayToVector(in, GetValueFromJson, out);
150}
151
Yves Gerey665174f2018-06-19 13:03:05152bool JsonArrayToIntVector(const Json::Value& in, std::vector<int>* out) {
henrike@webrtc.orgf0488722014-05-13 18:00:26153 return JsonArrayToVector(in, GetIntFromJson, out);
154}
155
156bool JsonArrayToUIntVector(const Json::Value& in,
157 std::vector<unsigned int>* out) {
158 return JsonArrayToVector(in, GetUIntFromJson, out);
159}
160
161bool JsonArrayToStringVector(const Json::Value& in,
162 std::vector<std::string>* out) {
163 return JsonArrayToVector(in, GetStringFromJson, out);
164}
165
Yves Gerey665174f2018-06-19 13:03:05166bool JsonArrayToBoolVector(const Json::Value& in, std::vector<bool>* out) {
henrike@webrtc.orgf0488722014-05-13 18:00:26167 return JsonArrayToVector(in, GetBoolFromJson, out);
168}
169
Yves Gerey665174f2018-06-19 13:03:05170bool JsonArrayToDoubleVector(const Json::Value& in, std::vector<double>* out) {
henrike@webrtc.orgf0488722014-05-13 18:00:26171 return JsonArrayToVector(in, GetDoubleFromJson, out);
172}
173
174namespace {
Yves Gerey665174f2018-06-19 13:03:05175template <typename T>
henrike@webrtc.orgf0488722014-05-13 18:00:26176Json::Value VectorToJsonArray(const std::vector<T>& vec) {
177 Json::Value result(Json::arrayValue);
178 for (size_t i = 0; i < vec.size(); ++i) {
179 result.append(Json::Value(vec[i]));
180 }
181 return result;
182}
183} // unnamed namespace
184
185Json::Value ValueVectorToJsonArray(const std::vector<Json::Value>& in) {
186 return VectorToJsonArray(in);
187}
188
189Json::Value IntVectorToJsonArray(const std::vector<int>& in) {
190 return VectorToJsonArray(in);
191}
192
193Json::Value UIntVectorToJsonArray(const std::vector<unsigned int>& in) {
194 return VectorToJsonArray(in);
195}
196
197Json::Value StringVectorToJsonArray(const std::vector<std::string>& in) {
198 return VectorToJsonArray(in);
199}
200
201Json::Value BoolVectorToJsonArray(const std::vector<bool>& in) {
202 return VectorToJsonArray(in);
203}
204
205Json::Value DoubleVectorToJsonArray(const std::vector<double>& in) {
206 return VectorToJsonArray(in);
207}
208
Yves Gerey665174f2018-06-19 13:03:05209bool GetValueFromJsonArray(const Json::Value& in, size_t n, Json::Value* out) {
henrike@webrtc.orgf0488722014-05-13 18:00:26210 if (!in.isArray() || !in.isValidIndex(static_cast<int>(n))) {
211 return false;
212 }
213
214 *out = in[static_cast<Json::Value::ArrayIndex>(n)];
215 return true;
216}
217
Yves Gerey665174f2018-06-19 13:03:05218bool GetIntFromJsonArray(const Json::Value& in, size_t n, int* out) {
henrike@webrtc.orgf0488722014-05-13 18:00:26219 Json::Value x;
220 return GetValueFromJsonArray(in, n, &x) && GetIntFromJson(x, out);
221}
222
Yves Gerey665174f2018-06-19 13:03:05223bool GetUIntFromJsonArray(const Json::Value& in, size_t n, unsigned int* out) {
henrike@webrtc.orgf0488722014-05-13 18:00:26224 Json::Value x;
225 return GetValueFromJsonArray(in, n, &x) && GetUIntFromJson(x, out);
226}
227
Yves Gerey665174f2018-06-19 13:03:05228bool GetStringFromJsonArray(const Json::Value& in, size_t n, std::string* out) {
henrike@webrtc.orgf0488722014-05-13 18:00:26229 Json::Value x;
230 return GetValueFromJsonArray(in, n, &x) && GetStringFromJson(x, out);
231}
232
Yves Gerey665174f2018-06-19 13:03:05233bool GetBoolFromJsonArray(const Json::Value& in, size_t n, bool* out) {
henrike@webrtc.orgf0488722014-05-13 18:00:26234 Json::Value x;
235 return GetValueFromJsonArray(in, n, &x) && GetBoolFromJson(x, out);
236}
237
Yves Gerey665174f2018-06-19 13:03:05238bool GetDoubleFromJsonArray(const Json::Value& in, size_t n, double* out) {
henrike@webrtc.orgf0488722014-05-13 18:00:26239 Json::Value x;
240 return GetValueFromJsonArray(in, n, &x) && GetDoubleFromJson(x, out);
241}
242
Yves Gerey665174f2018-06-19 13:03:05243bool GetValueFromJsonObject(const Json::Value& in,
Ali Tofigh7fa90572022-03-17 14:47:49244 absl::string_view k,
henrike@webrtc.orgf0488722014-05-13 18:00:26245 Json::Value* out) {
Ali Tofigh98bfd992022-07-22 20:10:49246 std::string k_str(k);
Ali Tofigh7fa90572022-03-17 14:47:49247 if (!in.isObject() || !in.isMember(k_str)) {
henrike@webrtc.orgf0488722014-05-13 18:00:26248 return false;
249 }
250
Ali Tofigh7fa90572022-03-17 14:47:49251 *out = in[k_str];
henrike@webrtc.orgf0488722014-05-13 18:00:26252 return true;
253}
254
Yves Gerey665174f2018-06-19 13:03:05255bool GetIntFromJsonObject(const Json::Value& in,
Ali Tofigh7fa90572022-03-17 14:47:49256 absl::string_view k,
henrike@webrtc.orgf0488722014-05-13 18:00:26257 int* out) {
258 Json::Value x;
259 return GetValueFromJsonObject(in, k, &x) && GetIntFromJson(x, out);
260}
261
Yves Gerey665174f2018-06-19 13:03:05262bool GetUIntFromJsonObject(const Json::Value& in,
Ali Tofigh7fa90572022-03-17 14:47:49263 absl::string_view k,
Yves Gerey665174f2018-06-19 13:03:05264 unsigned int* out) {
henrike@webrtc.orgf0488722014-05-13 18:00:26265 Json::Value x;
266 return GetValueFromJsonObject(in, k, &x) && GetUIntFromJson(x, out);
267}
268
Yves Gerey665174f2018-06-19 13:03:05269bool GetStringFromJsonObject(const Json::Value& in,
Ali Tofigh7fa90572022-03-17 14:47:49270 absl::string_view k,
Yves Gerey665174f2018-06-19 13:03:05271 std::string* out) {
henrike@webrtc.orgf0488722014-05-13 18:00:26272 Json::Value x;
273 return GetValueFromJsonObject(in, k, &x) && GetStringFromJson(x, out);
274}
275
Yves Gerey665174f2018-06-19 13:03:05276bool GetBoolFromJsonObject(const Json::Value& in,
Ali Tofigh7fa90572022-03-17 14:47:49277 absl::string_view k,
henrike@webrtc.orgf0488722014-05-13 18:00:26278 bool* out) {
279 Json::Value x;
280 return GetValueFromJsonObject(in, k, &x) && GetBoolFromJson(x, out);
281}
282
Yves Gerey665174f2018-06-19 13:03:05283bool GetDoubleFromJsonObject(const Json::Value& in,
Ali Tofigh7fa90572022-03-17 14:47:49284 absl::string_view k,
henrike@webrtc.orgf0488722014-05-13 18:00:26285 double* out) {
286 Json::Value x;
287 return GetValueFromJsonObject(in, k, &x) && GetDoubleFromJson(x, out);
288}
289
290std::string JsonValueToString(const Json::Value& json) {
Mirko Bonadeie99f6872021-06-24 13:24:59291 Json::StreamWriterBuilder builder;
292 std::string output = Json::writeString(builder, json);
293 return output.substr(0, output.size() - 1); // trim trailing newline
henrike@webrtc.orgf0488722014-05-13 18:00:26294}
Thiago Farinacb76b892015-04-02 09:59:15295
296} // namespace rtc