blob: cc40bd678de9d115c0ec30fa17e21182214f74b0 [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
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020011#include "rtc_base/json.h"
henrike@webrtc.orgf0488722014-05-13 18:00:26 +000012
13#include <errno.h>
14#include <limits.h>
15#include <stdlib.h>
16
17#include <sstream>
18
Thiago Farinacb76b892015-04-02 09:59:15 +000019namespace rtc {
20
henrike@webrtc.orgf0488722014-05-13 18:00:26 +000021bool GetStringFromJson(const Json::Value& in, std::string* out) {
22 if (!in.isString()) {
23 std::ostringstream s;
24 if (in.isBool()) {
25 s << std::boolalpha << in.asBool();
26 } else if (in.isInt()) {
27 s << in.asInt();
28 } else if (in.isUInt()) {
29 s << in.asUInt();
30 } else if (in.isDouble()) {
31 s << in.asDouble();
32 } else {
33 return false;
34 }
35 *out = s.str();
36 } else {
37 *out = in.asString();
38 }
39 return true;
40}
41
42bool GetIntFromJson(const Json::Value& in, int* out) {
43 bool ret;
44 if (!in.isString()) {
45 ret = in.isConvertibleTo(Json::intValue);
46 if (ret) {
47 *out = in.asInt();
48 }
49 } else {
50 long val; // NOLINT
51 const char* c_str = in.asCString();
52 char* end_ptr;
53 errno = 0;
54 val = strtol(c_str, &end_ptr, 10); // NOLINT
Yves Gerey665174f2018-06-19 15:03:05 +020055 ret = (end_ptr != c_str && *end_ptr == '\0' && !errno && val >= INT_MIN &&
56 val <= INT_MAX);
henrike@webrtc.orgf0488722014-05-13 18:00:26 +000057 *out = val;
58 }
59 return ret;
60}
61
62bool GetUIntFromJson(const Json::Value& in, unsigned int* out) {
63 bool ret;
64 if (!in.isString()) {
65 ret = in.isConvertibleTo(Json::uintValue);
66 if (ret) {
67 *out = in.asUInt();
68 }
69 } else {
70 unsigned long val; // NOLINT
71 const char* c_str = in.asCString();
72 char* end_ptr;
73 errno = 0;
74 val = strtoul(c_str, &end_ptr, 10); // NOLINT
Yves Gerey665174f2018-06-19 15:03:05 +020075 ret = (end_ptr != c_str && *end_ptr == '\0' && !errno && val <= UINT_MAX);
henrike@webrtc.orgf0488722014-05-13 18:00:26 +000076 *out = val;
77 }
78 return ret;
79}
80
81bool GetBoolFromJson(const Json::Value& in, bool* out) {
82 bool ret;
83 if (!in.isString()) {
84 ret = in.isConvertibleTo(Json::booleanValue);
85 if (ret) {
86 *out = in.asBool();
87 }
88 } else {
89 if (in.asString() == "true") {
90 *out = true;
91 ret = true;
92 } else if (in.asString() == "false") {
93 *out = false;
94 ret = true;
95 } else {
96 ret = false;
97 }
98 }
99 return ret;
100}
101
102bool GetDoubleFromJson(const Json::Value& in, double* out) {
103 bool ret;
104 if (!in.isString()) {
105 ret = in.isConvertibleTo(Json::realValue);
106 if (ret) {
107 *out = in.asDouble();
108 }
109 } else {
110 double val;
111 const char* c_str = in.asCString();
112 char* end_ptr;
113 errno = 0;
114 val = strtod(c_str, &end_ptr);
115 ret = (end_ptr != c_str && *end_ptr == '\0' && !errno);
116 *out = val;
117 }
118 return ret;
119}
120
121namespace {
Yves Gerey665174f2018-06-19 15:03:05 +0200122template <typename T>
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000123bool JsonArrayToVector(const Json::Value& value,
124 bool (*getter)(const Json::Value& in, T* out),
Yves Gerey665174f2018-06-19 15:03:05 +0200125 std::vector<T>* vec) {
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000126 vec->clear();
127 if (!value.isArray()) {
128 return false;
129 }
130
131 for (Json::Value::ArrayIndex i = 0; i < value.size(); ++i) {
132 T val;
133 if (!getter(value[i], &val)) {
134 return false;
135 }
136 vec->push_back(val);
137 }
138
139 return true;
140}
141// Trivial getter helper
142bool GetValueFromJson(const Json::Value& in, Json::Value* out) {
143 *out = in;
144 return true;
145}
146} // unnamed namespace
147
148bool JsonArrayToValueVector(const Json::Value& in,
149 std::vector<Json::Value>* out) {
150 return JsonArrayToVector(in, GetValueFromJson, out);
151}
152
Yves Gerey665174f2018-06-19 15:03:05 +0200153bool JsonArrayToIntVector(const Json::Value& in, std::vector<int>* out) {
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000154 return JsonArrayToVector(in, GetIntFromJson, out);
155}
156
157bool JsonArrayToUIntVector(const Json::Value& in,
158 std::vector<unsigned int>* out) {
159 return JsonArrayToVector(in, GetUIntFromJson, out);
160}
161
162bool JsonArrayToStringVector(const Json::Value& in,
163 std::vector<std::string>* out) {
164 return JsonArrayToVector(in, GetStringFromJson, out);
165}
166
Yves Gerey665174f2018-06-19 15:03:05 +0200167bool JsonArrayToBoolVector(const Json::Value& in, std::vector<bool>* out) {
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000168 return JsonArrayToVector(in, GetBoolFromJson, out);
169}
170
Yves Gerey665174f2018-06-19 15:03:05 +0200171bool JsonArrayToDoubleVector(const Json::Value& in, std::vector<double>* out) {
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000172 return JsonArrayToVector(in, GetDoubleFromJson, out);
173}
174
175namespace {
Yves Gerey665174f2018-06-19 15:03:05 +0200176template <typename T>
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000177Json::Value VectorToJsonArray(const std::vector<T>& vec) {
178 Json::Value result(Json::arrayValue);
179 for (size_t i = 0; i < vec.size(); ++i) {
180 result.append(Json::Value(vec[i]));
181 }
182 return result;
183}
184} // unnamed namespace
185
186Json::Value ValueVectorToJsonArray(const std::vector<Json::Value>& in) {
187 return VectorToJsonArray(in);
188}
189
190Json::Value IntVectorToJsonArray(const std::vector<int>& in) {
191 return VectorToJsonArray(in);
192}
193
194Json::Value UIntVectorToJsonArray(const std::vector<unsigned int>& in) {
195 return VectorToJsonArray(in);
196}
197
198Json::Value StringVectorToJsonArray(const std::vector<std::string>& in) {
199 return VectorToJsonArray(in);
200}
201
202Json::Value BoolVectorToJsonArray(const std::vector<bool>& in) {
203 return VectorToJsonArray(in);
204}
205
206Json::Value DoubleVectorToJsonArray(const std::vector<double>& in) {
207 return VectorToJsonArray(in);
208}
209
Yves Gerey665174f2018-06-19 15:03:05 +0200210bool GetValueFromJsonArray(const Json::Value& in, size_t n, Json::Value* out) {
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000211 if (!in.isArray() || !in.isValidIndex(static_cast<int>(n))) {
212 return false;
213 }
214
215 *out = in[static_cast<Json::Value::ArrayIndex>(n)];
216 return true;
217}
218
Yves Gerey665174f2018-06-19 15:03:05 +0200219bool GetIntFromJsonArray(const Json::Value& in, size_t n, int* out) {
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000220 Json::Value x;
221 return GetValueFromJsonArray(in, n, &x) && GetIntFromJson(x, out);
222}
223
Yves Gerey665174f2018-06-19 15:03:05 +0200224bool GetUIntFromJsonArray(const Json::Value& in, size_t n, unsigned int* out) {
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000225 Json::Value x;
226 return GetValueFromJsonArray(in, n, &x) && GetUIntFromJson(x, out);
227}
228
Yves Gerey665174f2018-06-19 15:03:05 +0200229bool GetStringFromJsonArray(const Json::Value& in, size_t n, std::string* out) {
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000230 Json::Value x;
231 return GetValueFromJsonArray(in, n, &x) && GetStringFromJson(x, out);
232}
233
Yves Gerey665174f2018-06-19 15:03:05 +0200234bool GetBoolFromJsonArray(const Json::Value& in, size_t n, bool* out) {
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000235 Json::Value x;
236 return GetValueFromJsonArray(in, n, &x) && GetBoolFromJson(x, out);
237}
238
Yves Gerey665174f2018-06-19 15:03:05 +0200239bool GetDoubleFromJsonArray(const Json::Value& in, size_t n, double* out) {
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000240 Json::Value x;
241 return GetValueFromJsonArray(in, n, &x) && GetDoubleFromJson(x, out);
242}
243
Yves Gerey665174f2018-06-19 15:03:05 +0200244bool GetValueFromJsonObject(const Json::Value& in,
245 const std::string& k,
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000246 Json::Value* out) {
247 if (!in.isObject() || !in.isMember(k)) {
248 return false;
249 }
250
251 *out = in[k];
252 return true;
253}
254
Yves Gerey665174f2018-06-19 15:03:05 +0200255bool GetIntFromJsonObject(const Json::Value& in,
256 const std::string& k,
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000257 int* out) {
258 Json::Value x;
259 return GetValueFromJsonObject(in, k, &x) && GetIntFromJson(x, out);
260}
261
Yves Gerey665174f2018-06-19 15:03:05 +0200262bool GetUIntFromJsonObject(const Json::Value& in,
263 const std::string& k,
264 unsigned int* out) {
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000265 Json::Value x;
266 return GetValueFromJsonObject(in, k, &x) && GetUIntFromJson(x, out);
267}
268
Yves Gerey665174f2018-06-19 15:03:05 +0200269bool GetStringFromJsonObject(const Json::Value& in,
270 const std::string& k,
271 std::string* out) {
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000272 Json::Value x;
273 return GetValueFromJsonObject(in, k, &x) && GetStringFromJson(x, out);
274}
275
Yves Gerey665174f2018-06-19 15:03:05 +0200276bool GetBoolFromJsonObject(const Json::Value& in,
277 const std::string& k,
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000278 bool* out) {
279 Json::Value x;
280 return GetValueFromJsonObject(in, k, &x) && GetBoolFromJson(x, out);
281}
282
Yves Gerey665174f2018-06-19 15:03:05 +0200283bool GetDoubleFromJsonObject(const Json::Value& in,
284 const std::string& k,
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000285 double* out) {
286 Json::Value x;
287 return GetValueFromJsonObject(in, k, &x) && GetDoubleFromJson(x, out);
288}
289
290std::string JsonValueToString(const Json::Value& json) {
291 Json::FastWriter w;
292 std::string value = w.write(json);
293 return value.substr(0, value.size() - 1); // trim trailing newline
294}
Thiago Farinacb76b892015-04-02 09:59:15 +0000295
296} // namespace rtc